0

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?

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
sree
  • 868
  • 2
  • 12
  • 35

1 Answers1

0

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"
        }
      }
Amit
  • 30,756
  • 6
  • 57
  • 88
  • Thanks for the input..But I only need the suggestions not the auto-corrected results – sree Mar 24 '20 at 08:29
  • @sree, what do you mean by just suggestions? – Amit Mar 24 '20 at 08:57
  • Got it, can you explain this `Can I send all the requests in a single query , so that I can show the suggestions if the results are zero` in details, what do you mean by all the requests in which single query, please explain it using some example – Amit Mar 24 '20 at 09:17
  • Currently, We are sending a search query to get the results.let's say when we search with a chair. It will return the results matching with a chair. Now if I search with "chiar" as no matches it is returning zero results. When we have zero results I have to show the suggestion. For suggestions, we have to send another query with suggestion builder. I wanted to know is there any extra attribute to pass on the search query to get the suggestions in the first request. – sree Mar 24 '20 at 09:25
  • @sree, Intresting, let me see if its possible, can you share your code for creating the suggest query and normal search query, so that I don't hv to write them – Amit Mar 24 '20 at 09:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210214/discussion-between-sree-and-opster-elasticsearch-ninja). – sree Mar 24 '20 at 09:36