# Percolate with scoring using script\_score

**URL:** <https://discuss.elastic.co/t/percolate-with-scoring-using-script-score/24587>\
**Category:** Elasticsearch\
**Created:** [June 29, 2015, 11:36pm UTC](https://discuss.elastic.co/t/percolate-with-scoring-using-script-score/24587 "2015-06-29T23:36:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoellner](https://avatars.discourse-cdn.com/v4/letter/z/e8c25b/32.png) [@zoellner](https://discuss.elastic.co/u/zoellner)\
**Post date:** [June 29, 2015, 11:36pm UTC](https://discuss.elastic.co/t/percolate-with-scoring-using-script-score/24587/1 "2015-06-29T23:36:11Z")

</div>

Hi,

does anyone have a working example for using percolate with scoring? It seems that the ones in the docs/github aren't working anymore due to dynamic scripts being disabled by default now.

I am storing queries like this under the .percolator type:

```
{
  _id: 1,
  query: {
    multi_match: {
      query: "some query",
      operator: 'and',
      fields: ['title.shingles^2', 'body.shingles'],
      tie_breaker: 0.2
    }
  },
  popularity: 4,
  analyzer: 'analyzer_shingle_for_tags'
}

```

I want to percolate a document of this format:

```
{
  title: "some string",
  body: "some other string"
}

```

but I want the returned matched queries to be scored and sorted according the the popularity.  
The request I am using currently is this:

```
{
  doc: {... a document like the one above ... }
  query: {
    function_score: {
      query: { match_all: {}},
      functions: [
        {
          script_score: {
            script: 'get_popularity'
          }
        }
      ]
    }
  },
  sort: true,
  size: 10,
  track_scores: true
}

```

where get\_popularity.groovy is in my scripts folder and has this content:

```
return doc['popularity'].value

```

But I get an error:

> org.elasticsearch.ElasticsearchIllegalArgumentException: malformed sort format, either start with array, object, or an actual string

---

<div class="post-metadata">

**Author:** ![zoellner](https://avatars.discourse-cdn.com/v4/letter/z/e8c25b/32.png) [@zoellner](https://discuss.elastic.co/u/zoellner)\
**Post date:** [June 30, 2015, 5:59am UTC](https://discuss.elastic.co/t/percolate-with-scoring-using-script-score/24587/2 "2015-06-30T05:59:50Z")

</div>

Eventually figured out that the error message did make sense. The sort field in the percolate request needs to be an array or string, so sorting by the "\_score" field would work like this:

```
{
  doc: {... a document like the one above ... }
  query: {
    function_score: {
      query: { match_all: {}},
      functions: [
        {
          script_score: {
            script: 'get_popularity'
          }
        }
      ]
    }
  },
  sort: "_score",
  size: 10,
  track_scores: true
}
```

---

<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:** [July 6, 2017, 12:04am UTC](https://discuss.elastic.co/t/percolate-with-scoring-using-script-score/24587/3 "2017-07-06T00:04:37Z")

</div>


