bool查询中的多个异或过滤器

时间:2016-06-27 14:20:26

标签: elasticsearch nest

在Nest的早期版本中,即在1.x中允许多个独占或过滤器,如下面的原始查询

在2.x中不再推荐使用AS或过滤器,可以替代它。如何在Bool查询中使用多个独占应该(或过滤器)和必须?

来自1.x的原始查询

  {
       "bool": {
         "must": [
           {
             "bool": {
               "must": [
                 {
                   "term": {
                     "EntityType": "ITSTrucks"
                   }
                 },
                 {
                   "or": {
                     "filters": [
                       {
                         "term": {
                           "AvailableDate": 36523
                         }
                       },
                       {
                         "range": {
                           "AvailableDate": {
                             "gte": "42036"
                           }
                         }
                       }
                     ],
                     "_name": "date"
                   }
                 },
                 {
                   "or": {
                     "filters": [
                       {
                         "geo_distance": {
                           "OriginCoordinate": "42.815656,-73.942324",
                           "distance": "100miles",
                           "distance_type": "plane"
                         }
                       }
                     ],
                     "_name": "OriginOrFilter"
                   }
                 },
                 {
                   "or": {
                     "filters": [
                       {
                         "term": {
                           "IsOpen": true
                         }
                       },
                       {
                         "term": {
                           "IsDestinationBoundaryNull": true
                         }
                       }
                     ],
                     "_name": "DestinationOrFilter"
                   }
                 }
               ]
             }
           }
         ]
       }
     }

会是这样的吗?

    BoolQueryDescriptor<AvailableTrucks>  queryParameterMust= new BoolQueryDescriptor<AvailableTrucks>();
    BoolQueryDescriptor<AvailableTrucks> queryParameterShouldFirst= new BoolQueryDescriptor<AvailableTrucks>();
    BoolQueryDescriptor<AvailableTrucks> queryParameterShouldSecond= new BoolQueryDescriptor<AvailableTrucks>();
    BoolQueryDescriptor<AvailableTrucks> queryParameterShouldThird= new BoolQueryDescriptor<AvailableTrucks>();
    if (mustfc.Count > 0)
    {
        queryParameterMust.Must(mustfc.ToArray());
    }
    if (shouldQueryListFirst.Count > 0)
    {
        queryParameterShouldFirst.Should(shouldQueryListFirst.ToArray());
    }
    if (ShouldQueryListSecond.Count > 0)
    {
        queryParameterShouldSecond.Should(ShouldQueryListSecond.ToArray());
    }
    if (ShouldQueryListThird.Count > 0)
    {
        queryParameterShouldThird.Should(ShouldQueryListThird.ToArray());
    }
elasticClient.Search<Truck>(s => s.Query(f => f.Bool(c => c.Must(u => u.MatchAll()).Filter(q => q.Bool(b => queryParameterMust && queryParameterShouldFirst && queryParameterShouldSecond && queryParameterShouldThird)))))>Index("Trucks")

1 个答案:

答案 0 :(得分:0)

在2.x.x版本中,您可以使用Should术语进行逻辑OR查询。

一些例子:

{
    "bool" : {
        "must" : {
            "term" : { "user" : "kimchy" }
        }
        "should" : [
            {
                "term" : { "tag" : "wow" }
            },
            {
                "term" : { "tag" : "elasticsearch" }
            }
        ],
        "minimum_should_match" : 1
    }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

请注意,您也可以嵌套这些条款。

要使用NEST构建查询,您可以尝试:

var mustMatchQueries = new List<QueryContainer>()
var shouldMatchQueries = new List<QueryContainer>();

// ... add queries to both lists

// aggregate queries
var mustMatchAggregatedTerms = mustMatchQueries.Aggregate(new QueryContainer(), (s, ff) => s &= ff);
var shouldMatchAggregatedTerms = shouldMatchQueries.Aggregate(new QueryContainer(), (s, ff) => s |= ff);

// build descriptor
var descriptor = new SearchDescriptor<DocumentClassName>()
descriptor.Query(q => q.Bool(b => b.Must(m => mustMatchAggregatedTerms && m.Bool(c => c.Should(shouldMatchAggregatedTerms)))));