0

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?

4

2 回答 2

0

Does Elastcsearch have tools to find the exact name?

Yes, Elastic search provides a "keyword" type for exact search.

in a case insensitive form and without autocomplete?

You can use a normalizer with a lowercase filter

  1. Add Normalizer in index setting

PUT /so_index/

{
   "settings":{
      "analysis":{
         "normalizer":{
            "name_normalizer":{
               "type":"custom",
               "filter":[
                  "lowercase"
               ]
            }
         }
      }
   }
}
  1. Mapping (Either you use the name as a keyword for just exact match or use both keyword and text for exact search and full-text search)

PUT /so_index/_mapping

{
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "normalizer": "name_normalizer"
        }
      }
    }
  }
}
  1. Use Match or Term Query

GET /so_index/_search

{
  "query": {
    "match": {
      "name.keyword": "apple iphone 11"
    }
  }
}
于 2021-03-02T14:07:00.243 回答
0

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
        }
    }
]
于 2021-03-04T12:18:51.887 回答