简体   繁体   English

通过多个字段聚合并在ElasticSearch中计数

[英]Aggregate by multiple fields and count in ElasticSearch

I am having an index with the following data: 我有一个包含以下数据的索引:

{
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl8H042YsxaK6w02jY",
        "_score": 1,
        "_source": {
          "user": "102",
          "song": "1",
          "created": "12-28-2018 19:12:12"
        }
      },
      {
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl8PM5zqmU2ne0dY9W",
        "_score": 1,
        "_source": {
          "user": "102",
          "song": "5",
          "created": "01-1-2018 23:12:12"
        }
      },
      {
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl8PPx2YsxaK6w02jc",
        "_score": 1,
        "_source": {
          "user": "102",
          "song": "5",
          "created": "01-1-2018 23:12:12"
        }
      },
      {
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl8Xdm2YsxaK6w02jf",
        "_score": 1,
        "_source": {
          "user": "102",
          "song": "5",
          "created": "06-10-2018 15:16:12"
        }
      },
      {
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl8XgvzqmU2ne0dY9a",
        "_score": 1,
        "_source": {
          "user": "102",
          "song": "5",
          "created": "06-10-2018 15:16:12"
        }
      },
      {
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl8ZLwzqmU2ne0dY9d",
        "_score": 1,
        "_source": {
          "user": "103",
          "song": "5",
          "created": "06-10-2018 15:16:12"
        }
      },
      {
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl8aF1zqmU2ne0dY9e",
        "_score": 1,
        "_source": {
          "user": "103",
          "song": "6",
          "created": "06-10-2018 15:16:12"
        }
      },
      {
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl79hm2YsxaK6w02jW",
        "_score": 1,
        "_source": {
          "user": "102",
          "song": "1",
          "created": "1-02-2018 13:12:12"
        }
      },
      {
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl8O1nzqmU2ne0dY9U",
        "_score": 1,
        "_source": {
          "user": "102",
          "song": "5",
          "created": "01-1-2018 23:12:12"
        }
      },
      {
        "_index": "recommender",
        "_type": "doc",
        "_id": "AWZl8O9F2YsxaK6w02ja",
        "_score": 1,
        "_source": {
          "user": "102",
          "song": "5",
          "created": "01-1-2018 23:12:12"
        }
      }

I want to get a count of songs per user. 我想统计每个用户的歌曲数。 So I can get the data as, user 102 played song 5 for a total of 4 times. 因此,我可以得到这样的数据:用户102播放了歌曲5总共4次。

I am apple to count the no. 我是苹果,算不上。 of users or no. 用户数或否。 of songs separately. 的歌曲分开。 But, not able to aggregate by song and user together. 但是,无法将歌曲和用户聚集在一起。

POST /eventindex/_search?size=0
{
    "aggs" : {
        "doc" : {
            "terms" : { "field" : "user" }
        }
    }
}

I read I can use sub-aggregation and tried something as this: 我读到可以使用子聚合并尝试了以下操作:

POST /recommender/_search?size=0

{
      "aggs": {
        "doc": {
          "nested": {
            "path": "docs"
          },
          "aggs": {
            "name": {
              "terms": {
                "field": "user"
              },
              "aggs": {
                "name": {
                  "terms": {
                    "field": "song"
                  }
                }
              }
            }
          }
        }
      }
    }
}

This however didn't work. 但是,这没有用。 What would be the right way to solve this? 解决这个问题的正确方法是什么?

Hopefully this should solve your problem 希望这可以解决您的问题

 POST /recommender/_search?size=0 { "aggs": { "song": { "terms": { "field": "song" }, "aggs": { "user": { "terms": { "field": "user" } } } } } } 

This query will output individual user count for each Song. 该查询将为每首歌曲输出单独的用户计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM