vamsi
vamsi

Reputation: 31

Elasticsearch, HOW to make phrase suggester return the exact suggestion?

I am using elasticsearch 5.5.2

I am trying phrase suggester and NOT able to configure it to return the exact suggestion that is in the index already. My index settings, type mappings and phrase suggest query are given below. Please help.

My index settings and type mappings are

PUT test
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "trigram_analyzer": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["shingle"]
          }
        },
        "filter": {
          "shingle": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 3
          }
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "title": {
          "type": "text",
          "fields": {
            "trigram": {
              "type": "text",
              "analyzer": "trigram_analyzer"
            }
          }
        }
      }
    }
  }
}

Indexed document using

POST test/test?refresh=true
{"title": "noble prize"}

The phrase suggester I am using

POST test/_search
{
  "suggest": {
    "text": "nobe priz",
    "simple_phrase": {
      "phrase": {
        "field": "title.trigram",
        "size": 1,
        "gram_size": 3,
        "direct_generator": [ {
          "field": "title.trigram",
          "suggest_mode": "always"
        } ],
        "highlight": {
          "pre_tag": "<em>",
          "post_tag": "</em>"
        }
      }
    }
  }
}

The result I am getting is

"suggest": {
    "simple_phrase": [
      {
        "text": "nobe priz",
        "offset": 0,
        "length": 9,
        "options": [
          {
            "text": "noble priz",
            "highlighted": "<em>noble</em> priz",
            "score": 0.09049256
          }
        ]
      }
    ]
  }

My question is, for the search text - 'nobe priz' - why I am NOT getting 'noble prize' as the suggestion. Instead why I am just getting 'noble priz'?

If we see, 'noble prize' is the document I have saved.

And if I increase the value of size to '2', then also I am NOT getting 'noble prize' as one of the suggestions.

With size as 2, for the search text 'nobe priz' I am getting the below response

"suggest": {
    "simple_phrase": [
      {
        "text": "nobe priz",
        "offset": 0,
        "length": 9,
        "options": [
          {
            "text": "noble priz",
            "highlighted": "<em>nobel</em> priz",
            "score": 0.09049256
          },
          {
            "text": "nobe prize",
            "highlighted": "nobe <em>prize</em>",
            "score": 0.09049256
          }
        ]
      }
    ]
  }

What should I do to get 'noble prize' as the suggestion? Please help.

Upvotes: 2

Views: 1624

Answers (1)

vamsi
vamsi

Reputation: 31

I found the answer myself. Need to tell ES how many terms in the search text are misspelled using the parameter 'max_errors'. 'max_errors' can be given as a percentage value in the form of float or an absolute number.

"click below for ES documentation on Phrase suggester with max_errors parameter" https://www.elastic.co/guide/en/elasticsearch/reference/master/search-suggesters-phrase.html

Accordingly I added 'max_errors' parameter value as 2 like below

POST test/_search
{
  "suggest": {
    "text": "nobe priz",
    "simple_phrase": {
      "phrase": {
        "field": "title.trigram",
        "size": 1,
        "gram_size": 3,
        "max_errors": 2,
        "direct_generator": [ {
          "field": "title.trigram",
          "suggest_mode": "always"
        } ],
        "highlight": {
          "pre_tag": "<em>",
          "post_tag": "</em>"
        }
      }
    }
  }
}

And I got the exact matching phrase suggestion as below

"suggest": {
    "simple_phrase": [
      {
        "text": "nobe priz",
        "offset": 0,
        "length": 9,
        "options": [
          {
            "text": "noble prize",
            "highlighted": "<em>noble prize</em>",
            "score": 0.4833575
          }
        ]
      }
    ]
  }

So with max_errors as 2, the suggestion 'noble prize' is getting returned.

Cheers :)

Upvotes: 1

Related Questions