2015-12-21 93 views
0

我使用过滤器构建产品列表页面。有很多过滤器,并且它们的数据在ES中用聚合函数计数。 最简单的例子,如果最大/最小价格:使用过滤器且不使用它的ElasticSearch聚合

{ 
 
    "size": 0, 
 
    "query": { 
 
    "filtered": { 
 
     "query": { 
 
     "match_all": {} 
 
     }, 
 
     "filter": { 
 
     "bool": { 
 
      "must": [ 
 
      { 
 
       "term": { 
 
       "shop_id": 44 
 
       } 
 
      }, 
 
      { 
 
       "term": { 
 
       "CategoryId": 36898 
 
       } 
 
      }, 
 
      { 
 
       "term": { 
 
       "products_status": 1 
 
       } 
 
      }, 
 
      { 
 
       "term": { 
 
       "availability": 3 
 
       } 
 
      } 
 
      ] 
 
     } 
 
     } 
 
    } 
 
    }, 
 
    "aggs": { 
 
    "min_price": { 
 
     "min": { 
 
     "field": "products_price" 
 
     } 
 
    }, 
 
    "max_price": { 
 
     "max": { 
 
     "field": "products_price" 
 
     } 
 
    } 
 
    } 
 
}

所以,在此ES请求,根据安装在过滤规则返回箱最小和最大的价格(CATEGORY_ID 36898,shop_id 44等)。 它工作完美。

问题是:是否可以更新此请求并获得没有过滤器的聚合?或者也许有可能在一个请求中用另一个过滤器返回聚合数据?

我的意思是我返回的数据:

MIN_PRICE和MAX_PRICE用于过滤的数据(QUERY1)

和mix_price和MAX_PRICE非过滤的数据(或查询过滤2数据)?

回答

0

对于聚合,您可以使用global选项来不应用查询块中提供的任何过滤器。

例如,您的查询使用以下json输入。

 
{ 
    "size": 0, 
    "query": { 
     "filtered": { 
      "query": { 
       "match_all": {} 
      }, 
      "filter": { 
       "bool": { 
        "must": [ 
        { 
         "term": { 
          "shop_id": 44 
         } 
        }, 
        { 
         "term": { 
          "CategoryId": 36898 
         } 
        }, 
        { 
         "term": { 
          "products_status": 1 
         } 
        }, 
        { 
         "term": { 
          "availability": 3 
         } 
        } 
        ] 
       } 
      } 
     } 
    }, 
    "aggs": { 
     "min_price": { 
      "min": { 
       "field": "products_price" 
      } 
     }, 
     "max_price": { 
      "max": { 
       "field": "products_price" 
      } 
     }, 
     "without_filter_min": { 
      "global": {}, 
      "aggs": { 
       "price_value": { 
        "min": { 
         "field": "products_price" 
        } 
       } 
      } 
     }, 
     "without_filter_max": { 
      "global": {}, 
      "aggs": { 
       "price_value": { 
        "max": { 
         "field": "products_price" 
        } 
       } 
      } 
     } 
    } 
} 
相关问题