curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"my_type": {
"properties": {
"city": {
"type": "text",
"fields": {
"raw": { #1
"type": "keyword"
}
}
}
}
}
}
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
"city": "New York"
}
'
curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -H 'Content-Type: application/json' -d'
{
"city": "York"
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"city": "york" #2
}
},
"sort": {
"city.raw": "asc" #3
},
"aggs": {
"Cities": {
"terms": {
"field": "city.raw" #4
}
}
}
}
'
备注
Multi_fields不会改变原始的_source字段。
注意
同一索引相同字段名可以设置不同的fields。可以通过PUT mapping API 在已经存在的字段加入新的multi-fields。
Multi-fields with multiple analyzers(多分析器处理多字段)
curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
"mappings": {
"my_type": {
"properties": {
"text": { #1
"type": "text",
"fields": {
"english": { #2
"type": "text",
"analyzer": "english"
}
}
}
}
}
}
}
'
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{ "text": "quick brown fox" } #3
'
curl -XPUT 'localhost:9200/my_index/my_type/2?pretty' -H 'Content-Type: application/json' -d'
{ "text": "quick brown foxes" } #4
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"multi_match": {
"query": "quick brown foxes",
"fields": [ #5
"text",
"text.english"
],
"type": "most_fields" #6
}
}
}
'