Suggest query issue

Hi,
I am using Suggest query for a Did-you-mean functionality based on this tutorial Create a simple “did you mean” — ElasticSearch but there is an issue.

If a typo happens in the middle of a word, the suggest query returns relative phrases and it's fine but if the typo happens in the beginning of a word, It mostly shows irrelative options or shows no options.

for example consider this
typeo is in the middle of the word, linox instead of linux

GET my_index/_search
{
  "suggest": {
    "text": "linox",
    "did_you_mean": {
      "phrase": {
        "field": "title.suggest",
        "size": 5,
        "confidence": 1,
        "max_errors":2,
        "collate": {
          "query": { 
            "source" : {
              "match": {
                "{{field_name}}": {
                  "query": "{{suggestion}}",
                  "operator": "and"
                }
              }
            }
          },
          "params": {"field_name" : "title"}, 
          "prune" :false
        }
      }
    }
  }
}

and It returns reletive options:

{
  "took": 8,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "suggest": {
    "did_you_mean": [
      {
        "text": "linox",
        "offset": 0,
        "length": 5,
        "options": [
          {
            "text": "linux",
            "score": 0.019855337
          },
          {
            "text": "link",
            "score": 0.0065617864
          }
        ]
      }
    ]
  }
}

But the problem is when the typo is at the first of the word. consider this:
Iinux (starting with capital i)

GET my_index/_search
{
  "suggest": {
    "text": "Iinux",
    "did_you_mean": {
      "phrase": {
        "field": "title.suggest",
        "size": 5,
        "confidence": 1,
        "max_errors":2,
        "collate": {
          "query": { 
            "source" : {
              "match": {
                "{{field_name}}": {
                  "query": "{{suggestion}}",
                  "operator": "and"
                }
              }
            }
          },
          "params": {"field_name" : "title"}, 
          "prune" :false
        }
      }
    }
  }
}

and the output is empty:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "suggest": {
    "did_you_mean": [
      {
        "text": "Iinox",
        "offset": 0,
        "length": 5,
        "options": []
      }
    ]
  }
}

Both words Iinux and linox only have once character mistake and both are really similar to linux.
Why Suggest query has this behavior and how can i fix this issue?

Thanks in advance.

Hi @jahedi

Change this prefix_length property to 0.

Doc:

prefix_length The number of minimal prefix characters that must match in order be a candidate suggestions. Defaults to 1. Increasing this number improves spellcheck performance. Usually misspellings don’t occur in the beginning of terms.

      "phrase": {
        "field": "title.suggest",
        "size": 5,
        "confidence": 1,
        "direct_generator": [
          {
            "field": "title.suggest",
            "prefix_length": 0. <<<<-------
          }
        ],
1 Like

Thank you so much