Searchkick: How to use 'weight' with Function Score Query?

Created on 6 Jul 2016  路  4Comments  路  Source: ankane/searchkick

I need to control the boost applied by boost_by_distance and there is weight multiplier: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-weight . Unfortunately I can't use it like this:

ModelName.search '*', order: { _score: :desc }, boost_by_distance: { field: :location, origin: { lat: 40, lon: -74 }, function: :gauss, weight: 4, scale: "30km", decay: 0.97 }

Is there any other way to accomplish that?

It could be done by adding a few lines to this method: https://github.com/tomajask/searchkick/blob/master/lib/searchkick/query.rb#L422-435

filter = {
  boost_by_distance[:function] => {
    boost_by_distance[:field] => function_params
  }
}
filter.merge!(weight: boost_by_distance[:weight]) if boost_by_distance[:weight]
custom_filters << filter

Most helpful comment

@pplant added a factor to set the weight in #1028 馃帀

All 4 comments

Hey Tomasz, from the docs, it looks like weight is a function similar to gauss rather than an option for it (but I may be misunderstanding).

Thank you, for quick answer!

Here is some example (w/o weight):

Job.search '*', order: { _score: :desc }, boost_by_distance: { field: :location, origin: {lat: 40, lon: -74}, function: :gauss, scale: "30km", decay: 0.97 }

generates:

curl http://localhost:9200/jobs_development/_search?pretty -d '{"query":{"function_score":{"functions":[{"gauss":{"location":{"scale":"30km","origin":{"lat":40,"lon":-74},"decay":0.97}}}],"query":{"match_all":{}},"score_mode":"sum"}},"size":1000,"from":0,"sort":{"_score":"desc"},"fields":[]}'

and returns these scores:

 0.8226902,
 0.8226902,
 0.8226902,
 0.8226902,
 0.8085689,
 0.79994756,
 0.7917186,
 0.7917186,
 0.7917186,
 0.71576816,

With the adjustment I wrote earlier (w/ weight):

Job.search '*', order: { _score: :desc }, boost_by_distance: { field: :location, origin: {lat: 40, lon: -74}, weight: 4, function: :gauss, scale: "30km", decay: 0.97 }

generates:

curl http://localhost:9200/jobs_development/_search?pretty -d '{"query":{"function_score":{"functions":[{"gauss":{"location":{"scale":"30km","origin":{"lat":40,"lon":-74},"decay":0.97}},"weight":4}],"query":{"match_all":{}},"score_mode":"sum"}},"size":1000,"from":0,"sort":{"_score":"desc"},"fields":[]}'

and there is a weight parameter inside on the same level as gauss:

{"gauss":{"location":{"scale":"30km","origin":{"lat":40,"lon":-74},"decay":0.97}},"weight":4}

and return these scores:

 3.2907608,
 3.2907608,
 3.2907608,
 3.2907608,
 3.2342756,
 3.1997902,
 3.1668744,
 3.1668744,
 3.1668744,
 2.8630726,

weight parameter multiplies whatever is returned by the function (gauss here), I think.

Hey @tomajask how do you feel about this issue at this point. Do you believe your question has been fully answered?

@pplant added a factor to set the weight in #1028 馃帀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rajatgarg79 picture rajatgarg79  路  4Comments

yurgon picture yurgon  路  4Comments

mehulkar picture mehulkar  路  5Comments

cubaraphael picture cubaraphael  路  3Comments

namila picture namila  路  4Comments