Elasticsearch-dsl-py: Using function score with queries

Created on 2 Mar 2017  路  3Comments  路  Source: elastic/elasticsearch-dsl-py

What is the best way to use FunctionScore with other queries? I ended up doing something like this:

f = {
    'gauss': {
        'location_point': {
            'origin': {
                'lat': lat,
                'lon': lng
            },
            'offset': '1mi',
            'scale': '5mi'
        }
    }
}
fs = FunctionScore(functions=[f])
fs_query = {
    'query': fs.to_dict()
}
fs_query['query']['function_score'].update(search.to_dict()) # Adding query info here
fs_search = search.from_dict(fs_query) # New search object from dict

Most helpful comment

is FunctionScore documented at all in this lib? What about using a script (ScriptScore)?

All 3 comments

There is a SF shortcut for ScoreFunction creation that might make it easier:

# instyantiate search
s = Search()
# create your query
s = s.query()....
# wrap the query in function score
s.query = FunctionScore(query=s.query, functions=[SF('gauss', location_point={'origin': ..., 'offset': ...})])

Hope this helps

That worked. Thanks.

is FunctionScore documented at all in this lib? What about using a script (ScriptScore)?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

beanaroo picture beanaroo  路  4Comments

SalahAdDin picture SalahAdDin  路  4Comments

rokcarl picture rokcarl  路  4Comments

njoannin picture njoannin  路  3Comments

abuzakaria picture abuzakaria  路  4Comments