0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Its working, but the doc count of songs is completely wrong. What could be the reason for that? I want the count per song, per user.
this didn't work as needed either. My requirement is to get the counts of songs per user. In the updated one, user comes under song.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.