2016-11-29 51 views
0

我正在尝试通过elasticsearch计算我的数据库中字段的总计平均值。 我不具有不进行任何过滤计算Av值的任何问题:应用筛选器以弹性搜索排除嵌套对象字段上的特定数值

{ 
    "query": { 
     "match_all":{} 
    }, 
    "size": 0, 
    "aggs": { 
     "avg_quantity": { 
      "avg": { 
       "field": "license_offer.unit_price" 
      } 
     } 
    } 
} 

但是我需要从具有0 license_offer.unit_price聚集文档排除(licence_offer是内许可嵌套对象)。

我尝试不同的东西,这是我最新的尝试:

{ 
    "size": 0, 
    "query": { 
     "constant_score": { 
      "filter": { 
       "license_offer.unit_price": { 
         "gte": 0 
       } 
      } 
     } 
    }, 
    "aggs": { 
     "quantity_stats": { 
      "stats": { 
       "field": "license_offer.unit_price" 
      } 
     } 
    } 

} 

但我得到一个错误:

"type": "parsing_exception", 
     "reason": "no [query] registered for [license_offer.unit_price]", 

如何申请一个过滤器,以排除对一个具体数值嵌套对象的领域与弹性搜索?

回答

0

您的查询是不正确的,你只是缺少range关键字:

{ 
    "size": 0, 
    "query": { 
     "constant_score": { 
      "filter": { 
       "range": {       <--- add this 
        "license_offer.unit_price": { 
         "gte": 0 
        } 
       } 
      } 
     } 
    }, 
    "aggs": { 
     "quantity_stats": { 
      "stats": { 
       "field": "license_offer.unit_price" 
      } 
     } 
    } 

} 

您还可以将过滤器凝聚部内:

{ 
    "size": 0, 
    "aggs": { 
    "only_positive": { 
     "filter": { 
     "range": { 
      "license_offer.unit_price": { 
      "gt": 0 
      } 
     } 
     }, 
     "aggs": { 
     "quantity_stats": { 
      "stats": { 
      "field": "license_offer.unit_price" 
      } 
     } 
     } 
    } 
    } 
} 
+0

感谢@val请你展示你如何通过特别排除0值来做到这一点? –

+0

使用'gt'而不是'gte'? – Val

+0

是的,我只是这样做,但有没有办法做到这一点,而不使用范围逻辑,而是使用“排除”/过滤逻辑(如“不是0”) –