Antony Francis
Antony Francis

Reputation: 304

What are the equivalent types for FunctionScoreFunctionsDescriptor & FunctionScoreFunction in 2.3.3

Upgrading from NEST 1.6.2 to 2.3.3.

What are the new types for FunctionScoreFunctionsDescriptor and FunctionScoreFunction in NEST 2.3.3?

Are they FunctionScoreFunctionsDescriptor -> ScoreFunctionsDescriptor and FunctionScoreFunction -> ScoreFunctionsDescriptor?

If that's the case, we build a var functionScores = new List<Func<ScoreFunctionsDescriptor<IndexData>, ScoreFunctionsDescriptor<Property>>>(), but how do we pass this down to

var searchDescriptor = new SearchDescriptor<IndexData>() .Paged(pageable) .Query(q => q .FunctionScore(fs => fs.Functions(***How do we pass the functionScores***));

Can we build it like below?

functionScores.ForEach(f => searchDescriptor.Query(q => q.FunctionScore(fc => fc.Functions(f))));

Upvotes: 0

Views: 258

Answers (1)

Russ Cam
Russ Cam

Reputation: 125528

The function_score query can take either an IEnumerable<IScoreFunction> or a Func<ScoreFunctionsDescriptor<T>, IPromise<IList<IScoreFunction>>> i.e. a function that takes a score descriptor and returns a list of functions. A ScoreFunctionsDescriptor<T> implements IPromise<IList<IScoreFunction>>

Based on this, if we want to aggregate a bunch of functions together, we can aggregate over a bunch of functions that take a ScoreFunctionsDescriptor<T> and return a ScoreFunctionsDescriptor<T>

public class Document
{
    public string Name { get; set;}
    public GeoLocation Location { get; set;}
}

var functions = new List<Func<ScoreFunctionsDescriptor<Document>, ScoreFunctionsDescriptor<Document>>>
{
    s => s.FieldValueFactor(fvf => fvf
        .Field(f => f.Name).Weight(3)),
    s => s.ExponentialGeoLocation(geo => geo
        .Field(f => f.Location)
        .Offset("1km")
        .Origin(new GeoLocation(-33.87189, 151.21623))
        .Scale("2km")
    )
};

client.Search<Document>(s => s
    .Query(q => q
        .FunctionScore(fs => fs
            .Functions(sc => functions.Aggregate(sc, (a,f) => f(a)))
        )
    )
);

which in this example yields

{
  "query": {
    "function_score": {
      "functions": [
        {
          "field_value_factor": {
            "field": "name"
          },
          "weight": 3.0
        },
        {
          "exp": {
            "location": {
              "origin": {
                "lat": -33.87189,
                "lon": 151.21623
              },
              "scale": "2.0km",
              "offset": "1.0km"
            }
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions