How to create Fluent DSL lambda expression for below json

橙三吉。 提交于 2019-12-11 07:46:38

问题


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))))
                                                               )))))

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

回答1:


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 })))));

    }
}

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


来源:https://stackoverflow.com/questions/57232632/how-to-create-fluent-dsl-lambda-expression-for-below-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!