Searching by ngrams

I use search with query string in index analyzed with ngrams.
When I try to search doc with field_1 = SU0001023277 it's ok. But when I try to use less letters, like SU0001023 the resultset is 0. Why it comes out like this?
Index mapping:

PUT /instruments
{
"settings": {
"index": {
"max_ngram_diff": 20
},
"analysis": {
"analyzer": {
"autocomplete": {
"tokenizer": "autocomplete",
"filter": [
"lowercase"
]
},
"autocomplete_search": {
"tokenizer": "standard",
"filter": [
"lowercase"
]
}
},
"tokenizer": {
"autocomplete": {
"type": "ngram",
"min_gram": 1,
"max_gram": 15
}
}
}
},
"mappings": {
"properties": {
"field_1": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "autocomplete_search",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}

Which version of Elasticsearch are you using?

What does your query look like?

1 Like

I use search template:

PUT _scripts/liquid_instruments_search
{
"script": {
"lang": "mustache",
"source": {
"query": {
"bool": {
"must": [
{
"match": {
"field_1": "true"
}
},
{
"match": {
"field_2": "true"
}
},
{
"match":{
"field_3":"true"
}
},
{
"bool": {
"should": [
{
"match": {
"field_4": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 4
}
}
},
{
"match": {
"field_5": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 3
}
}
},
{
"match": {
"field_6": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 2
}
}
},
{
"match": {
"field_7": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 1
}
}
}
]
}
}
]
}
},
"from": "{{from}}{{^from}}0{{/from}}",
"size": "{{size}}{{^size}}10{{/size}}"
},
"params": {
"query_string": "Query string"
}
}
}
GET instruments/_search/template
{
"id": "liquid_instruments_search",
"params": {
"query_string": "SU00090212",
"from": 0,
"size": 10
}
}

It looks like the query string does not get applied against field_1, which you provided mapping for.

Please provide a full example that anyone can use to reproduce the issue. This includes mappings, settings, sample documents, data and queries run.

I also helps to know which version you are using.

Sorry, here is correct template:

PUT _scripts/liquid_instruments_search
{
"script": {
"lang": "mustache",
"source": {
"query": {
"bool": {
"must": [
{
"match": {
"field_0": "true"
}
},
{
"match": {
"field_0.1": "true"
}
},
{
"match":{
"field_3":"true"
}
},
{
"bool": {
"should": [
{
"match": {
"field_1": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 4
}
}
},
{
"match": {
"field_2": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 3
}
}
},
{
"match": {
"field_3": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 2
}
}
},
{
"match": {
"field_4": {
"query": "{{query_string}}",
"fuzziness": "1",
"boost": 1
}
}
}
]
}
}
]
}
},
"from": "{{from}}{{^from}}0{{/from}}",
"size": "{{size}}{{^size}}10{{/size}}"
},
"params": {
"query_string": "Query string"
}
}
}
GET instruments/_search/template
{
"id": "liquid_instruments_search",
"params": {
"query_string": "SU00090212",
"from": 0,
"size": 10
}
}

Can you share a document you expect to match that does not?

Can you please also provide this information?

Version is 7.17.5.

Here is the doc:

"hits" : [
{
"_index" : "instruments",
"_type" : "_doc",
"_id" : "144942",
"_score" : 14.99827,
"_source" : {
"actId" : "2100",
"isin" : "SU00090212777",
"objectId" : "174126",
"isLiquidBoard" : "true",
"lot" : "1"
}
}
]

The template seems to check that 3 fields (with different names) are set to "true" in addition to field_1, but there is as far as I can see only 1 field set to "true" in the sample document. Please provide a full example that fully reproduces the issue.

The problem is not in these fields. I wrote in my question, that I can find this doc with all conditions above when I search for the whole name of field_1: "SU00090212777". But when I try to use this statement in query string: "SU0009021" the resultset is empty.

And all other conditions are the same

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.