使用nest 7在函数得分查询中使用ElasticSearch过滤器

sulc1iza  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(0)|浏览(154)

在我的场景中,我必须先展示库存商品,鼓励一些品牌,也鼓励一些品牌。
我正在尝试使用函数score query来实现它https://www.elastic.co/guide/en/elasticsearch/client/net-api/5.x/function-score-query-usage.html
我尝试了文档代码,它返回了有效的结果。库存商品排在第一位,不鼓励品牌进入预期的最后一页。但我的问题是品牌不是第一位的。
这是我的一些样本品牌的代码,作为我的例子,库存商品应该排在第一位,橙色品牌应该排在第一位。

var query = new FunctionScoreQuery()
            {
                Name = "product_query",
                Boost = 5,
                Query = new MatchAllQuery { },        
                Functions = new List<IScoreFunction>
                            {
                                //new RandomScoreFunction
                                //{
                                //    Filter = new MatchQuery { Field = "ManufacturerName", Query = "Apple" },
                                //    Weight = 35
                                //},  
                                //new RandomScoreFunction
                                //{
                                //    Filter = new MatchQuery { Field = "ManufacturerName", Query = "Orange" },
                                //    Weight = 52
                                //},
                                new WeightFunction
                                {
                                    Filter = new MatchQuery { Field = "ManufacturerName", Query = "Apple" },
                                    Weight = 25
                                },
                                new WeightFunction
                                {
                                    Filter = new MatchQuery { Field = "ManufacturerName", Query = "Orange" },
                                    Weight = 42
                                },
                                new WeightFunction 
                                {
                                    Filter = new MatchQuery{ Field = "StockAvailability", Query = "true"},
                                    Weight = 42
                                }
                           },
                MaxBoost = 42,
                ScoreMode = FunctionScoreMode.Max,
                BoostMode = FunctionBoostMode.Multiply,
                MinScore = 42
            };

这是我的问题

GET dev-products/_search?
{
  "from": 0,
  "profile": true,
  "query": {
    "function_score": {
      "boost_mode": "multiply",
      "functions": [
        {
          "filter": {
            "match": {
              "manufacturerName": {
                "query": "Apple"
              }
            }
          },
          "weight": 25
        },
        {
          "filter": {
            "match": {
              "manufacturerName": {
                "query": "Orange"
              }
            }
          },
          "weight": 42
        },
        {
          "filter": {
            "match": {
              "stockAvailability": {
                "query": "true"
              }
            }
          },
          "weight": 42
        }
      ],
      "max_boost": 42,
      "min_score": 42,
      "query": {
        "bool": {
          "filter": [
            {
              "bool": {
                "must": [
                  {
                    "range": {
                      "price_AU": {
                        "gte": 0
                      }
                    }
                  },
                  {
                    "range": {
                      "price_AU": {
                        "lte": 100000000
                      }
                    }
                  }
                ]
              }
            }
          ],
          "must": [
            {
              "match_all": {}
            }
          ]
        }
      },
      "score_mode": "max",
      "boost": 5,
      "_name": "product_query"
    }
  },
  "size": 9,
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "version": true
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题