TommyN
TommyN

Reputation: 2381

Is it possible to boost suggestions based on the Elasticsearch index

I have multiple indices that I want suggestions from, but I want to score/order the suggestions based on the index they're from. I've successfully boosted searches based on indices (using indices_boost), but this doesn't seem to work for suggestions. I tried something like:

GET index1,index2/_search
{
  "indices_boost" : [
        { "index1" : 9 },
        { "index2" : 1 }
    ],
  "suggest": {
    "mySuggest":{
      "text":"someText",
      "completion": {
        "field":"suggestField",
        "size":6
      }
    }
  }
}

Is this doable?

At the moment I've resorted to sorting the suggestions in code.

Upvotes: 1

Views: 570

Answers (1)

Nikolay Vasiliev
Nikolay Vasiliev

Reputation: 6066

I believe you can try to use category boost in context suggester to achieve the desired behavior. You need to attach a special category field to each suggestion document, which can be exactly the same as the index name.

How to use category context to boost suggestions

The mapping may look like this:

PUT food
{
    "mappings": {
        "properties" : {
            "suggestField" : {
                "type" : "completion",
                "contexts": [
                    { 
                        "name": "index_name",
                        "type": "category"
                    }
                ]
            }
        }
    }
}

For demonstration purposes I will create another index, exactly like the one above but with name movie. (Index names can be arbitrary.)

Let's add the suggest documents:

PUT food/_doc/1
{
    "suggestField": {
        "input": ["timmy's", "starbucks", "dunkin donuts"],
        "contexts": {
            "index_name": ["food"] 
        }
    }
}

PUT movie/_doc/2
{
    "suggestField": {
        "input": ["star wars"],
        "contexts": {
            "index_name": ["movie"] 
        }
    }
}

Now we can run a suggest query with our boosts set:

POST food,movie/_search
{
    "suggest": {
        "my_suggestion": {
            "prefix": "star",
            "completion": {
                "field": "suggestField",
                "size": 10,
                "contexts": {
                    "index_name": [
                        {
                            "context": "movie",
                            "boost": 9
                        },
                        {
                            "context": "food",
                            "boost": 1
                        }
                    ]
                }
            }
        }
    }
}

Which will return something like this:

{
  "suggest": {
    "my_suggestion": [
      {
        "text": "star",
        "offset": 0,
        "length": 4,
        "options": [
          {
            "text": "star wars",
            "_index": "movie",
            "_type": "_doc",
            "_id": "2",
            "_score": 9.0,
            "_source": ...,
            "contexts": {
              "index_name": [
                "movie"
              ]
            }
          },
          {
            "text": "starbucks",
            "_index": "food",
            "_type": "_doc",
            "_id": "1",
            "_score": 1.0,
            "_source": ...,
            "contexts": {
              "index_name": [
                "food"
              ]
...

Why didn't indices_boost work?

Seems like indices_boost parameter only affects the search, not suggest. _suggest used to be a standalone endpoint, but was deprecated, and probably this is the source of confusion.

Hope that helps!

Upvotes: 1

Related Questions