gpt4 book ai didi

elasticsearch - 如何为以下json创建Fluent DSL lambda表达式

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

我有json,其中我有多个用于汽车的模型和变体,现在我们需要根据对具有相关变体的汽车模型的请求来动态创建查询。我下面有json。

我试图创建查询,但不知道我将如何动态处理多个模型和变体请求。

var response = _esclient.EsClient().Search<ClassName>(a => a
.Index("Test")
.Type("Testa")
.Query(q => q.Bool(b =>
b.Must(m => m.Bool(p => p.Should(should =>
should.Bool(sb => sb.Must(m3 => m3.Term(t => t.Field(classname => classname.model).Value(modelname)) m3 => m3.Term(t => t.Field(classname => classname.model).Value(varientName)))))), should => should.Bool(sb => sb.Must(m1 => m1.Term(c => c.Field(classname => classname.variant).Value(varientname)), m1 => m1.Term(c => c.Field(classname => classname.model).Value(modelname))))
)))))

我已经为带有关联变量的两个模型静态创建了表达式。但我想针对任意数量的模型和关联的模型动态地使用它,因为我不知道会出现什么模型和关联的变体请求。
预期的Json适用于4个模型和相关的变体请求。可以根据要求增加或减少。
{  
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"variant": "lxi"
}
},
{
"term": {
"model": "ritz"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"variant": "lxi"
}
},
{
"term": {
"model": "alto"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"variant": "lxi"
}
},
{
"term": {
"model": "omni"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"variant": "vxi"
}
},
{
"term": {
"model": "alto 800"
}
}
]
}
}
]
}
}
]
}
}
}

最佳答案

这不是一个真正的Nest问题。坦白地说,这与C#lambda表达式的用法有关。我将在下面显示适用于任何数量汽车的代码。但是您必须努力自己理解lambda表达式:)

internal class Program
{
private class Car
{
public string Model { get; set; }
public string Variant { get; set; }
}

private static void Main(string[] args)
{
ElasticClient esClient = new ElasticClient(new Uri("http://localhost:9200"));

List<Car> cars = new List<Car>
{
new Car
{
Model = "ritz",
Variant = "lxi"
},
new Car
{
Model = "alto",
Variant = "lxi"
},
new Car
{
Model = "omni",
Variant = "lxi"
},
new Car
{
Model = "alto 800",
Variant = "vxi"
}
};

ISearchResponse<object> response = esClient.Search<object>(a => new SearchDescriptor<object>()
.Index("index")
.Type("type")
.Query(q => q
.Bool(b => b
.Should(cars.Select<Car, Func<QueryContainerDescriptor<object>, QueryContainer>>(car =>
d => new TermQuery { Field = "variant", Value = car.Variant }
&& new TermQuery { Field = "model", Value = car.Model })))));

}
}

这将生成一个Elasticsearch搜索请求主体,如下所示。
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"term": {
"variant": {
"value": "lxi"
}
}
},
{
"term": {
"model": {
"value": "ritz"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"variant": {
"value": "lxi"
}
}
},
{
"term": {
"model": {
"value": "alto"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"variant": {
"value": "lxi"
}
}
},
{
"term": {
"model": {
"value": "omni"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"variant": {
"value": "vxi"
}
}
},
{
"term": {
"model": {
"value": "alto 800"
}
}
}
]
}
}
]
}
}
}

关于elasticsearch - 如何为以下json创建Fluent DSL lambda表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57232632/

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