# Phrase suggester doesn't provide the correct suggestions

**URL:** <https://discuss.elastic.co/t/phrase-suggester-doesnt-provide-the-correct-suggestions/248428>\
**Category:** Elasticsearch\
**Created:** [September 13, 2020, 3:18am UTC](https://discuss.elastic.co/t/phrase-suggester-doesnt-provide-the-correct-suggestions/248428 "2020-09-13T03:18:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![smuthuka](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@smuthuka](https://discuss.elastic.co/u/smuthuka)\
**Post date:** [September 13, 2020, 3:18am UTC](https://discuss.elastic.co/t/phrase-suggester-doesnt-provide-the-correct-suggestions/248428/1 "2020-09-13T03:18:47Z")

</div>

Phrase suggester doesn't provide the correct suggestions. This usually happens after adding/deleting documents.

Index Mapping

```auto
PUT sugg_test
{
  "settings": {
    "index": {
      "number_of_shards": "1",
      "number_of_replicas": "1",
      "analysis": {
        "analyzer": {
          "trigram": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "lowercase",
              "shingle"
            ]
          },
          "reverse": {
            "type": "custom",
            "tokenizer": "whitespace",
            "filter": [
              "lowercase",
              "reverse"
            ]
          }
        },
        "normalizer": {
          "lowercase_normalizer": {
            "type": "custom",
            "filter": [
              "lowercase"
            ]
          }
        },
        "filter": {
          "shingle": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 3
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "normalizer": "lowercase_normalizer"
          },
          "trigram": {
            "type": "text",
            "analyzer": "trigram"
          },
          "reverse": {
            "type": "text",
            "analyzer": "reverse"
          }
        }
      },
      "suggest": {
        "type": "completion"
      }
    }
  }
}

```

Suggest query

```auto
GET sugg_test/_search
{
  "suggest": {
    "text": "SEARCH TERM",
    "name_phrase": {
      "phrase": {
        "field": "name.trigram",
        "size": 3,
        "confidence": 1,
        "direct_generator": [
          {
            "field": "name.trigram",
            "suggest_mode": "always"
          },
          {
            "field": "name.reverse",
            "suggest_mode": "always",
            "pre_filter": "reverse",
            "post_filter": "reverse"
          }
        ],
        "collate": {
          "query": {
            "source": {
              "match_phrase": {
                "name": "{{suggestion}}"
              }
            }
          },
          "prune": false
        }
      }
    }
  }
}

```

I add a document for `"elk stack"` and I am able to get that back as a suggestion for `"elk stacks"`

```auto
PUT sugg_test/_doc/1
{
  "name": "elk stack",
  "suggest": [
    {
      "input": [
        "elk stack"
      ],
      "weight": 2
    }
  ]
}

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "suggest" : {
    "name_phrase" : [
      {
        "text" : "elk stacks",
        "offset" : 0,
        "length" : 10,
        "options" : [
          {
            "text" : "elk stack",
            "score" : 0.5508418
          }
        ]
      }
    ]
  }
}

```

Reverse works as well. I delete and recreate the index and add a document for `"elk stacks"` and I am able to get that back as a suggestion for `"elk stack"`

```auto
PUT sugg_test/_doc/1
{
  "name": "elk stacks",
  "suggest": [
    {
      "input": [
        "elk stacks"
      ],
      "weight": 2
    }
  ]
}

{
  "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" : {
    "name_phrase" : [
      {
        "text" : "elk stack",
        "offset" : 0,
        "length" : 9,
        "options" : [
          {
            "text" : "elk stacks",
            "score" : 0.5508418
          }
        ]
      }
    ]
  }
}

```

This all works good. Now I delete and recreate the index.

1. Add `"elk stack"` (this gets correctly suggested for `"elk stacks"`)
2. Remove `"elk stack"` (with a \_delete\_by\_query)
3. Add `"elk stacks"`

```auto
PUT sugg_test/_doc/1
{
  "name": "elk stacks",
  "suggest": [
    {
      "input": [
        "elk stacks"
      ],
      "weight": 2
    }
  ]
}

```

1. Query for `"elk stack"` and that doesn't return anything

```auto
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : []
  },
  "suggest" : {
    "name_phrase" : [
      {
        "text" : "elk stack",
        "offset" : 0,
        "length" : 9,
        "options" : []
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 11, 2020, 3:18am UTC](https://discuss.elastic.co/t/phrase-suggester-doesnt-provide-the-correct-suggestions/248428/2 "2020-10-11T03:18:51Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
