Using script_score with Suggests

Hello,

I'm working on an autocomplete input similar to what twitter uses.
I need to have the matching users I follow at the top of the list (see yellow highlight on the screenshot)

image

For now I have a query like this :

{
  "suggest": {
    "suggestions": {
      "prefix": "dan",
      "completion": {
        "field": "suggest_users",
        "fuzzy": {
          "fuzziness": "AUTO",
          "prefix_length": 3
        }
      }
    }
  }
}

But I'd like to enhance it with a script_score like this (pseudo code):

{
    "script_score" : {
        "query" : { ??? },
        "script" : {
            "params": { 
                "following_user_ids": [...]
            },
            "source" : "... params.following_user_ids.contains(doc['user_id'].value) ..."
        }
    }
}

But each time I try to merge these 2 pieces of code I get an error.

Is it possible to use script_score with suggests ?

If not what would be a smart workaround ?

Thanks :slight_smile:

Hey,

have you seen the search as you type datatype? This allows you to write a regular query and may remove the need for the completion suggester (as it's also quite fast). The advantage of this is the ability to use arbitrary queries. For example you could have a boolean query, that consists of a must part of your regular search and then have a should clause to boost scoring, that contains the followers of the user executing the query. A terms lookup query might help you here.

Hope this helps as a start.

--Alex

1 Like

Yay thanks !
Looks pretty good, I'll try :slight_smile:

I keep this thread open in case I have related questions.

Have a great day.

Your question triggered me to write a small blog post about trying to emulate the behaviour that linkedin is doing with Elasticsearch. Maybe you find it useful. It's available at https://spinscale.de/posts/2020-05-29-implementing-a-linkedin-like-search-as-you-type-with-elasticsearch.html

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