通过多个字段进行汇总并计入ElasticSearch

时间:2018-10-12 02:11:37

标签: elasticsearch group-by count aggregate

我有一个包含以下数据的索引:

{
        "_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"
        }
      }

我想统计每个用户的歌曲数。这样一来,用户102播放歌曲5总共4次即可得到数据。

我是苹果,不算数。用户数或否。的歌曲分开。但是,无法将歌曲和用户聚集在一起。

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

我读到可以使用子聚合,并尝试了以下方法:

POST /recommender/_search?size=0

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

但是这没有用。解决这个问题的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

希望这可以解决您的问题

 listings.asInstanceOf[JsArray].value.map{ jsvalue =>
  (jsvalue.asInstanceOf[JsObject]) + ("extra" -> JsString("something"))
}

此查询将为每首歌曲输出单独的用户计数。