gpt4 book ai didi

c# - 利用NEST for ES FunctionScore具有可变数量的功能

转载 作者:行者123 更新时间:2023-12-02 23:40:25 24 4
gpt4 key购买 nike

利用NEST的0.12版本,我需要通过功能评分API来建立支持,以便在我的C#应用程序中具有可变数量的功能参数,例如下面定义的。基于API的当前形状,我无法找到有条件地添加函数或将函数项的原始数组传递给查询描述符对象的方法。在0.12中是否有其他方法可以实现这一目标?

bool useFunctionScoreForCreatedDate = true;
bool useFunctionScoreForAge = true;

var s = new SearchDescriptor<ExampleDataObject>().From(0).Size(10)
.Query(aa => aa
.FunctionScore(fs => fs
.Query(qq => qq.MatchAll())
.Functions(
// need to conditionally add this function score to the FunctionScoreFunction[] if useFunctionScoreForCreatedDate
f => f.Linear(x => x.createddate, d => d.Scale("0d")),
// need to conditionally add this function score to the FunctionScoreFunction[] if useFunctionScoreForAge
f => f.Exp(x => x.age, d => d.Scale("0.5")),
f => f.BoostFactor(2)
)
.ScoreMode(FunctionScoreMode.sum)
)
).Fields(x => x.title);

最佳答案

看起来.Functions().FunctionScore()方法接受了FunctionScoreFunctionsDescriptor<>对象。因此,您应该可以使用类似于以下内容的内容...

 bool useFunctionScoreForCreatedDate = true;
bool useFunctionScoreForAge = true;

var fsFunctionsDescriptor = new FunctionScoreFunctionsDescriptor<ExampleDataObject();
if (useFunctionsScoreForCreatedDate)
{
fsFunctionsDescriptor.Linear(x => x.createdate, d => d.Scale("0d"));
}

if (useFunctionScoreForAge)
{
fsFunctionsDescriptor.Exp( x => x.age, d => d.Scale("0.5"));
}
fsFunctionsDescriptor.BoostFactor(2);

var s = new SearchDescriptor<ExampleDataObject>().From(0).Size(10)
.Query(aa => aa
.FunctionScore(fs => fs
.Query(qq => qq.MatchAll())
.Functions(fsFunctionsDescriptor)
.ScoreMode(FunctionScoreMode.sum)
)
).Fields(x => x.title);

关于c# - 利用NEST for ES FunctionScore具有可变数量的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22183598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com