esafwan
esafwan

Reputation: 18029

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.

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?

Upvotes: 0

Views: 1322

Answers (1)

Mohammed Ashfaq
Mohammed Ashfaq

Reputation: 3426

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.

Upvotes: 1

Related Questions