如何为下面的json创建Fluent DSL lambda表达式 [英] How to create Fluent DSL lambda expression for below json

查看:95
本文介绍了如何为下面的json创建Fluent DSL lambda表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have json where i have multiple model and variant for car and now we need to dynamically create the query as per request for car model with associated variant. I have json below.

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

I tried to create the query but don't know how i will handle dynamically multiple model and variant request.

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个模型和相关的变体请求.可以根据要求增加或减少.

I have created the expression statically for two model with associated variant. but i want it dynamically for any number of model and associated model, because i have no idea what model and associated variant request will come. Expected Json for 4 model and associated variant request. It can be increase or decrease as per request.

{  
  "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表达式:)

This is not really a Nest question. It is about the usage of C# lambda expressions which frankly can get confusing sometimes. I'll show the code below that will work for any number of cars. But you have to make the effort of understanding the lambda expressions on your own :)

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搜索请求主体,如下所示.

This will generate an Elasticsearch search request body as under which is what you want.

{
    "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"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

这篇关于如何为下面的json创建Fluent DSL lambda表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