在QueryContainer中动态添加filter子句

问题描述

我已经有两个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

解决方法

所有布尔查询shouldmustfiltermust_not子句都接受params QueryContainer[]个查询,因此对于每个子句,您可以使用{ {1}}收集对子句的查询,然后List<QueryContainer>列表

.ToArray()

如果子句中有很多查询,则此will be faster than combining many queries with the overloaded operators

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...