如何对对象数组进行嵌套查询
How to do a nested query into an array of objects
我有一个带有嵌套对象数组的简单对象
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
为 "orange"。
我设法走到这一步,然后我不知道
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
似乎没有设置另一种类型来查询,我有点不知道如何进行这个查询。
通过 mqd.Tags.First().TagName,"admiral"
下面会做
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 表达式 return 可以访问 属性 的 ProductTag
,并且表示 MemberExpression
我有一个带有嵌套对象数组的简单对象
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
为 "orange"。
我设法走到这一步,然后我不知道
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
似乎没有设置另一种类型来查询,我有点不知道如何进行这个查询。
通过 mqd.Tags.First().TagName,"admiral"
下面会做
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 表达式 return 可以访问 属性 的 ProductTag
,并且表示 MemberExpression