0

Is it possible to use index boost when using completion suggester in Elasticsearch? I have tried many different ways but doesn't seem to work. Haven't found any reference in the documentation claiming that it does not work for completion suggester. Example:

POST index1,index2/_search
{
  "suggest" : {
    "name_suggest" : {
      "text" : "my_query",
      "completion" : {
        "field" : "name_suggest",
        "size" : 7,
        "fuzzy" :{}
      }
    }
  },
  "indices_boost" : [
        { "index1" : 2 },
        { "index2" : 1.5 }
    ]
}

The above does not return boosted scores. The scores are the same compared to running it without the indices_boost parameter.

Anupam
  • 14,950
  • 19
  • 67
  • 94

1 Answers1

1

Tried few options but these didn't work directly, instead, you can define the weight of a document at index-time, and these could be used as a workaround to get the boosted document, below is the complete example.

Index mapping same for index1, index2

{
  "mappings": {
    "properties": {
      "suggest": {
        "type": "completion"
      },
      "title": {
        "type": "keyword"
      }
    }
  }
}

Index doc 1 with weight in index-1

{
    "suggest": {
        "input": [
            "Nevermind",
            "Nirvana"
        ],
        "weight": 30
    }
}

Similar doc is inserted in index-2 with diff weight

{
    "suggest": {
        "input": [
            "Nevermind",
            "Nirvana"
        ],
        "weight": 10 --> note less weight
    }
}

And the simple search will now sort it according to weight

{
    "suggest": {
        "song-suggest": {
            "prefix": "nir",
            "completion": {
                "field": "suggest"
            }
        }
    }
}

And search result

    {
                        "text": "Nirvana",
                        "_index": "index-1",
                        "_type": "_doc",
                        "_id": "1",
                        "_score": 34.0,
                        "_source": {
                            "suggest": {
                                "input": [
                                    "Nevermind",
                                    "Nirvana"
                                ],
                                "weight": 30
                            }
                        }
                    },
                    {
                        "text": "Nirvana",
                        "_index": "index-2",
                        "_type": "_doc",
                        "_id": "1",
                        "_score": 30.0,
                        "_source": {
                            "suggest": {
                                "input": [
                                    "Nevermind",
                                    "Nirvana"
                                ],
                                "weight": 10
                            }
                        }
                    }
                ]
Amit
  • 30,756
  • 6
  • 57
  • 88
  • but I am doing it query-time (had the posted the `search` request above) - am I missing anything – Anupam Oct 28 '20 at 11:34
  • @Anupam did you get a chance to go through the updated answer? – Amit Oct 29 '20 at 05:11
  • Just saw the updated answer. Thanks for trying a workaround. This seems like a good workaround. So the `score` will be boosted based on the weight value? I want to show the results from a particular index first (in a search-as-you-type dropdown). My completion suggester returns 7 results and sometimes the result I need is not even in the 7 because of lower score (otherwise I could have reordered them after receiving back the results) – Anupam Oct 29 '20 at 08:13
  • `index boost` works with `match` queries but not `completion suggester`, it seems. But I want to avoid using regular search instead of `completion suggester` because as I understand `completion suggester` is designed for faster lookups (needed for search-as-you-type) – Anupam Oct 29 '20 at 08:17
  • @Anupam you are right, but i looked at Elasticsearch source code and it seems still they have not added the support of it, also its mentioned on their website that this feature is still in development, which might be the reason for missing this feature. – Amit Oct 29 '20 at 14:40
  • @Anupam i am not sure about your complete use-case, maybe you should ask a different question explaining it in more detail, that way community can even suggest you better approach, for your information I hv written a detailed blog post on autocomplete https://opster.com/elasticsearch-glossary/elasticsearch-autocomplete-troubleshooting-guide/ – Amit Oct 29 '20 at 14:42
  • @Anupam apart from above blog, my this answer https://stackoverflow.com/a/60584211/4039431 explains the various trade-off of different approach, pls go through them, I am sure, it would be useful for you and lmk if you have followup question – Amit Oct 29 '20 at 14:43
  • Thanks @ElasticsearchNinja. I implemented the weights and it worked - boosted the score. Accepted this answer also since it seems to be the best workaround. For others' reference, [here](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-suggesters-completion.html#indexing) is the weights documentation for Completion Suggester – Anupam Nov 02 '20 at 10:46