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
接口(interface)只允许ExponentialDecayFunction
, GaussDateDecayFunction
, LinearGeoDecayFunction
, FieldValueFactorFunction
, RandomScoreFunction
, WeightFunction
, ScriptScoreFunction
最佳答案
函数是 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);
关于c# - 如何使用 .NET NEST 客户端在函数评分查询中创建过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53609236/