在嵌套中组合 BoolQueryDescriptor 2.x
Combining BoolQueryDescriptor in Nest 2.x
下面的代码有两个 BoolQueryDescriptor 类型的变量。各自的变量都有自己不同的Must, MustNot, Filter部分。
有没有办法将每个变量中的所有相应的 Must(s) 、 Filters 、 MustNot(s) 组合成 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"
}
}
}
]
}
}
}
下面的代码有两个 BoolQueryDescriptor 类型的变量。各自的变量都有自己不同的Must, MustNot, Filter部分。
有没有办法将每个变量中的所有相应的 Must(s) 、 Filters 、 MustNot(s) 组合成 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"
}
}
}
]
}
}
}