作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有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))))
)))))
{
"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 })))));
}
}
{
"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/
我是一名优秀的程序员,十分优秀!