# Query based on array item

**URL:** https://discuss.elastic.co/t/query-based-on-array-item/127764
**Category:** Elasticsearch
**Created:** [April 12, 2018, 8:32am UTC](https://discuss.elastic.co/t/query-based-on-array-item/127764 "2018-04-12T08:32:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![cassiopeia](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@cassiopeia](https://discuss.elastic.co/u/cassiopeia)
#### Post date: [April 12, 2018, 8:32am UTC](https://discuss.elastic.co/t/query-based-on-array-item/127764/1 "2018-04-12T08:32:58Z")

</div>

Hi,

My document looks like this

```
{  

```

"branchCode":"WA-0104556",  
"items":[  
{  
"id":"CAR-0014",  
"qty":4,  
"category":"PRIMARY"  
},  
{  
"id":"CAR-0015",  
"qty":5,  
"category":"PRIMARY"  
},  
{  
"id":"CAR-0016",  
"qty":4,  
"category":"MISCELLANNEOUS"  
}  
]  
}

how do I query for something like:

1. Get me all documents whose NONE of its items' "category" is PRIMARY
2. Get me all documents whose ANY of its items' "category" is MISCELLANEOUS

is there any special way when uploading the document to ES? what's the datatype and index type for the field "items"? The document structure shown above is structure in DB, not ES.  
ES index is not yet created for this one.

thanks

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [April 12, 2018, 9:09am UTC](https://discuss.elastic.co/t/query-based-on-array-item/127764/2 "2018-04-12T09:09:45Z")

</div>

Nothing special to do.

Just index it.

Then search with a bool query

Must not clause for the first case  
Filter clause for the second one

---

<div class="post-metadata">

### Author: ![cassiopeia](https://avatars.discourse-cdn.com/v4/letter/c/6de8d8/32.png) [@cassiopeia](https://discuss.elastic.co/u/cassiopeia)
#### Post date: [April 12, 2018, 9:22am UTC](https://discuss.elastic.co/t/query-based-on-array-item/127764/3 "2018-04-12T09:22:02Z")

</div>

dang, I forgot about the "must not" clause...  
feel like an idiot here...  
anyway, thanks a lot

---

<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: [May 10, 2018, 9:22am UTC](https://discuss.elastic.co/t/query-based-on-array-item/127764/4 "2018-05-10T09:22:32Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
