【问题标题】:How create the filters in Function Score Query with .NET NEST Client如何使用 .NET NEST 客户端在函数评分查询中创建过滤器
【发布时间】:2018-12-04 09:08:53
【问题描述】:

In Elasticsearch Document描述函数分数查询显示代码如下

GET /_search
{
    "query": {
        "function_score": {
          "query": { "match_all": {} },
          "boost": "5", 
          "functions": [
              {
                  "filter": { "match": { "test": "bar" } },
                  "random_score": {}, 
                  "weight": 23
              },
              {
                  "filter": { "match": { "test": "cat" } },
                  "weight": 42
              }
          ],
          "max_boost": 42,
          "score_mode": "max",
          "boost_mode": "multiply",
          "min_score" : 42
        }
    }
}

我将此查询写给object initializer syntax

var searchRequest = new SearchRequest<ProductType>
{
   Query = new FunctionScoreQuery()
   {
      Query = new MatchAllQuery {},
      Boost = 5,
      Functions = new List<IScoreFunction>
      {
         Filters...?
      },
      MaxBoost = 42,
      ScoreMode = FunctionScoreMode.Max,
      BoostMode = FunctionBoostMode.Max,
      MinScore = 42
   }
};

如何在函数中构建过滤器?

IScoreFunction 接口只允许ExponentialDecayFunctionGaussDateDecayFunctionLinearGeoDecayFunctionFieldValueFactorFunctionRandomScoreFunctionWeightFunctionScriptScoreFunction

【问题讨论】:

    标签: c# .net elasticsearch nest elasticsearch.net


    【解决方案1】:

    函数是IScoreFunction 的集合。在示例 JSON 中,第一个函数是随机得分函数,第二个函数是权重函数。链接的 Query DSL 示例有不同功能的示例,这里有一个匹配上面 JSON 的示例

    var client = new ElasticClient();
    
    var searchRequest = new SearchRequest<ProductType>
    {
        Query = new FunctionScoreQuery()
        {
            Query = new MatchAllQuery { },
            Boost = 5,
            Functions = new List<IScoreFunction>
            {
                new RandomScoreFunction
                {
                    Filter = new MatchQuery
                    {
                        Field = "test",
                        Query = "bar"
                    },
                    Weight = 23
                },
                new WeightFunction
                {
                    Filter = new MatchQuery
                    {
                        Field = "test",
                        Query = "cat"
                    },
                    Weight = 42
                }
            },
            MaxBoost = 42,
            ScoreMode = FunctionScoreMode.Max,
            BoostMode = FunctionBoostMode.Multiply,
            MinScore = 42
        }
    };
    
    var searchResponse = client.Search<ProductType>(searchRequest);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多