# Re: Filter a query without a term

**URL:** <https://discuss.elastic.co/t/re-filter-a-query-without-a-term/4094>\
**Category:** Elasticsearch\
**Created:** [March 15, 2011, 7:23am UTC](https://discuss.elastic.co/t/re-filter-a-query-without-a-term/4094 "2011-03-15T07:23:30Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [March 15, 2011, 7:23am UTC](https://discuss.elastic.co/t/re-filter-a-query-without-a-term/4094/1 "2011-03-15T07:23:30Z")

</div>

Can you post a recreation ([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/help))? For filter, you should use a term filter for this, not a query wrapping filter.  
On Tuesday, March 15, 2011 at 2:07 AM, Rob Faraj wrote:

> I'm still relatively new to Elasticsearch and can't seem to figure out  
> if I'm doing this correctly. I am trying to produce a query that  
> searches for a given string. In addition to this query I also want to  
> filter the results off of an integer field in the document. I am using  
> the request below and its working...
> 
> curl -XPOST '[http://localhost:9200/index/type/\_search](http://localhost:9200/index/type/_search)?  
> pretty=true&fields=\_id,item\_id' -d '  
> {  
> "query" : {  
> "query\_string" : {  
> "query" : "foobar"  
> }  
> },  
> "filter" : {  
> "query" : {  
> "field": {  
> "status\_id" : "1"  
> }  
> }  
> }  
> }  
> '
> 
> Sometimes there is no term provided, but I would still like to filter  
> the results. I have substituted the term (foobar) for "\*" which I  
> guess matches everything. Is this a good approach for what I'm trying  
> to accomplish?
> 
> Thanks in advance.

---

<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:** [March 15, 2011, 3:11pm UTC](https://discuss.elastic.co/t/re-filter-a-query-without-a-term/4094/2 "2011-03-15T15:11:22Z")

</div>

Hi Rob

> [es-noterm.js · GitHub](https://gist.github.com/e251f1d3f0e17c2fa7ae)

I've posted a comment to your gist:

> <https://gist.github.com/robfaraj/e251f1d3f0e17c2fa7ae>

For the first query with a query string, you should use a filtered  
query, rather than using the top level filter param.

For the second query, without a query string, you can just wrap your  
filter in a constant\_score query and use it as shown

clint

---

<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:** [March 15, 2011, 11:22pm UTC](https://discuss.elastic.co/t/re-filter-a-query-without-a-term/4094/3 "2011-03-15T23:22:49Z")

</div>

You are putting the facets within the "query", it should eb on the same level as the "query" (json wise).  
On Wednesday, March 16, 2011 at 12:10 AM, Rob Faraj wrote:

> Thanks Clint. The constant\_score query worked great when there is no  
> term and I would like to filter by one field. However, I can't seem to  
> get facets to work with the constant\_score query.
> 
> I've updated the gist to be a reproduction of this (see the last curl  
> request). The odd thing is that the response comes back fine, but  
> there is no facet information in the response even though it is part  
> of the query object sent.
> 
> On Mar 15, 11:11 am, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:
> 
> > Hi Rob
> > 
> > > [es-noterm.js · GitHub](https://gist.github.com/e251f1d3f0e17c2fa7ae)
> > 
> > I've posted a comment to your gist:
> > 
> > [es-noterm.js · GitHub](https://gist.github.com/e251f1d3f0e17c2fa7ae#comments)
> > 
> > For the first query with a query string, you should use a filtered  
> > query, rather than using the top level filter param.
> > 
> > For the second query, without a query string, you can just wrap your  
> > filter in a constant\_score query and use it as shown
> > 
> > clint

---

<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:** [March 16, 2011, 7:57pm UTC](https://discuss.elastic.co/t/re-filter-a-query-without-a-term/4094/4 "2011-03-16T19:57:34Z")

</div>

Hi Rob

> Would someone mind taking a glance over the two different JSON docs I  
> have put together? They work as I have tested them, but I want to make  
> sure I'm doing things as simplistically efficient as possible.
> 
> [esbasics.js · GitHub](https://gist.github.com/f8a124e09faf61cff35b)

I've added comments showing how to make these queries more efficient. I  
left out the facets for simplicity.

clint

---

<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:** [March 16, 2011, 8:17pm UTC](https://discuss.elastic.co/t/re-filter-a-query-without-a-term/4094/5 "2011-03-16T20:17:22Z")

</div>

I approve Clinton enhancements :). One note, you don't need to specify in the fields \_id, since you always get it. Just provide an empty array in the fields, and it will not return the \_source.  
On Wednesday, March 16, 2011 at 9:57 PM, Clinton Gormley wrote:

> Hi Rob
> 
> > Would someone mind taking a glance over the two different JSON docs I  
> > have put together? They work as I have tested them, but I want to make  
> > sure I'm doing things as simplistically efficient as possible.
> > 
> > [esbasics.js · GitHub](https://gist.github.com/f8a124e09faf61cff35b)
> 
> I've added comments showing how to make these queries more efficient. I  
> left out the facets for simplicity.
> 
> clint

---

<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:** [March 16, 2011, 9:15pm UTC](https://discuss.elastic.co/t/re-filter-a-query-without-a-term/4094/6 "2011-03-16T21:15:01Z")

</div>

Those two queries are exactly the same. The field query is just a query\_string query with a nicer syntax and the default\_field is set to the field provided.  
On Wednesday, March 16, 2011 at 10:46 PM, Rob Faraj wrote:

> Thanks again. The 'terms' filter is exactly what I was looking for.  
> The support here is really fast and helpful. Speaks volumes to the  
> long term forecast of elasticsearch. Kudos to both.
> 
> I posted a follow up on gist with a question about the difference  
> between
> 
> "query" : {  
> "query\_string" : {  
> "query" : "foobar"  
> }  
> }
> 
> AND
> 
> "query" : {  
> "field" : {  
> "\_all" : "_foobar_"  
> }  
> }
> 
> On Mar 16, 3:57 pm, Clinton Gormley [clin...@iannounce.co.uk](mailto:clin...@iannounce.co.uk) wrote:
> 
> > Hi Rob
> > 
> > > Would someone mind taking a glance over the two different JSON docs I  
> > > have put together? They work as I have tested them, but I want to make  
> > > sure I'm doing things as simplistically efficient as possible.
> > 
> > > [esbasics.js · GitHub](https://gist.github.com/f8a124e09faf61cff35b)
> > 
> > I've added comments showing how to make these queries more efficient. I  
> > left out the facets for simplicity.
> > 
> > clint

---

<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:09am UTC](https://discuss.elastic.co/t/re-filter-a-query-without-a-term/4094/7 "2017-07-06T04:09:51Z")

</div>


