aclowkay
aclowkay

Reputation: 3877

Suggest next word in phrase suggester

How can I suggest the next word in the phrase suggester? I tried a simple example from https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html but it doesn't seem to suggest

if I type in "red" I don't get anything, when I expect to get "red dog", the shingle filter does produce the bigram I expect.

Here is an example

PUT test
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "analysis": {
        "analyzer": {
          "trigram": {
            "type": "custom",
            "tokenizer": "standard",
            "filter": ["standard", "shingle"]
          }
        },
        "filter": {
          "shingle": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 3
          }
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "summary": {
          "type": "text",
          "fields": {
            "trigram": {
              "type": "text",
              "analyzer": "trigram"
            }
          }
        }
      }
    }
  }
}
POST test/test?refresh=true
{"summary": "Red dog"}
POST test/test?refresh=true
{"summary": "Blue word"}


GET test/_analyze
{
  "field":"summary.trigram",
  "text":"red dog"
}

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

Upvotes: 2

Views: 1886

Answers (1)

Nikolay Vasiliev
Nikolay Vasiliev

Reputation: 6066

Why doesn't phrase suggester work?

The reason why Phrase suggester doesn't suggest anything for string "red" is because it is spelled correctly, and phrase suggester is actually a spelling suggester, and not a completion suggester.

If you try to do "reddog" (without a whitespace) you will get the suggestion:

  "suggest": {
    "simple_phrase": [
      {
        "text": "reddog",
        "offset": 0,
        "length": 6,
        "options": [
          {
            "text": "red dog",
            "highlighted": "<em>red dog</em>",
            "score": 0.44063255
          }
        ]
      }
    ]
  }

If you add this document:

POST test/test?refresh=true
{"title": "reddish dog"}

And query a suggest for input "reddi" you will also get a spelling correction:

  "suggest": {
    "simple_phrase": [
      {
        "text": "reddi",
        "offset": 0,
        "length": 5,
        "options": [
          {
            "text": "reddish",
            "highlighted": "<em>reddish</em>",
            "score": 0.3440574
          }
        ]
      }
    ]
  }

How can I suggest next word in a phrase?

It seems that you are looking for a different type of suggest - a Completion Suggest.

If you try with this mapping:

PUT test
{
  "mappings": {
    "test": {
      "properties": {
        "summary": {
          "type": "text",
          "fields": {
            "suggest": {
              "type" : "completion"
            }
          }
        }
      }
    }
  }
}

And this query:

POST test/_search
{

  "suggest" : {
    "my-suggestion" : {
      "text" : "red",
      "completion" : {
        "field" : "summary.suggest"
      }
    }
  }
}

You will get what you wanted:

{
  // ...
  "suggest": {
    "my-suggestion": [
      {
        "text": "red",
        "offset": 0,
        "length": 3,
        "options": [
          {
            "text": "Red dog",
            "_index": "test",
            "_type": "test",
            "_id": "AWGanwweVn0PQ9k--kkE",
            "_score": 1,
            "_source": {
              "summary": "Red dog"
            }
          }
        ]
      }
    ]
  }
}

Hope that helps!

Upvotes: 2

Related Questions