# Querying for documents which do not contain an element in an array field

**URL:** <https://discuss.elastic.co/t/querying-for-documents-which-do-not-contain-an-element-in-an-array-field/4341>\
**Category:** Elasticsearch\
**Created:** [May 4, 2011, 9:20am UTC](https://discuss.elastic.co/t/querying-for-documents-which-do-not-contain-an-element-in-an-array-field/4341 "2011-05-04T09:20:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adam\_Warski](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Adam\_Warski](https://discuss.elastic.co/u/Adam_Warski)\
**Post date:** [May 4, 2011, 9:20am UTC](https://discuss.elastic.co/t/querying-for-documents-which-do-not-contain-an-element-in-an-array-field/4341/1 "2011-05-04T09:20:21Z")

</div>

Suppose I have a document with a "tags" field which is an array (just  
as here [http://www.elasticsearch.org/guide/reference/mapping/array-type.html](http://www.elasticsearch.org/guide/reference/mapping/array-type.html))

Is it possible to query for documents _do not_ contain a given tag?

Thanks,  
Adam

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [May 4, 2011, 9:27am UTC](https://discuss.elastic.co/t/querying-for-documents-which-do-not-contain-an-element-in-an-array-field/4341/2 "2011-05-04T09:27:22Z")

</div>

Hi Adam

On Wed, 2011-05-04 at 02:20 -0700, Adam Warski wrote:

> Suppose I have a document with a "tags" field which is an array (just  
> as here [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/mapping/array-type.html))
> 
> Is it possible to query for documents _do not_ contain a given tag?

curl -XGET '[http://127.0.0.1:9200/myindex/mytype/\_search?pretty=1](http://127.0.0.1:9200/myindex/mytype/_search?pretty=1)' -d '  
{  
"query" : {  
"constant\_score" : {  
"filter" : {  
"not" : {  
"filter" : {  
"term" : {  
"tags" : "foo"  
}  
}  
}  
}  
}  
}  
}  
'

---

<div class="post-metadata">

**Author:** ![Adam\_Warski](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Adam\_Warski](https://discuss.elastic.co/u/Adam_Warski)\
**Post date:** [May 4, 2011, 10:42am UTC](https://discuss.elastic.co/t/querying-for-documents-which-do-not-contain-an-element-in-an-array-field/4341/3 "2011-05-04T10:42:53Z")

</div>

Ah, so simple, thanks! 🙂

Adam

On May 4, 11:27 am, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:

> Hi Adam
> 
> On Wed, 2011-05-04 at 02:20 -0700, Adam Warski wrote:
> 
> > Suppose I have a document with a "tags" field which is an array (just  
> > as herehttp://www.elasticsearch.org/guide/reference/mapping/array-type.html)
> 
> > Is it possible to query for documents _do not_ contain a given tag?
> 
> curl -XGET '[http://127.0.0.1:9200/myindex/mytype/\_search?pretty=1](http://127.0.0.1:9200/myindex/mytype/_search?pretty=1)' -d '  
> {  
> "query" : {  
> "constant\_score" : {  
> "filter" : {  
> "not" : {  
> "filter" : {  
> "term" : {  
> "tags" : "foo"  
> }  
> }  
> }  
> }  
> }  
> }}
> 
> '

---

<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, 4:07am UTC](https://discuss.elastic.co/t/querying-for-documents-which-do-not-contain-an-element-in-an-array-field/4341/4 "2017-07-06T04:07:00Z")

</div>


