我正在使用ElasticSearch在多个结构化字段上构建一个自由形式搜索即用型功能。人们搜索的主要字段是first_name
,last_name
和city
。
问题:以下两次搜索David Salazar
和David Salazar Denver
返回的结果与“Denver”似乎被忽略的结果相同。
我认为这是我的查询的问题,但我仍然坚持如何改变它以获得我正在寻找的东西。
以下是查询:
GET index_name/_search
{
"query": {
"multi_match": {
"fields": [
"first_name","middle_name", "last_name", "city", "county", "street"],
"query": "David Salazar Denver",
"type": "cross_fields",
"use_dis_max": false
}
},
"size": 10
}
以下是索引设置和字段映射的相关部分
{
"index": {
"aliases": {},
"mappings": {
"type": {
"properties": {
"city": {
"type": "keyword"
},
"county": {
"type": "keyword"
},
"first_name": {
"type": "text",
"analyzer": "synonym_autocomplete",
"search_analyzer": "standard"
},
"last_name": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"middle_name": {
"type": "text",
"analyzer": "synonym_autocomplete",
"search_analyzer": "standard"
},
"street": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
}
}
},
"settings": {
"index": {
[...]
"analysis": {
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [Long list of nicknames]
},
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": "2",
"max_gram": "15"
}
},
"analyzer": {
"synonym_autocomplete": {
"filter": [
"standard", "lowercase", "synonym", "autocomplete_filter"],
"type": "custom",
"tokenizer": "standard"
},
"autocomplete": {
"filter": ["standard","lowercase","autocomplete_filter"],
"type": "custom",
"tokenizer": "standard"
}
}
},
[...]
}
}
}
}
}
答案 0 :(得分:0)
请查看cross_fields
查询文档。您有一个operator
参数,如果不存在则设置为OR
。这意味着您当前的查询正在搜索字段列表"David Salazar Denver"
中["first_name","middle_name", "last_name", "city", "county", "street"]
的任何字词。这基本上意味着只要在您的任何字段中找到搜索查询中的一个单词,就会从搜索中返回文档。
答案 1 :(得分:0)
Val是正确的,主要问题是cross_fields
仅适用于使用相同分析器的字段。
所以我使用下面的代码创建了一个新索引,然后使用reindex
API将数据复制到这个新索引
{
"index": {
"aliases": {},
"mappings": {
"type": {
"properties": {
"city": {
"type": "keyword"
},
"county": {
"type": "text",
"analyzer": "synonym_autocomplete",
"search_analyzer": "standard"
},
"first_name": {
"type": "text",
"analyzer": "synonym_autocomplete",
"search_analyzer": "standard"
},
"last_name": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"middle_name": {
"type": "text",
"analyzer": "synonym_autocomplete",
"search_analyzer": "standard"
},
"street": {
"type": "text",
"analyzer": "synonym_autocomplete",
"search_analyzer": "standard"
},
}
}
},
"settings": {
"index": {
[...]
"analysis": {
"filter": {
"synonym": {
"type": "synonym",
"synonyms": [Long list of nicknames]
},
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": "2",
"max_gram": "15"
}
},
"analyzer": {
"synonym_autocomplete": {
"filter": [
"standard", "lowercase", "synonym", "autocomplete_filter"],
"type": "custom",
"tokenizer": "standard"
},
"autocomplete": {
"filter": ["standard","lowercase","autocomplete_filter"],
"type": "custom",
"tokenizer": "standard"
}
}
},
[...]
}
}
}
}
}