0

For every document I have a category array, it looks like this:

[{
    "id": 1,
    "level": 1
}, {
    "id": 2,
    "level": 2
}, {
    "id": 3,
    "level": 3
}]

How can I count the categories I have in every document according to the level 3 category.id?

1
  • pls add more info or post a query along with schema would be good.thx Commented Aug 21, 2016 at 18:39

1 Answer 1

1

category array shall be nested field. And the rest can be handled via aggregations. Try something similar to the code given below.

"aggregations": {
"mainAgg": {
  "nested": {
    "path": "category"
  },
  "aggs": {
    "levelFilter": {
        -- filter condition
      "filter": {
        "term": {
          "level": 3
        },
          -- count aggregation
        "aggs": {
          "count": {
            "value_count": {
              "field": "level"
            }
          }
        }
      }
    }
  }
}

}

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

Comments

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.