# Using script\_score with Suggests

**URL:** <https://discuss.elastic.co/t/using-script-score-with-suggests/234209>\
**Category:** Elasticsearch\
**Created:** [May 25, 2020, 8:26pm UTC](https://discuss.elastic.co/t/using-script-score-with-suggests/234209 "2020-05-25T20:26:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chapelle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chapelle/32/45812_2.png) [@chapelle](https://discuss.elastic.co/u/chapelle)\
**Post date:** [May 25, 2020, 8:26pm UTC](https://discuss.elastic.co/t/using-script-score-with-suggests/234209/1 "2020-05-25T20:26:54Z")

</div>

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](https://us1.discourse-cdn.com/elastic/original/3X/8/b/8b0210d8ee041e08d23069fc19af829bcb1524cb.png)

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 🙂

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 26, 2020, 11:04am UTC](https://discuss.elastic.co/t/using-script-score-with-suggests/234209/2 "2020-05-26T11:04:16Z")

</div>

Hey,

have you seen the [search as you type datatype](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-as-you-type.html)? 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

---

<div class="post-metadata">

**Author:** ![chapelle](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chapelle/32/45812_2.png) [@chapelle](https://discuss.elastic.co/u/chapelle)\
**Post date:** [May 26, 2020, 12:22pm UTC](https://discuss.elastic.co/t/using-script-score-with-suggests/234209/3 "2020-05-26T12:22:14Z")

</div>

Yay thanks !  
Looks pretty good, I'll try 🙂

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

Have a great day.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [May 29, 2020, 12:01pm UTC](https://discuss.elastic.co/t/using-script-score-with-suggests/234209/4 "2020-05-29T12:01:12Z")

</div>

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](https://spinscale.de/posts/2020-05-29-implementing-a-linkedin-like-search-as-you-type-with-elasticsearch.html)

---

<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:** [June 26, 2020, 12:01pm UTC](https://discuss.elastic.co/t/using-script-score-with-suggests/234209/5 "2020-06-26T12:01:14Z")

</div>

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