# Exists filter not working

**URL:** <https://discuss.elastic.co/t/exists-filter-not-working/6553>\
**Category:** Elasticsearch\
**Created:** [January 31, 2012, 12:59pm UTC](https://discuss.elastic.co/t/exists-filter-not-working/6553 "2012-01-31T12:59:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [January 31, 2012, 12:59pm UTC](https://discuss.elastic.co/t/exists-filter-not-working/6553/1 "2012-01-31T12:59:34Z")

</div>

Hi guys

My exists filter is not working for the array stucture. The query is as  
follows:

{  
"query": {  
"filtered": {  
"query": {  
"term": {  
"subtype": {  
"value": "izapregistereduser",  
"boost": 1  
}  
}  
},  
"filter": {  
"exists": {  
"field": "posts"  
}  
}  
}  
}  
}  
Here the posts is a nested array stucture,but if i run the following query,  
its giving the results:

{  
"query": {  
"filtered": {  
"query": {  
"term": {  
"subtype": {  
"value": "izapregistereduser",  
"boost": 1  
}  
}  
},  
"filter": {  
"exists": {  
"field": "guid"  
}  
}  
}  
}  
}

Here the guid can be a numeric or string value

Can anyone tell me the reason and solution plz???

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 31, 2012, 5:43pm UTC](https://discuss.elastic.co/t/exists-filter-not-working/6553/2 "2012-01-31T17:43:25Z")

</div>

What si posts? Is it an actual field that gets indexed? Can you _gist_ an example of the data you index?

On Tuesday, January 31, 2012 at 2:59 PM, Narinder Kaur wrote:

> Hi guys
> 
> My exists filter is not working for the array stucture. The query is as follows:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "term": {  
> "subtype": {  
> "value": "izapregistereduser",  
> "boost": 1  
> }  
> }  
> },  
> "filter": {  
> "exists": {  
> "field": "posts"  
> }  
> }  
> }  
> }  
> }  
> Here the posts is a nested array stucture,but if i run the following query, its giving the results:
> 
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "term": {  
> "subtype": {  
> "value": "izapregistereduser",  
> "boost": 1  
> }  
> }  
> },  
> "filter": {  
> "exists": {  
> "field": "guid"  
> }  
> }  
> }  
> }  
> }
> 
> Here the guid can be a numeric or string value
> 
> Can anyone tell me the reason and solution plz???

