1

I'm using the code below and it does not give auto-suggestion as curd when i type "cu"

But it does match the document with yogurt which is correct. How can I get both auto-complete for synonym words and document match for the same?

PUT products
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "synonym_analyzer": {
            "tokenizer": "standard",
            "filter": [
            "lowercase",
              "synonym_graph"
            ]
          }
        },
        "filter": {
          "synonym_graph": {
            "type": "synonym_graph",
            "synonyms": [
               "yogurt, curd, dahi"
            ]
          }
        }
      }
    }
  }
}
PUT products/_mapping
{
  "properties": {
    "description": {
      "type": "text",
      "analyzer": "synonym_analyzer"
    }
  }
}
POST products/_doc
{
  "description": "yogurt"
}
GET products/_search
{
  "query": {
    "match": {
      "description": "cu"
    }
  }
}
Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68
jason
  • 3,932
  • 11
  • 52
  • 123

1 Answers1

0

When you provide a list of synonyms in a synonym_graph filter it simply means that ES will treat any of the synonyms interchangeably. But when they're analyzed via the standard analyzer, only full-word tokens will be produced:

POST products/_analyze?filter_path=tokens.token
{
  "text": "yogurt",
  "field": "description"
}

yielding:

{
  "tokens" : [
    {
      "token" : "curd"
    },
    {
      "token" : "dahi"
    },
    {
      "token" : "yogurt"
    }
  ]
}

As such, a regular match_query won't cut it here because the standard analyzer hasn't provided it with enough context in terms of matchable substrings (n-grams).

In the meantime you can replace match with match_phrase_prefix which does exactly what you're after -- match an ordered sequence of characters while taking into account the synonyms:

GET products/_search
{
  "query": {
    "match_phrase_prefix": {
      "description": "cu"
    }
  }
}

But that, as the query name suggests, is only going to work for prefixes. If you fancy an autocomplete that suggests terms regardless of where the substring matches occur, have a look at my other answer where I talk about leveraging n-grams.

Joe - GMapsBook.com
  • 15,787
  • 4
  • 23
  • 68
  • Thanks @Joe for looking into this. I am using the same query above and it matches the right document but I dont get **curd** in the autosuggest as it is the synonym but I get match for **yogurt**. I wanted even the suggestion as **curd**. Currently, I dont get any suggestion except yogurt. Please can you let me know how I can achieve that? Thanks! – jason Mar 06 '21 at 20:29
  • 1
    You're welcome. The thing is, it only works in one direction -- i.e. you search for curd and get yogurt back because the two are said to be synonyms. Having said that, you'll only receive the actual document that you stored in ES -- i.e. the one with yogurt. ES cannot magically return documents (or document "alternatives" that doesn't store. Hope that makes sense. So your best bet is to have a hashmap of synonyms where you post-process the ES response and enrich the results with synonyms before they're passed to a frontend autocomplete. – Joe - GMapsBook.com Mar 06 '21 at 21:50
  • Thanks for the suggestion. So, I'm using autocomplete library for suggestions then passing it to ES. Thanks a ton! – jason Mar 07 '21 at 09:35
  • You're welcome @jason! Hey btw -- my handbook containing actionable elasticsearch guides is coming very soon! You can [let me know](https://jozefsorocin.typeform.com/to/XeQRxdwV) which topics interest you the most and I'll let you know when it's out. – Joe - GMapsBook.com Mar 07 '21 at 18:10
  • Nice Joe! These topics - Implementation of End to End application as search and sub-modules like Knowledge Graph implementation and unfolding and ranking and recommendations. Also above $150 is fine. Also, exception case handling with intent detection. – jason Mar 07 '21 at 23:02
  • Cool! Would you mind filling out [the typeform](https://jozefsorocin.typeform.com/to/XeQRxdwV) (also shared above)? I wouldn't want you to paste your email here :) – Joe - GMapsBook.com Mar 07 '21 at 23:09
  • Done! You can probably have a website like https://www.educative.io where you can keep updating courses instead of 1 book. You can cover more topics in depth and we can access it from anywhere. I guess this might help everyone! All the best. Let me know when it is out! – jason Mar 08 '21 at 15:23
  • Thanks for signing up & for the tip! – Joe - GMapsBook.com Mar 08 '21 at 16:17
  • My Pleasure! How can I reach out to you? Is there any stackoverflow messenger or chat? – jason Mar 08 '21 at 17:44
  • You can reach me at joe@elasticsearchbook.com! – Joe - GMapsBook.com Mar 08 '21 at 20:00
  • Cool! Thanks Joe! – jason Mar 09 '21 at 09:26