![](/img/trans.png)
[英]adding filters into the search query in elasticsearch using NEST client
[英]How create the filters in Function Score Query with .NET NEST Client
在 Elasticsearch 文檔中描述函數分數查詢顯示代碼如下
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
}
}
}
我將此查詢寫入對象初始值設定項語法
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
接口只允許ExponentialDecayFunction
、 GaussDateDecayFunction
、 LinearGeoDecayFunction
、 FieldValueFactorFunction
、 RandomScoreFunction
、 WeightFunction
、 ScriptScoreFunction
Functions 是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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.