elasticsearchmatch-phrase

How to turn off autocomplete for easticsearch match_phrase or match_phrase_prefix?


I have ES data, which contains a field name of type text. I have to search by a lowercase input, while the actual name might use lower and uppercase symbols. I need only the exact (but case insensitive) names.

I try to use match_phrase (as well as match_phrase_prefix). But it returns results with autocompleting. Like query

"match_phrase": {
  "name": {
    "query": "apple iphone 11"
  }
}

returns two items:

{
"id": "547",
"name": "Apple iPhone 11",
}

and

{
"id": "253",
"name": "Apple iPhone 11 Pro",
}

I need only the one with id: 547, i.e. where there are no extra symbols in the name.

Does Elastcsearch have tools to find the exact name, but in a case insensitive form and without autocomplete?


Solution

  • I achieved my needs via a simple script:

    "filter": [
        {
            "script": {
                "script": {
                    "source": "doc[params.nameField].value != null && doc[params.nameField].value.equalsIgnoreCase(params.name)",
                    "lang": "painless",
                    "params": {
                        "name": "apple iphone 11",
                        "nameField": "name.exact"
                    }
                },
                "boost": 1.0
            }
        }
    ]