Elasticsearch filter support socket in functioncore function

I'm currently trying to implement a "function_score" request in NEST with functions that only apply when the filter matches.

It doesn't seem like FunctionScoreFunctionsDescriptor supports adding a filter. Will this feature be added soon?



Here is a super basic example of what I would like to implement:

  • Runs an ES query with basic grades
  • Enumerates the list of functions and adds the first score to it, 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 preerelease.

Thank!

+4
source share
2 answers

It has already been 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))));
+4
source

Udi . , (v 2.3, #) Filter() ScoreFunctionsDescriptor.

. IScoreFunction. new FunctionScoreFunction() :

class CustomFunctionScore<T> : FunctionScoreFunction
    where T: class
{
    public CustomFunctionScore(Func<QueryContainerDescriptor<T>, QueryContainer> selector, double? weight = null)
    {
        this.Filter = selector.Invoke(new QueryContainerDescriptor<T>());
        this.Weight = weight;
    }
}

( ):

        SearchDescriptor<BlobPost> searchDescriptor = new SearchDescriptor<BlobPost>()
            .Query(qr => qr
                .FunctionScore(fs => fs
                    .Query(q => q.Bool(b => b.Should(s => s.Match(a => a.Field(f => f.FirstName).Query("john")))))
                    .ScoreMode(FunctionScoreMode.Max)
                    .BoostMode(FunctionBoostMode.Sum)
                    .Functions(
                        new[] 
                        {
                            new CustomFunctionScore<BlobPost>(q => q.Match(a => a.Field(f => f.Id).Query("my_id")), 10),
                            new CustomFunctionScore<BlobPost>(q => q.Match(a => a.Field(f => f.FirstName).Query("john")), 10),
                        }
                    )
                )
            );
+3

Source: https://habr.com/ru/post/1539362/