Percolate with scoring using script_score


#1

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


#2

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
}

(system) #3