# Exists filter

**URL:** <https://discuss.elastic.co/t/exists-filter/15232>\
**Category:** Elasticsearch\
**Created:** [January 14, 2014, 12:48pm UTC](https://discuss.elastic.co/t/exists-filter/15232 "2014-01-14T12:48:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Reynolds](https://avatars.discourse-cdn.com/v4/letter/j/b2d939/32.png) [@James\_Reynolds](https://discuss.elastic.co/u/James_Reynolds)\
**Post date:** [January 14, 2014, 12:48pm UTC](https://discuss.elastic.co/t/exists-filter/15232/1 "2014-01-14T12:48:06Z")

</div>

I'm trying to run a query that looks sort of like this:

{  
"filter": {  
"term": {  
"b\_id": "6"  
},  
"constant\_score": {  
"filter": {  
"exists": {  
"field": "rear\_placement"  
}  
}  
}  
},  
"fields": [  
"rear\_placement",  
"price",  
"controller",  
"name",  
"short\_name",  
"nm"  
],  
"sort": [  
"rear\_placement"  
]  
}

When I add the "constant score" bit the "fields" do not return. Only  
\_source. Removing the constant score exists bit, the six fields above  
return without issue.

It isn't the end of the world for me to return the entire document in this  
case, I was just wondering what the deal is with this and if there is  
something I'm doing wrong.

James

--  
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/a06d4480-16bb-426e-b97a-467b2cd5bf9f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a06d4480-16bb-426e-b97a-467b2cd5bf9f%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 14, 2014, 6:22pm UTC](https://discuss.elastic.co/t/exists-filter/15232/2 "2014-01-14T18:22:45Z")

</div>

You would need to combine your two filters with a bool filter:

> **[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.

The constant score query should be at the top level and the exists/term  
filters should be combined with a bool filter underneath it. The constant  
score wrapper is not really necessary since your filters are not doing any  
scoring, so you can just skip it and use the bool filter.

Something like (not tested!)

POST phrase/test/\_search  
{  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"b\_id": "6"  
}  
},  
{  
"exists": {  
"field": "rear\_placement"  
}  
}  
]  
}  
},  
"fields": [  
"rear\_placement",  
"price",  
"controller",  
"name",  
"short\_name",  
"nm"  
],  
"sort": [  
"rear\_placement"  
]  
}

Cheers,

Ivan

On Tue, Jan 14, 2014 at 4:48 AM, James Reynolds [eire1130@gmail.com](mailto:eire1130@gmail.com) wrote:

> I'm trying to run a query that looks sort of like this:
> 
> {  
> "filter": {  
> "term": {  
> "b\_id": "6"  
> },  
> "constant\_score": {  
> "filter": {  
> "exists": {  
> "field": "rear\_placement"  
> }  
> }  
> }  
> },  
> "fields": [  
> "rear\_placement",  
> "price",  
> "controller",  
> "name",  
> "short\_name",  
> "nm"  
> ],  
> "sort": [  
> "rear\_placement"  
> ]  
> }
> 
> When I add the "constant score" bit the "fields" do not return. Only  
> \_source. Removing the constant score exists bit, the six fields above  
> return without issue.
> 
> It isn't the end of the world for me to return the entire document in this  
> case, I was just wondering what the deal is with this and if there is  
> something I'm doing wrong.
> 
> James
> 
> --  
> 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/a06d4480-16bb-426e-b97a-467b2cd5bf9f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a06d4480-16bb-426e-b97a-467b2cd5bf9f%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CALY%3DcQDdx\_i0WALXGNhFaUsQjsPzUuNERbJMtwW7uztSXVOuCw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQDdx_i0WALXGNhFaUsQjsPzUuNERbJMtwW7uztSXVOuCw%40mail.gmail.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, 1:56am UTC](https://discuss.elastic.co/t/exists-filter/15232/3 "2017-07-06T01:56:49Z")

</div>


