我正在使用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"
}
}
},
[...]
}
}
}
}
}
最佳答案
请查看 cross_fields
查询文档。那里有一个operator
参数,如果不存在,则将其设置为OR
。这意味着您当前的查询正在从字段列表"David Salazar Denver"
中的["first_name","middle_name", "last_name", "city", "county", "street"]
搜索任何术语。这基本上意味着,只要在您的任何字段中找到来自搜索查询的一个单词,就会从搜索中返回文档。
关于elasticsearch - ElasticSearch查询忽略其他搜索词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50113806/