# Searching by size of nested type field

**URL:** <https://discuss.elastic.co/t/searching-by-size-of-nested-type-field/21588>\
**Category:** Elasticsearch\
**Created:** [January 12, 2015, 4:52pm UTC](https://discuss.elastic.co/t/searching-by-size-of-nested-type-field/21588 "2015-01-12T16:52:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hayden\_Marchant](https://avatars.discourse-cdn.com/v4/letter/h/7feea3/32.png) [@Hayden\_Marchant](https://discuss.elastic.co/u/Hayden_Marchant)\
**Post date:** [January 12, 2015, 4:52pm UTC](https://discuss.elastic.co/t/searching-by-size-of-nested-type-field/21588/1 "2015-01-12T16:52:01Z")

</div>

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.

> <https://gist.github.com/haydenmarchant/213e03924e79b4412200>

I have a few options on the plate:

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

"doc["comments.id'].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  
document.

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 \>  
45.

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 [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0c28c76a-30a9-44fe-9a37-1807a410541c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0c28c76a-30a9-44fe-9a37-1807a410541c%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:39am UTC](https://discuss.elastic.co/t/searching-by-size-of-nested-type-field/21588/2 "2017-07-06T00:39:30Z")

</div>


