Given a full string Knasweg 12, 9062 Knasweg, Österreich
, how do I highlight (and return) only the substring Knasweg
if I search for this exact substring?
In other words, I'd like this query:
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"
}
}
}
}
}
}
}
}
}
to return
"highlight": {
"location.pretty_address": [
"Knasweg"
]
}
instead of
"highlight": {
"location.pretty_address": [
"Knasweg 12, 9062 Knasweg, Österreich"
]
}
My mapping:
"location": {
"dynamic": "true",
"properties": {
"pretty_address": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
},
"analyzer": "autocomplete_analyzer"
}
}
My settings:
"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"
}
}
}
}
}
According to the documentation - here - you should add the fragment_size
param and set it to 1 where 1 is the number of tokens in your query:
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"
}
}
}
}
}
}
}
}
}