【问题标题】:Global term aggregation with filtered count - Elasticsearch 5具有过滤计数的全局术语聚合 - Elasticsearch 5
【发布时间】:2016-11-30 13:43:35
【问题描述】:

我将产品存储在 ES 中,我正在尝试按它们的大小汇总它们。我想设计以下行为。对于即使在查询之外的每个术语,也可以根据查询接收术语计数。

所以查询尺寸 ["S", "M"] 我想收到:

S:1
男:1
左:0

这有可能吗?

这是我得到以下结果的设置:

S:1
男:1

但是 L 完全不见了。

PUT demo
{
    "mappings": {
        "product": {
            "properties": {
                "size": { 
                  "type": "keyword"
                }
            }
        }
    }
}

PUT demo/product/1
{
    "size": "S"  
}

PUT demo/product/2
{
    "size": "M"  
}

PUT demo/product/3
{
    "size": "L"  
}

GET demo/_search
{
   "size": 0,
   "query": {
      "bool": {
         "must": [
            {
               "terms": {
                  "size": [
                     "S",
                     "M"
                  ]
               }
            }
         ]
      }
   },
   "aggs": {
      "size": {
         "terms": {
            "field": "size"
         }
      }
   }
}

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-5


    【解决方案1】:

    您可以使用过滤器。

    {
        "size": 0,
        "query": {
            "bool": {
                "must": [
                    { "terms": { "field": "size" } }
                ],
                "filter": { 
                    "terms": { "size": [ "S", "M"] } 
                }
            }
        },
        "aggs": {
            "size": {
                "terms": { "field": "size" }
            }
        }
    }
    

    【讨论】:

    • 这似乎在 elasticsearch 5 中不起作用。elastic.co/guide/en/elasticsearch/reference/current/…
    • @JosefRousek 我已经编辑了我的答案。现在在 ES5 中重试
    • 感谢您的努力,但这也不起作用。在发布之前我已经做了很长时间的研究,所以这只是我描述问题时遇到的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 2019-01-11
    • 2021-06-06
    • 2015-12-30
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多