以下代码具有两个BoolQueryDescriptor类型的变量。各个变量中有各自不同的Must,MustNot和Filter部分。

有没有一种方法可以将来自每个变量的所有各自的Must,Filters,MustNot组合到BoolQueryDescriptor变量中,并提供给最终查询或Boolquery。

BoolQueryDescriptor<Requote> queryParameter = null;
BoolQueryDescriptor<Requote> queryParameterMultiple = null;

queryParameter = QueryFilterBuilder.BuildQueryFilter<Requote>(group);
queryParameterMultiple = QueryFilterBuilder.BuildQueryFilterForMultipleInputValues<Requote>(group);


Var searchResult = elasticClient.Search<Requote>(query => query.Query(q => q.Bool(b => queryParameter)).Index(IndexConstants.Requote).Size(searchInputs.ResultCount).From(searchInputs.From).Sort(s => s.Field(sortField, sortOrder)));

最佳答案

由于BoolQueryDescriptor<T>明确实现了IBoolQuery,因此您可以将每个描述符转换为IBoolQuery并将这些子句组合成一个新的BoolQueryDescriptor<T>

public static BoolQueryDescriptor<T> CombineBoolQueryDescriptors<T>(params BoolQueryDescriptor<T>[] queries) where T : class
{
    var descriptor = new BoolQueryDescriptor<T>();
    var combinedQuery = (IBoolQuery)descriptor;

    foreach (var query in queries.Cast<IBoolQuery>())
    {
        if (query.Must != null)
        {
            combinedQuery.Must = combinedQuery.Must != null
                ? combinedQuery.Must.Concat(query.Must)
                : (query.Must.ToArray());
        }
        if (query.Should != null)
        {
            combinedQuery.Should = combinedQuery.Should != null
                ? combinedQuery.Should.Concat(query.Should)
                : (query.Should.ToArray());
        }

        if (query.MustNot != null)
        {
            combinedQuery.MustNot = combinedQuery.MustNot != null
                ? combinedQuery.MustNot.Concat(query.MustNot)
                : (query.MustNot.ToArray());
        }

        if (query.Filter != null)
        {
            combinedQuery.Filter = combinedQuery.Filter != null
                ? combinedQuery.Filter.Concat(query.Filter)
                : (query.Filter.ToArray());
        }
    }

    return descriptor;
}

您将需要决定如何将其他属性(例如.Boost().MinimumMustMatch()Strict().Verbatim()等)应用于新的组合bool查询。

一个例子
var bool1 = new BoolQueryDescriptor<Document>()
    .Must(
        m => m.Term("field", "value"),
        m => m.Match(match => match.Query("query").Field("field"))
    );

var bool2 = new BoolQueryDescriptor<Document>()
.Must(
    m => m.Term("field1", "value1"),
    m => m.Match(match => match.Query("query1").Field("field1"))
);

var combined = CombineBoolQueryDescriptors(bool1, bool2);

client.Search<Document>(s => s.Query(q => q.Bool(b => combined)));

产量
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "field": {
              "value": "value"
            }
          }
        },
        {
          "match": {
            "field": {
              "query": "query"
            }
          }
        },
        {
          "term": {
            "field1": {
              "value": "value1"
            }
          }
        },
        {
          "match": {
            "field1": {
              "query": "query1"
            }
          }
        }
      ]
    }
  }
}

关于elasticsearch - 在Nest 2.x中合并BoolQueryDescriptor,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38014661/

10-10 07:39