我有一个带有嵌套对象数组的简单对象
public class Product {
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; }
public ProductTag[] Tags {get; set;} = new ProductTag[0];
}
public class ProductTag {
public string TagName {get; set;}
public string Color {get; set;} = "orange";
}
使用
Tags
为mapped as a nested datatype的位置
client.CreateIndex(Indices.Index<Product>(), d =>
d.Mappings(m =>
m.Map<Product>(mm => mm
.AutoMap()
.Properties(pd => pd
.Text(tpd => tpd.Name(x => x.Name))
.Nested<ProductTag>(npd => npd
.Name(x => x.Tags)
.AutoMap()
.Properties(pd2 => pd2
.Keyword(kpd => kpd
.Name(x => x.Color)
)
)
)
)
)
)
我不能为我的生活找到如何查询所有产品的产品索引,例如标签上带有TagName
“橙色”的标签。
我设法做到这一点,然后我不知道
client.Search<Product>(s => s
.Query(q => q
.Nested(nqd => nqd
.Path(x => x.Tags)
.Query(qcd => qcd
.Bool(bqd => bqd
.Must(qcd
.Match(mqd => mqd
.???
)
)
)
)
)
)
由于Nested
似乎没有设置另一种类型来查询我对如何进行此查询感到有点迷失。
答案 0 :(得分:1)
尝试mqd.Tags.First()。TagName,&#34; admiral&#34;
答案 1 :(得分:1)
以下将会这样做
client.Search<Product>(s => s
.Query(q => q
.Nested(nqd => nqd
.Path(x => x.Tags)
.Query(qcd => qcd
.Match(mqd => mqd
.Field(f => f.Tags.First().TagName)
.Query("orange")
)
)
)
)
);
生成
{
"query": {
"nested": {
"query": {
"match": {
"tags.tagName": {
"query": "orange"
}
}
},
"path": "tags"
}
}
}
获取字段名称的lambda表达式就是表达式。 NEST理解此上下文中的LINQ方法.First()
是遍历目标字段的表达式的一部分。你也可以使用
f => f.Tags[0].TagName
f => f.Tags.Last().TagName
f => f.Tags.Single().TagName
f => f.Tags.ElementAt(0).TagName
f => f.Tags.Max().TagName
或任何其他LINQ表达式,它将返回可以访问属性的ProductTag
,并表示MemberExpression