Elasticsearch:return 仅突出显示 substring/keyword 个匹配项
Elasticsearch: return only highlighted substring/keyword matches
给定一个完整的字符串 Knasweg 12, 9062 Knasweg, Österreich
,如果我搜索这个确切的子字符串,我如何仅突出显示(和 return)子字符串 Knasweg
?
换句话说,我想要这个查询:
GET _search
{
"query": {
"match": {
"location.pretty_address": {
"query": "Knasweg",
"operator": "and",
"fuzziness": 1
}
}
},
"highlight": {
"pre_tags": "",
"post_tags": "",
"fields": {
"location.pretty_address": {
"highlight_query": {
"bool": {
"must": {
"match": {
"location.pretty_address": {
"query": "Knasweg"
}
}
}
}
}
}
}
}
}
到return
"highlight": {
"location.pretty_address": [
"Knasweg"
]
}
而不是
"highlight": {
"location.pretty_address": [
"Knasweg 12, 9062 Knasweg, Österreich"
]
}
我的映射:
"location": {
"dynamic": "true",
"properties": {
"pretty_address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
},
"analyzer": "autocomplete_analyzer"
}
}
我的设置:
"settings": {
"index": {
"analysis": {
"analyzer": {
"comma_analyzer": {
"tokenizer": "comma_tokenizer"
},
"autocomplete_analyzer": {
"filter": "lowercase",
"tokenizer": "autocomplete_tokenizer"
}
},
"tokenizer": {
"autocomplete_tokenizer": {
"type": "ngram",
"min_gram": "3",
"max_gram": "20"
},
"comma_tokenizer": {
"pattern": ", ",
"type": "pattern"
}
}
}
}
}
根据文档 - here - 您应该添加 fragment_size
参数并将其设置为 1,其中 1 是查询中的标记数:
GET _search
{
"query": {
"match": {
"location.pretty_address": {
"query": "Knasweg",
"operator": "and",
"fuzziness": 1
}
}
},
"highlight": {
"pre_tags": "",
"post_tags": "",
"fragment_size" : 1,
"fields": {
"location.pretty_address": {
"highlight_query": {
"bool": {
"must": {
"match": {
"location.pretty_address": {
"query": "Knasweg"
}
}
}
}
}
}
}
}
}
给定一个完整的字符串 Knasweg 12, 9062 Knasweg, Österreich
,如果我搜索这个确切的子字符串,我如何仅突出显示(和 return)子字符串 Knasweg
?
换句话说,我想要这个查询:
GET _search
{
"query": {
"match": {
"location.pretty_address": {
"query": "Knasweg",
"operator": "and",
"fuzziness": 1
}
}
},
"highlight": {
"pre_tags": "",
"post_tags": "",
"fields": {
"location.pretty_address": {
"highlight_query": {
"bool": {
"must": {
"match": {
"location.pretty_address": {
"query": "Knasweg"
}
}
}
}
}
}
}
}
}
到return
"highlight": {
"location.pretty_address": [
"Knasweg"
]
}
而不是
"highlight": {
"location.pretty_address": [
"Knasweg 12, 9062 Knasweg, Österreich"
]
}
我的映射:
"location": {
"dynamic": "true",
"properties": {
"pretty_address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
},
"analyzer": "autocomplete_analyzer"
}
}
我的设置:
"settings": {
"index": {
"analysis": {
"analyzer": {
"comma_analyzer": {
"tokenizer": "comma_tokenizer"
},
"autocomplete_analyzer": {
"filter": "lowercase",
"tokenizer": "autocomplete_tokenizer"
}
},
"tokenizer": {
"autocomplete_tokenizer": {
"type": "ngram",
"min_gram": "3",
"max_gram": "20"
},
"comma_tokenizer": {
"pattern": ", ",
"type": "pattern"
}
}
}
}
}
根据文档 - here - 您应该添加 fragment_size
参数并将其设置为 1,其中 1 是查询中的标记数:
GET _search
{
"query": {
"match": {
"location.pretty_address": {
"query": "Knasweg",
"operator": "and",
"fuzziness": 1
}
}
},
"highlight": {
"pre_tags": "",
"post_tags": "",
"fragment_size" : 1,
"fields": {
"location.pretty_address": {
"highlight_query": {
"bool": {
"must": {
"match": {
"location.pretty_address": {
"query": "Knasweg"
}
}
}
}
}
}
}
}
}