【问题标题】:Elastic search aggregation弹性搜索聚合
【发布时间】:2014-08-13 17:06:44
【问题描述】:

如果颜色不同,有没有办法只退回一种产品。 例如假设我有一个具有以下属性的产品:

brand,color,title
nike, red, air max
nike, blue, air max

现在我想创建弹性搜索查询以在聚合时只返回一个产品,但算作两个属于品牌耐克。

{
    "query" : {
        "match_all" : {}

    },

    "aggs" : {
        "brand" : {
            "terms" : {
                "field" : "brand"
            },
            "aggs" : {
                "size" : {
                    "terms" : {
                        "field" : "title"
                    }
                }
            }
        }

    }

}

我无法获得想要的结果。我想要select name,color,title, count(*) title from product group by name,title

【问题讨论】:

    标签: lucene elasticsearch


    【解决方案1】:

    我想你想获取按名称、标题聚合的文档

    这可以使用topHits 聚合来完成。

    {
       "size": 0,
       "query": {
          "match_all": {}
       },
       "aggs": {
          "brand": {
             "terms": {
                "field": "name"
             },
             "aggs": {
                "size": {
                   "terms": {
                      "field": "title"
                   }
                },
                "aggs":{
                    "top_hits" :{
                        "_source" :[ "name","color","band"],
                        "size":1
                    }
                }
             }
          }
       }
    }
    

    对于计数,返回的存储桶中始终存在 doc_count。

    希望这会有所帮助!!如果我遗漏了什么,请务必提及。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 2019-11-08
      • 2021-02-19
      • 2023-03-18
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多