# Help querying array of nested objects

**URL:** https://discuss.elastic.co/t/help-querying-array-of-nested-objects/10644
**Category:** Elasticsearch
**Created:** [February 6, 2013, 8:41pm UTC](https://discuss.elastic.co/t/help-querying-array-of-nested-objects/10644 "2013-02-06T20:41:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Roly\_Vicaria](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roly_vicaria/32/506_2.png) [@Roly\_Vicaria](https://discuss.elastic.co/u/Roly_Vicaria)
#### Post date: [February 6, 2013, 8:41pm UTC](https://discuss.elastic.co/t/help-querying-array-of-nested-objects/10644/1 "2013-02-06T20:41:01Z")

</div>

Hello,

I'm having difficulty crafting a query and I was hoping for some help. I  
created a gist with the general idea: [https://gist.github.com/rolyv/4725510](https://gist.github.com/rolyv/4725510)

Each of my documents has an array of these "criteria" objects. These  
criteria objects each have a boolean flag for requiredness and a token id.  
Each user in the system has a set of tokens. So when a user executes a  
query, I take the user's tokens and I want to craft a query/filter so that  
the documents returned follow this logic:

1. a document is returned if all its required criteria are met by the user.  
(a document with only optional, required = false, criteria would also be  
returned)
2. the score of the document should directly relate to how many of the  
criteria are met by the user (both optional and required).

I think I can do part 2 straightforwardly with a bool query and a bunch of  
should clauses, but the filter for part 1 is evading me. Any help is much  
appreciated.

Thanks,

Roly

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)
#### Post date: [February 8, 2013, 3:47pm UTC](https://discuss.elastic.co/t/help-querying-array-of-nested-objects/10644/2 "2013-02-08T15:47:01Z")

</div>

Hey Roly,

To query in your criteria objects you will need to use a nested  
query/filter as a wrapper. For the other properties you can use a regular  
query.  
See my query example here: [query.json · GitHub](https://gist.github.com/martijnvg/4739823)

You can use the nested query's score mode to control how the scores from  
the criteria objects are mapped into the search hits:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Martijn

On 6 February 2013 21:41, Roly Vicaria [rolyv19@gmail.com](mailto:rolyv19@gmail.com) wrote:

> Hello,
> 
> I'm having difficulty crafting a query and I was hoping for some help. I  
> created a gist with the general idea:  
> [https://gist.github.com/rolyv/4725510](https://gist.github.com/rolyv/4725510)
> 
> Each of my documents has an array of these "criteria" objects. These  
> criteria objects each have a boolean flag for requiredness and a token id.  
> Each user in the system has a set of tokens. So when a user executes a  
> query, I take the user's tokens and I want to craft a query/filter so that  
> the documents returned follow this logic:
> 
> 1. a document is returned if all its required criteria are met by the  
> user. (a document with only optional, required = false, criteria would also  
> be returned)
> 2. the score of the document should directly relate to how many of the  
> criteria are met by the user (both optional and required).
> 
> I think I can do part 2 straightforwardly with a bool query and a bunch of  
> should clauses, but the filter for part 1 is evading me. Any help is much  
> appreciated.
> 
> Thanks,
> 
> Roly
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:52am UTC](https://discuss.elastic.co/t/help-querying-array-of-nested-objects/10644/3 "2017-07-06T02:52:23Z")

</div>


