Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch nest support of filters in functionscore function

I am currently trying to implement a "function_score" query in NEST, with functions that are only applied when a filter matches.

It doesn't look like FunctionScoreFunctionsDescriptor supports adding a filter yet. Is this functionality going to be added any time soon?



Here's a super basic example of what I'd like to be able to implement:

  1. Runs an ES query, with basic scores
  2. Goes through a list of functions, and adds to it the first score where the filter matches
"function_score": {
    "query": {...},  // base ES query
    "functions": [
        {
            "filter": {...},
            "script_score": {"script": "25"}
        },
        {
            "filter": {...},
            "script_score": {"script": "15"}
        }      
    ],
    "score_mode": "first",  // take the first script_score where the filter matches
    "boost_mode": "sum"  // and add this to the base ES query score
}

I am currently using Elasticsearch v1.1.0, and NEST v1.0.0-beta1 prerelease.

Thanks!

like image 615
IBN Avatar asked Jan 10 '23 15:01

IBN


1 Answers

It's already implemented:

_client.Search<ElasticsearchProject>(s => 
            s.Query(q=>q
                .FunctionScore(fs=>fs.Functions(
                    f=>f
                        .ScriptScore(ss=>ss.Script("25"))
                        .Filter(ff=>ff.Term(t=>t.Country, "A")),
                    f=> f
                        .ScriptScore(ss=>ss.Script("15"))
                        .Filter(ff=>ff.Term("a","b")))
                .ScoreMode(FunctionScoreMode.first)
                .BoostMode(FunctionBoostMode.sum))));
like image 90
Udi Avatar answered May 15 '23 22:05

Udi