FunctionScoreFunctionsDescriptor&的等价类型是什么? 2.3.3

时间:2016-07-02 09:56:33

标签: elasticsearch nest elasticsearch-net

从NEST 1.6.2升级到2.3.3。

NEST 2.3.3中的FunctionScoreFunctionsDescriptorFunctionScoreFunction有哪些新类型?

他们是FunctionScoreFunctionsDescriptor - > ScoreFunctionsDescriptorFunctionScoreFunction - > ScoreFunctionsDescriptor

如果是这样,我们会构建一个var functionScores = new List<Func<ScoreFunctionsDescriptor<IndexData>, ScoreFunctionsDescriptor<Property>>>(),但我们如何将其传递给

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

我们可以像下面那样构建它吗?

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

1 个答案:

答案 0 :(得分:1)

function_score查询可以采用IEnumerable<IScoreFunction>Func<ScoreFunctionsDescriptor<T>, IPromise<IList<IScoreFunction>>>,即获取分数描述符并返回函数列表的函数。 ScoreFunctionsDescriptor<T>实施IPromise<IList<IScoreFunction>>

基于此,如果我们想要将一堆函数聚合在一起,我们可以汇总一堆函数,这些函数需要ScoreFunctionsDescriptor<T>并返回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)))
        )
    )
);

在这个例子中产生

{
  "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"
            }
          }
        }
      ]
    }
  }
}