I am trying to write query for search as you type with fuzziness added.(elastic search 7.12)
{
"query": {
"multi_match": {
"query": "airl recl",
"fields": [
"tags",
"display_text",
"display_subtext"
],
"type" : "most_fields",
"operator": "and",
"fuzziness": "AUTO:4,6",
"prefix_length" :2
}
}
}
I have inserted docs with "airtel recharge" values. I am also using edge n gram(1:50) for above given 3 fields along with space analyzer.
space analyzer :
"words_with_spaces_analyzer" : {
"filter" : [
"lowercase",
"asciifolding"
],
"type" : "custom",
"tokenizer" : "words_with_space"
}
},
"tokenizer" : {
"words_with_space" : {
"pattern" : "([a-zA-Z0-9.-]+[\\s]*)",
"type" : "pattern",
"group" : "0"
}
}
},
Mapping
"display_text": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
},
"analyzer": "edge_nGram_analyzer",
"search_analyzer": "words_with_spaces_analyzer"
}
Can someone please help me in understanding why above given query behaves this way for multi token input whereas both the token giving output if run them separately ?
You need to use a whitespace analyzer, as a search_analyzer
. This will break the search term "airl recl"
into airl
and recl
. And, then will perform the search on these individual tokens.
Adding a working example with index mapping, search query, and search result
Index Mapping:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 50,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 50
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer",
"search_analyzer": "whitespace"
}
}
}
}
Index Data:
{
"name": "airtel recharge"
}
Search Query:
{
"query": {
"multi_match": {
"query": "airl recl",
"fields": [
"name"
],
"type": "most_fields",
"operator": "and",
"fuzziness": "AUTO:4,6",
"prefix_length": 2
}
}
}
Search Result:
"hits": [
{
"_index": "67702617",
"_type": "_doc",
"_id": "1",
"_score": 0.22729424,
"_source": {
"name": "airtel recharge"
}
}
]