问题描述
我已经有两个QueryDescriptor变量,可以动态地建立布尔查询,例如:
QueryContainer should_container = new QueryContainer();
QueryContainer filter_container = new QueryContainer();
foreach (var parameter in should_parameters) {
should_container |= constructClause(parameter);
}
foreach (var parameter in filter_parameters) {
filter_container &= constructClause(parameter);
}
当我尝试合并这两个变量以构建查询时,如下所示:
var result = client.Search<ElasticSearchProject>(s=>s
.From(0)
.Size(10)
.Query(qr => qr
.Bool(b => b
.Should(should_container)
.Filter(filter_container)
)
)
);
调试返回的结果变量:
"query": {
"bool": {
"should" : [
"bool": {
"should": [
{...},{...}
]
}
],"filter": [
"bool": {
"must": [
{...},{...}
]
}
]
不是不正确!但我想知道是否有一种方法可以避免使用这两个should
子句,并获得以下输出:
"query": {
"bool": {
"should" : [
{...},{...}
],{...}
]
}
]
那给了我完全相同的结果。如何使用nesT编写它以获取上述查询? 我正在使用Elasticsearch 6.8.0
解决方法
所有布尔查询should
,must
,filter
,must_not
子句都接受params QueryContainer[]
个查询,因此对于每个子句,您可以使用{ {1}}收集对子句的查询,然后List<QueryContainer>
列表
.ToArray()
如果子句中有很多查询,则此will be faster than combining many queries with the overloaded operators。