---

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [February 1, 2012, 6:01am UTC](https://discuss.elastic.co/t/exists-filter-not-working/6553/3 "2012-02-01T06:01:28Z")

</div>

```
       Its an nested array structure, that has some ids and related 

```

attributes to those ids. and I wanted to get only those documents which has  
this posts or non-empty posts . You can have an idea about its structure  
from the following example.

posts: [

```
{
    id: 195098
    can_reply: true
    owner_guid: 194988
    pushed_owner_guid: 57
    relations: null
}
{
    id: 195097
    can_reply: true
    owner_guid: 194988
    relations: [
        friend_of
        all
    ]
}
{
    id: 195096
    can_reply: true
    owner_guid: 194988
    relations: [
        friend_of
        all
    ]
}
{
    id: 195095
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195084
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195083
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195082
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195072
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195071
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195070
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195069
    can_reply: true
    owner_guid: 194988
    relations: [
        friend_of
        all
    ]
}
{
    id: 195059
    can_reply: true
    owner_guid: 194988
    pushed_owner_guid: 57
    relations: null
}
{
    id: null
    can_reply: true
    owner_guid: 194988
    pushed_owner_guid: 57
    relations: null
}
{
    id: null
    can_reply: true
    owner_guid: 194988
    pushed_owner_guid: 57
    relations: null
}
{
    id: 195034
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195033
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195032
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195031
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195028
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195027
    can_reply: true
    owner_guid: 57
    relations: []
}
{
    id: 195009
    can_reply: true
    owner_guid: 194988
    relations: [
        all
        follower_of
    ]
}
{
    id: 195008
    can_reply: true
    owner_guid: 194988
    relations: [
        all
        follower_of
    ]
}

```

]

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [February 1, 2012, 9:47am UTC](https://discuss.elastic.co/t/exists-filter-not-working/6553/4 "2012-02-01T09:47:50Z")

</div>

You can only do exists on a specific actual "core" field that has data, not on a json object, for example, on posts.id

On Wednesday, February 1, 2012 at 8:01 AM, Narinder Kaur wrote:

> ```
> Its an nested array structure, that has some ids and related attributes to those ids. and I wanted to get only those documents which has this posts or non-empty posts . You can have an idea about its structure from the following example.
> 
> ```
> 
> posts: [
> 
> ```
> {
> id: 195098
> can_reply: true
> owner_guid: 194988
> pushed_owner_guid: 57
> relations: null
> }
> {
> id: 195097
> can_reply: true
> owner_guid: 194988
> relations: [
> friend_of
> all
> ]
> }
> {
> id: 195096
> can_reply: true
> owner_guid: 194988
> relations: [
> friend_of
> all
> ]
> }
> {
> id: 195095
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195084
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195083
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195082
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195072
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195071
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195070
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195069
> can_reply: true
> owner_guid: 194988
> relations: [
> friend_of
> all
> ]
> }
> {
> id: 195059
> can_reply: true
> owner_guid: 194988
> pushed_owner_guid: 57
> relations: null
> }
> {
> id: null
> can_reply: true
> owner_guid: 194988
> pushed_owner_guid: 57
> relations: null
> }
> {
> id: null
> can_reply: true
> owner_guid: 194988
> pushed_owner_guid: 57
> relations: null
> }
> {
> id: 195034
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195033
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195032
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195031
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195028
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195027
> can_reply: true
> owner_guid: 57
> relations: []
> }
> {
> id: 195009
> can_reply: true
> owner_guid: 194988
> relations: [
> all
> follower_of
> ]
> }
> {
> id: 195008
> can_reply: true
> owner_guid: 194988
> relations: [
> all
> follower_of
> ]
> }
> 
> ```
> 
> ]

---

<div class="post-metadata">

**Author:** ![narinder\_izap](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/narinder_izap/32/803_2.png) [@narinder\_izap](https://discuss.elastic.co/u/narinder_izap)\
**Post date:** [February 1, 2012, 10:43am UTC](https://discuss.elastic.co/t/exists-filter-not-working/6553/5 "2012-02-01T10:43:01Z")

</div>

Thanks for the solution, its working now

---

<div class="post-metadata">

**Author:** ![MarcusLongmuir](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@MarcusLongmuir](https://discuss.elastic.co/u/MarcusLongmuir)\
**Post date:** [April 20, 2012, 8:48am UTC](https://discuss.elastic.co/t/exists-filter-not-working/6553/6 "2012-04-20T08:48:41Z")

</div>

I don't mean to hijack this thread, but I think I'm trying to achieve the  
same as the OP, but I have no alternatives other than to check whether or  
not a json object exists. For example the structure:

[  
{  
"User" : "Marcus",  
"Content" : {  
"Content" : "This is a post",  
"Title" : "The title of this post"  
},  
"Media" : {  
"Header" : "http://.../images/one.jpg"  
}  
},  
{  
"User" : "Marcus",  
"Content" : {  
"Content" : "This is another post, but without a header",  
"Title" : "The title of this other post"  
}  
}  
]

These objects are representative of the problem, but are not my structures.  
I need to be able to find all documents with a "Media" object.

As long as I can retrieve the objects in the above structure I am open to  
adding an array of the keys present as long as I can remove it before  
retrieval.

One workaround I was pondering was to use the filter on one of the core  
fields in the "Media" object, but the keys are subject to change so there  
isn't a single key which is present for all "Media" objects.

Is there a technical reason why the exists filter isn't implemented on  
objects?

---

<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, 3:31am UTC](https://discuss.elastic.co/t/exists-filter-not-working/6553/7 "2017-07-06T03:31:40Z")

</div>


