multi_field (多域类型)允许你对同一个值以映射的方式定义成多个基本类型。这个非常有用,比如,一个 string 类型字段可以被映射成 text 字段作为 full-text 进行搜索,同时也可以作为 keyword 字段用于排序和聚合。
下面来看个例子:
PUT /multi_field_test
{
"mappings": {
"properties": {
"description":{
"type": "text"
},
"ingredients":{
"type": "text",
"fields": {
"keyword":{
"type":"keyword"
}
}
}
}
}
}
我们是定义一个名为 ingredients 的字段, 它的数据类型是 string 字符类型, 该字段映射了两次(实际物理上产生了2个索引字段),一个是text 分词类型,另外一个定义成了keyword 类型,即不分词处理。
往索引中插入一条文档:
POST /multi_field_test/_doc
{
"description": "To make this spaghetti carbonara...",
"ingredients": ["Spaghetti", "Bacon", "Eggs"]
}

此时ingredients字段会产生两个索引(text和keyword):

进行match query查询:

进行term query查询:

可见无论是使用match query还是term query,都能查询到结果