elasticsearch nest support of filters in functions

2020-07-17 08:05发布

问题:

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!

回答1:

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))));


回答2:

The Udi's answer didn't work for me. It seems that in new version (v 2.3, C#) there's no Filter() method on ScoreFunctionsDescriptor class.

But I found a solution. You can provide an array of IScoreFunction. To do that you can use new FunctionScoreFunction() or use my helper class:

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;
    }
}

With this class, filter can be applied this way (this is just an example):

        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),
                        }
                    )
                )
            );