Searching by size of nested type field

I would like to be able to filter documents based on the number of elements
in a list of nested object. In the classic example of posts, I am modeiing
the comments field as a nested field of the post type. I would like to be
able to query all posts where # comments = N.

I do not have a clear idea about how to do this, and would like some ideas
please. Here is a gist that shows what the model is like. I have created 2
comments fields - one nested and one unnested in case the methods for
doing this depend on type of object, even though in my case I really need a
nested object.

I have a few options on the plate:

  1. Craft a query/filter that will express this constraint using a script
    such as :

"doc["'].values.size() == 2"

  1. Have an additional field called comments_count field in the post
    document which is automatically maintained each time a comment is added.

  2. Similar to #2, manually maintain a comment_count field in the post

Even if I manage to do #1, the question is how costly it will be on my
queries. I intend on using this as one of many filters in document
aggregations for a reporting console. e.g. give me breakdown of blogs by
date where #comment > 3, and post is tagged 'sport' and age of poster is >

I am indexing using the Bulk API, and even though I see a way to invoke a
script during update of a document, I don't see how this can be done during
Bulk update.

Manually maintaining a counter field only works if I have all instances of
comments each time I update a post - this means that I'd have to read,
modify & write.

You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
For more options, visit