简体   繁体   中英

SuggestionBuilder with BoolQueryBuilder in Elasticsearch

I am currently using BoolQueryBuilder to build a text search. I am having an issue with wrong spellings. When someone searches for a "chiar" instead of "chair" I have to show them some suggestions.

I have gone through the documentation and observed that the SuggestionBuilder is useful to get the suggestions.

Can I send all the requests in a single query, so that I can show the suggestions if the result is zero?

No need to send different search terms ie chair , chiar to get suggestions, it's not efficient and performant and you don't know all the combinations which user might misspell.

Instead, Use the fuzzy query or fuzziness param in the match query itself, which can be used in the bool query.

Let me show you an example, using the match query with the fuzziness parameter.

index def

{
    "mappings": {
        "properties": {
            "product": {
                "type": "text"
            }
        }
    }
}

Index sample doc

{
  "product" : "chair"
}

Search query with wrong term chiar

{
    "query": {
        "match" : {
            "product" : {
                "query" : "chiar",
                "fuzziness" : "4" --> control it according to your application
            }
        }
    }
}

Search result

 "hits": [
      {
        "_index": "so_fuzzy",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.23014566,
        "_source": {
          "product": "chair"
        }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM