How to get all the suggestions in completion query

so here is my mapping

"skills_completion": {
      "type": "completion",
      "contexts": [
        {
          "name": "api_key",
          "type": "category"
        }
      ]
      },

and below are my inserted data in skills_completion

{ "input": [ "deal", "don", "drag", "resource", "question", "player", "training" ], "contexts": { "api_key": "4" } }

{ "input": [ "deal", "dog", "group", "resource", "question", "player", "training" ], "contexts": { "api_key": "4" } }

thing is that when I query it like this

POST /cvs/_search
{
  "_source": false,
  "suggest": {
    "name-suggest": {
      "prefix": "d",  // Change this prefix as needed
      "completion": {
        "field": "skills_completion",
        "skip_duplicates": true,
        "contexts": {
          "api_key": [
            {
              "context": "4"
            }
          ]
        }
      }
    }
  }
}

thing is that In response I get only deal and not deal, dog, don, drag
however when I query with prefix = "do" then I do get dog,don

what is the issue here is it due to an array that I am providing for completion or something else

Hi!

I was able to replicate your results and there are a few things to note.
In the current state you are searching for which documents have a match to your prefix query - and the result is the first match found per document.

Since you have skip_duplicates to true in the search with d you are getting deal back only once (if you turn duplicates to false both documents will be returned due to the deal match).
In the case of searching for do you get back the first result in each document (dog and don).

This is the expected behavior since your input is formated as two documents and your query is meant to return the document indexes that match the prefix.

If you want to get all possible terms, you can add each term as its own document instead. So for example if I input as:

PUT test_complete/_doc/2?refresh
{
  "skills_completion" : 
   { "input": [ "deal"], "contexts": { "api_key": "4" } }

}

PUT test_complete/_doc/3?refresh
{
  "skills_completion" : 
   { "input": [ "dog"], "contexts": { "api_key": "4" } }

}

PUT test_complete/_doc/4?refresh
{
  "skills_completion" : 
   { "input": [ "don"], "contexts": { "api_key": "4" } }

}

When you query with the d prefix you now get all three documents back.

Hope this helps!
Iulia

1 Like