6

Sample documents:

{
    "id": "62655",
    "attributes": [
        {
            "name": "genre",
            "value": "comedy"
        },
        {
            "name": "year",
            "value": "2016"
        }
    ]
}

{
    "id": "62656",
    "attributes": [
        {
            "name": "genre",
            "value": "horror"
        },
        {
            "name": "year",
            "value": "2016"
        }
    ]
}

{
    "id": "62657",
    "attributes": [
        {
            "name": "language",
            "value": "english"
        },
        {
            "name": "year",
            "value": "2015"
        }
    ]
}

Expected Output:

{
    "hits" : {
        "total": 3,
        "hits": []
    },
    "aggregations": {
        "attribCount": {
            "language": 1,
            "genre": 2,
            "year": 3
        },
        "attribVals": {
            "language": {
                "english": 1
            },
            "genre": {
                "comedy": 1,
                "horror": 1
            },
            "year": {
                "2016": 2,
                "2015": 1
            }
        }
    }
}

My Query:

I could get the "attribCount" aggregation using below query. But I don't know how to get each attribute value count.

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            }
        }
    },
    "aggs": {
        "attribCount": {
            "terms": {
                "field": "attributes.name",
                "size": 0
            }
        }
    },
    "size": 0
}

When I aggregate using attributes.value, it gives overall count. But I need it listed under the name value as given in expected output.

Sriram
  • 8,574
  • 4
  • 21
  • 30
  • https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html – blackmamba Sep 20 '16 at 09:50
  • @blackmamba i already looked at it. its not clear how to accomplish my requirement. When i give path as "attributes" i get exception. when i give "attributes.name", it get count as 0 – Sriram Sep 20 '16 at 10:01
  • you have mapped attributes and its parent as nested right ? – blackmamba Sep 20 '16 at 10:11
  • @blackmamba i just structure the data in code as an array and let elastic make the mapping automatically based on data – Sriram Sep 20 '16 at 10:12
  • you'll have to make them both as nested type, predefined it before populating data. – blackmamba Sep 20 '16 at 10:20
  • deleted the index, created new index and tried with the mapping given here (http://codebeautify.org/jsonviewer/cbc3d7e5). still doesnt work – Sriram Sep 20 '16 at 10:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/123775/discussion-between-sriram-and-blackmamba). – Sriram Sep 20 '16 at 10:40

1 Answers1

5

As you say the attribute field is nested. Try this, this will work

{
  "size": 0,
  "aggs": {
    "count": {
      "nested": {
        "path": "attributes"
      },
      "aggs": {
        "attribCount": {
          "terms": {
            "field": "attributes.name"
          }
        },
        "attribVal": {
          "terms": {
            "field": "attributes.name"
          },
          "aggs": {
            "attribval2": {
              "terms": {
                "field": "attributes.value"
              }
            }
          }
        }
      }
    }
  }
}
blackmamba
  • 556
  • 3
  • 11
  • Just came across http://stackoverflow.com/a/31052532/1179958 this answer, which is exactly what I was looking for and when i was here to tell you that, you have answered it exactly :) – Sriram Sep 20 '16 at 11:53