【ElasticSearch-基础篇】Mapping结构
warning:
这篇文章距离上次修改已过444天,其中的内容可能已经有所变动。
在Elasticsearch中,Mapping是一个非常重要的概念,它定义了文档的字段名和字段类型,以及这些字段的特定配置。
以下是一个创建Mapping的基本示例:
PUT /my_index
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"email": {
"type": "keyword"
},
"name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
}
}
}
}在这个例子中,我们创建了一个名为my_index的索引,并定义了三个字段:age(整数类型),email(关键字类型),和name(文本类型)。name字段有一个额外的子字段name.raw,类型为关键字,用于不分析的搜索。
在Elasticsearch 7.0+中,创建索引和Mapping的语法略有不同:
PUT /my_index
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"email": {
"type": "keyword"
},
"name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}在Elasticsearch 8.0+中,字符串类型由text和keyword合并为text类型,并引入了search_analyzer和normalizer等新特性:
PUT /my_index
{
"mappings": {
"properties": {
"age": {
"type": "integer"
},
"email": {
"type": "keyword"
},
"name": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}注意:在Elasticsearch 8.0+中,string类型替代了text和keyword类型,并且fields属性中可以指定多种子字段,包括search_analyzer和normalizer。
评论已关闭