Elasticsearch exact matches on analyzed fields

后端 未结 3 899
清酒与你
清酒与你 2020-12-06 04:28

Is there a way to have ElasticSearch identify exact matches on analyzed fields? Ideally, I would like to lowercase, tokenize, stem and perhaps even phoneticize my docs, then

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 05:03

    You can keep the analyzer as what you expected (lowercase, tokenize, stem, ...), and use query_string as the main query, match_phrase as the boosting query to search. Something like this:

    {
       "bool" : {
          "should" : [
             {
                "query_string" : {
                   "default_field" : "your_field",
                   "default_operator" : "OR",
                   "phrase_slop" : 1,
                   "query" : "Hamburger"
                }
             },
             {
                "match_phrase": {
                   "your_field": {
                      "query": "Hamburger"
                   }
                }
             }
          ]
       }
    }
    

    It will match both documents, and exact match (match_phrase) will be on top since the query match both should clauses (and get higher score)

    default_operator is set to OR, it will help the query "Hamburger Buns" (match hamburger OR bun) match the document "Hamburger" also. phrase_slop is set to 1 to match terms with distance = 1 only, e.g. search for Hamburger Buns will not match document Hamburger Big Buns. You can adjust this depend on your requirements.

    You can refer Closer is better, Query string for more details.

提交回复
热议问题