Search code examples
azure-cognitive-searchn-gram

Differences between Suggesters and NGram


I've built an index with a Custom Analyzer

"analyzers": [
    {
      "@odata.type": "#Microsoft.Azure.Search.CustomAnalyzer",
      "name": "ingram",
      "tokenizer": "whitespace",
      "tokenFilters": [ "lowercase", "NGramTokenFilter" ],
      "charFilters": []
    }
  ],
  "tokenFilters": [
    {
      "@odata.type": "#Microsoft.Azure.Search.NGramTokenFilterV2",
      "name": "NGramTokenFilter",
      "minGram": 3,
      "maxGram": 8
    }
  ],

I came upon Suggesters and was wondering what the pros/cons were between these 2 approaches.

Basically, I'm doing an JavaScript autocomplete text box. I need to do partial text search inside of the search text (i.e. search=ell would match on "Hello World".


Solution

  • Azure Search offers two features to enable this depending on the experience you want to give to your users: - Suggestions: https://learn.microsoft.com/en-us/rest/api/searchservice/suggestions - Autocomplete: https://learn.microsoft.com/en-us/rest/api/searchservice/autocomplete

    Suggestions will return a list of matching documents even with incomplete query terms, and you are right that it can be reproduced with a custom analyzer that uses ngrams. It's just a simpler way to accomplish that (since we took care of setting up the analyzer for you).

    Autocomplete is very similar, but instead of returning matching documents, it will simply return a list of completed "terms" that match the incomplete term in your query. This will make sure terms are not duplicated in the autocomplete list (which can happen when using the suggestions API, since as I mentioned above, suggestions return matching documents, rather than a list of terms).