如何使用基于多个术语的嵌套文档进行过滤?
How to filter with a nested document based on multiple terms?
我正在尝试在 NEST 中复制此 DSL 查询。基本上是一个结构化过滤器,它将 return 所有具有红色的产品。
{
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "keywordFacets",
"query": {
"bool": {
"filter": [
{ "term": { "keywordFacets.name": "color" } },
{ "term": { "keywordFacets.value": "Red" } }
]
}
}
}
}
]
}
}
}
这是带有属性映射的 POCO。
[ElasticsearchType]
public class Product
{
[Keyword]
public long ProductId { get; set; }
[Nested]
public List<KeywordFacet> KeywordFacets { get; set; }
// other properties...
}
[ElasticsearchType]
public class KeywordFacet
{
[Keyword]
public string Name { get; set; }
[Keyword]
public string Value { get; set; }
}
我不知道如何在嵌套过滤器数组中获取这两个项。这是我迄今为止失败的尝试:
var searchRequest = new SearchDescriptor<Product>()
.Query(q => q
.Bool(b => b
.Filter(bf => bf
.Nested(nq => nq
.Path(nqp => nqp.KeywordFacets)
.Query(qq => qq
.Bool(bb => bb
.Filter(ff => ff
.Term(t => t
.Field(p => p.KeywordFacets.First().Name)
.Value("color")
.Field(p2 => p2.KeywordFacets.First().Value).Value("Red")))))))));
这是我在 Postman 中 运行 DSL 查询时 return 编辑的一些示例数据:
{
"productId": 183150,
"keywordFacets": [
{
"name": "color",
"value": "Red"
},
{
"name": "color",
"value": "Blue"
},
{
"name": "color",
"value": "Grey"
}
]
}
这是经过摸索一段时间后的正确语法。
var searchRequest = new SearchDescriptor<Product>()
.Query(q => q
.Bool(b => b
.Filter(bf => bf
.Nested(nq => nq
.Path(nqp => nqp.KeywordFacets)
.Query(qq => qq.Bool(bb => bb
.Filter(ff => ff
.Term(t => t
.Field(p => p.KeywordFacets[0].Name).Value("color"))))
&& qq.Bool(bb => bb
.Filter(ff => ff
.Term(t => t
.Field(p2 => p2.KeywordFacets[0].Value).Value("Red"))))
)
)
)
)
);
我正在尝试在 NEST 中复制此 DSL 查询。基本上是一个结构化过滤器,它将 return 所有具有红色的产品。
{
"query": {
"bool": {
"filter": [
{
"nested": {
"path": "keywordFacets",
"query": {
"bool": {
"filter": [
{ "term": { "keywordFacets.name": "color" } },
{ "term": { "keywordFacets.value": "Red" } }
]
}
}
}
}
]
}
}
}
这是带有属性映射的 POCO。
[ElasticsearchType]
public class Product
{
[Keyword]
public long ProductId { get; set; }
[Nested]
public List<KeywordFacet> KeywordFacets { get; set; }
// other properties...
}
[ElasticsearchType]
public class KeywordFacet
{
[Keyword]
public string Name { get; set; }
[Keyword]
public string Value { get; set; }
}
我不知道如何在嵌套过滤器数组中获取这两个项。这是我迄今为止失败的尝试:
var searchRequest = new SearchDescriptor<Product>()
.Query(q => q
.Bool(b => b
.Filter(bf => bf
.Nested(nq => nq
.Path(nqp => nqp.KeywordFacets)
.Query(qq => qq
.Bool(bb => bb
.Filter(ff => ff
.Term(t => t
.Field(p => p.KeywordFacets.First().Name)
.Value("color")
.Field(p2 => p2.KeywordFacets.First().Value).Value("Red")))))))));
这是我在 Postman 中 运行 DSL 查询时 return 编辑的一些示例数据:
{
"productId": 183150,
"keywordFacets": [
{
"name": "color",
"value": "Red"
},
{
"name": "color",
"value": "Blue"
},
{
"name": "color",
"value": "Grey"
}
]
}
这是经过摸索一段时间后的正确语法。
var searchRequest = new SearchDescriptor<Product>()
.Query(q => q
.Bool(b => b
.Filter(bf => bf
.Nested(nq => nq
.Path(nqp => nqp.KeywordFacets)
.Query(qq => qq.Bool(bb => bb
.Filter(ff => ff
.Term(t => t
.Field(p => p.KeywordFacets[0].Name).Value("color"))))
&& qq.Bool(bb => bb
.Filter(ff => ff
.Term(t => t
.Field(p2 => p2.KeywordFacets[0].Value).Value("Red"))))
)
)
)
)
);