# Filtered query question

**URL:** <https://discuss.elastic.co/t/filtered-query-question/8051>\
**Category:** Elasticsearch\
**Created:** [June 12, 2012, 2:48am UTC](https://discuss.elastic.co/t/filtered-query-question/8051 "2012-06-12T02:48:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Warner\_Onstine](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warner_onstine/32/1086_2.png) [@Warner\_Onstine](https://discuss.elastic.co/u/Warner_Onstine)\
**Post date:** [June 12, 2012, 2:48am UTC](https://discuss.elastic.co/t/filtered-query-question/8051/1 "2012-06-12T02:48:00Z")

</div>

Ok, now that I finally have some stuff working I'm trying to figure  
out why my filtered query is returning 0 records.

Here's my query:

{"query": {  
"filtered" : {  
"query\_string" : {  
"fields": ["description", "title"],  
"query" : "Understanding"  
},  
"filter" : {  
"term" : {  
"docType" : "Event"  
}  
}  
}  
}  
}

If I remove the filter portion it returns the records (who have the  
docType fieild and the appropriate value of "Event").

Originally I didn't have the doctype in my mapping so I added it, but  
it didn't help :/.

Any ideas? Been searching but don't see a lot of examples on filtered  
queries on the web.

Thanks in advance.

-warner

---

<div class="post-metadata">

**Author:** ![Anne\_Veling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anne_veling/32/1260_2.png) [@Anne\_Veling](https://discuss.elastic.co/u/Anne_Veling)\
**Post date:** [June 12, 2012, 8:15am UTC](https://discuss.elastic.co/t/filtered-query-question/8051/2 "2012-06-12T08:15:01Z")

</div>

Can you give the json output of one of the documents you expect to be  
returned for this query? What is the exact value of the "docType" field for  
that document?  
Also, how is that field analyzed? Could it be that it has a  
StandardAnalyzer and is lowercased ("event")?

Anne

On Tue, Jun 12, 2012 at 4:48 AM, Warner Onstine [warnero@gmail.com](mailto:warnero@gmail.com) wrote:

> Ok, now that I finally have some stuff working I'm trying to figure  
> out why my filtered query is returning 0 records.
> 
> Here's my query:
> 
> {"query": {  
> "filtered" : {  
> "query\_string" : {  
> "fields": ["description", "title"],  
> "query" : "Understanding"  
> },  
> "filter" : {  
> "term" : {  
> "docType" : "Event"  
> }  
> }  
> }  
> }  
> }
> 
> If I remove the filter portion it returns the records (who have the  
> docType fieild and the appropriate value of "Event").
> 
> Originally I didn't have the doctype in my mapping so I added it, but  
> it didn't help :/.
> 
> Any ideas? Been searching but don't see a lot of examples on filtered  
> queries on the web.
> 
> Thanks in advance.
> 
> -warner

--  
Anne Veling  
[BeyondTrees.com](http://BeyondTrees.com)  
+31 6 50 969 170  
@anneveling

---

<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:** [June 12, 2012, 8:20am UTC](https://discuss.elastic.co/t/filtered-query-question/8051/3 "2012-06-12T08:20:04Z")

</div>

> ```
> "filter" : {
> "term" : {
> "docType" : "Event"
> }
> }
> }
> }
> 
> ```
> 
> }
> 
> If I remove the filter portion it returns the records (who have the  
> docType fieild and the appropriate value of "Event").

You're using a term query which means "find exactly 'Event'", but I bet  
you haven't mapped your docType field yourself, which means it is  
default to be "analyzed" which means that, what is stored, is actually  
'event', not 'Event'.

Set the mapping for that field to  
{ type: "string", index: "not\_analyzed"}

c

> Originally I didn't have the doctype in my mapping so I added it, but  
> it didn't help :/.
> 
> Any ideas? Been searching but don't see a lot of examples on filtered  
> queries on the web.
> 
> Thanks in advance.
> 
> -warner

---

<div class="post-metadata">

**Author:** ![Warner\_Onstine](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warner_onstine/32/1086_2.png) [@Warner\_Onstine](https://discuss.elastic.co/u/Warner_Onstine)\
**Post date:** [June 12, 2012, 1:44pm UTC](https://discuss.elastic.co/t/filtered-query-question/8051/4 "2012-06-12T13:44:16Z")

</div>

Here's a snippet:

{"took":17,"timed\_out":false,"\_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max\_score":null,"hits":}}Holden:logs  
warnero$ curl -XGET 'localhost:9200/syncrswim/\_search' -d  
'{"query":{"filtere"query\_string" : {"query" : "Teach","fields" : [  
"title", "description" ]}}}}}'  
{"took":37,"timed\_out":false,"\_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max\_score":0.18167211,"hits":[{"\_index":"syncrswim","\_type":"syncrswim","\_id":"3283712673","\_score":0.18167211,  
"\_source" : {"tags":["meditation"],  
"docType":"Event",  
"lastUpdated":"2012-05-13 14:30:36",

So, you can see docType is uppercase Event. I'm just using a default  
analyzer (no customizations).

-warner

On Tue, Jun 12, 2012 at 1:15 AM, Anne Veling [anne@beyondtrees.com](mailto:anne@beyondtrees.com) wrote:

> Can you give the json output of one of the documents you expect to be  
> returned for this query? What is the exact value of the "docType" field for  
> that document?  
> Also, how is that field analyzed? Could it be that it has a StandardAnalyzer  
> and is lowercased ("event")?
> 
> Anne
> 
> On Tue, Jun 12, 2012 at 4:48 AM, Warner Onstine [warnero@gmail.com](mailto:warnero@gmail.com) wrote:
> 
> > Ok, now that I finally have some stuff working I'm trying to figure  
> > out why my filtered query is returning 0 records.
> > 
> > Here's my query:
> > 
> > {"query": {  
> > "filtered" : {  
> > "query\_string" : {  
> > "fields": ["description", "title"],  
> > "query" : "Understanding"  
> > },  
> > "filter" : {  
> > "term" : {  
> > "docType" : "Event"  
> > }  
> > }  
> > }  
> > }  
> > }
> > 
> > If I remove the filter portion it returns the records (who have the  
> > docType fieild and the appropriate value of "Event").
> > 
> > Originally I didn't have the doctype in my mapping so I added it, but  
> > it didn't help :/.
> > 
> > Any ideas? Been searching but don't see a lot of examples on filtered  
> > queries on the web.
> > 
> > Thanks in advance.
> > 
> > -warner
> 
> --  
> Anne Veling  
> [BeyondTrees.com](http://BeyondTrees.com)  
> +31 6 50 969 170  
> @anneveling

---

<div class="post-metadata">

**Author:** ![Anne\_Veling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anne_veling/32/1260_2.png) [@Anne\_Veling](https://discuss.elastic.co/u/Anne_Veling)\
**Post date:** [June 12, 2012, 7:11pm UTC](https://discuss.elastic.co/t/filtered-query-question/8051/5 "2012-06-12T19:11:06Z")

</div>

If you're using the default analyzer, it is mapped to "event".  
You could verify that by changing the filter query to "event" and see if  
you get the document back.

Could you try changing the analyzer of the docType field to "not\_analyzed"?

Anne

On Tue, Jun 12, 2012 at 3:44 PM, Warner Onstine [warnero@gmail.com](mailto:warnero@gmail.com) wrote:

> Here's a snippet:
> 
> {"took":17,"timed\_out":false,"\_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max\_score":null,"hits":}}Holden:logs  
> warnero$ curl -XGET 'localhost:9200/syncrswim/\_search' -d  
> '{"query":{"filtere"query\_string" : {"query" : "Teach","fields" : [  
> "title", "description" ]}}}}}'
> 
> {"took":37,"timed\_out":false,"\_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max\_score":0.18167211,"hits":[{"\_index":"syncrswim","\_type":"syncrswim","\_id":"3283712673","\_score":0.18167211,  
> "\_source" : {"tags":["meditation"],  
> "docType":"Event",  
> "lastUpdated":"2012-05-13 14:30:36",
> 
> So, you can see docType is uppercase Event. I'm just using a default  
> analyzer (no customizations).
> 
> -warner
> 
> On Tue, Jun 12, 2012 at 1:15 AM, Anne Veling [anne@beyondtrees.com](mailto:anne@beyondtrees.com) wrote:
> 
> > Can you give the json output of one of the documents you expect to be  
> > returned for this query? What is the exact value of the "docType" field  
> > for  
> > that document?  
> > Also, how is that field analyzed? Could it be that it has a  
> > StandardAnalyzer  
> > and is lowercased ("event")?
> > 
> > Anne
> > 
> > On Tue, Jun 12, 2012 at 4:48 AM, Warner Onstine [warnero@gmail.com](mailto:warnero@gmail.com)  
> > wrote:
> > 
> > > Ok, now that I finally have some stuff working I'm trying to figure  
> > > out why my filtered query is returning 0 records.
> > > 
> > > Here's my query:
> > > 
> > > {"query": {  
> > > "filtered" : {  
> > > "query\_string" : {  
> > > "fields": ["description", "title"],  
> > > "query" : "Understanding"  
> > > },  
> > > "filter" : {  
> > > "term" : {  
> > > "docType" : "Event"  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > > 
> > > If I remove the filter portion it returns the records (who have the  
> > > docType fieild and the appropriate value of "Event").
> > > 
> > > Originally I didn't have the doctype in my mapping so I added it, but  
> > > it didn't help :/.
> > > 
> > > Any ideas? Been searching but don't see a lot of examples on filtered  
> > > queries on the web.
> > > 
> > > Thanks in advance.
> > > 
> > > -warner
> > 
> > --  
> > Anne Veling  
> > [BeyondTrees.com](http://BeyondTrees.com)  
> > +31 6 50 969 170  
> > @anneveling

--  
Anne Veling  
[BeyondTrees.com](http://BeyondTrees.com)  
+31 6 50 969 170  
@anneveling

---

<div class="post-metadata">

**Author:** ![Warner\_Onstine](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warner_onstine/32/1086_2.png) [@Warner\_Onstine](https://discuss.elastic.co/u/Warner_Onstine)\
**Post date:** [June 12, 2012, 9:51pm UTC](https://discuss.elastic.co/t/filtered-query-question/8051/6 "2012-06-12T21:51:25Z")

</div>

Moving it to lowercase did the trick thank you!

-warner

On Tue, Jun 12, 2012 at 12:11 PM, Anne Veling [anne@beyondtrees.com](mailto:anne@beyondtrees.com) wrote:

> If you're using the default analyzer, it is mapped to "event".  
> You could verify that by changing the filter query to "event" and see if you  
> get the document back.
> 
> Could you try changing the analyzer of the docType field to "not\_analyzed"?
> 
> Anne
> 
> On Tue, Jun 12, 2012 at 3:44 PM, Warner Onstine [warnero@gmail.com](mailto:warnero@gmail.com) wrote:
> 
> > Here's a snippet:
> > 
> > {"took":17,"timed\_out":false,"\_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max\_score":null,"hits":}}Holden:logs  
> > warnero$ curl -XGET 'localhost:9200/syncrswim/\_search' -d  
> > '{"query":{"filtere"query\_string" : {"query" : "Teach","fields" : [  
> > "title", "description" ]}}}}}'
> > 
> > {"took":37,"timed\_out":false,"\_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max\_score":0.18167211,"hits":[{"\_index":"syncrswim","\_type":"syncrswim","\_id":"3283712673","\_score":0.18167211,  
> > "\_source" : {"tags":["meditation"],  
> > "docType":"Event",  
> > "lastUpdated":"2012-05-13 14:30:36",
> > 
> > So, you can see docType is uppercase Event. I'm just using a default  
> > analyzer (no customizations).
> > 
> > -warner
> > 
> > On Tue, Jun 12, 2012 at 1:15 AM, Anne Veling [anne@beyondtrees.com](mailto:anne@beyondtrees.com) wrote:
> > 
> > > Can you give the json output of one of the documents you expect to be  
> > > returned for this query? What is the exact value of the "docType" field  
> > > for  
> > > that document?  
> > > Also, how is that field analyzed? Could it be that it has a  
> > > StandardAnalyzer  
> > > and is lowercased ("event")?
> > > 
> > > Anne
> > > 
> > > On Tue, Jun 12, 2012 at 4:48 AM, Warner Onstine [warnero@gmail.com](mailto:warnero@gmail.com)  
> > > wrote:
> > > 
> > > > Ok, now that I finally have some stuff working I'm trying to figure  
> > > > out why my filtered query is returning 0 records.
> > > > 
> > > > Here's my query:
> > > > 
> > > > {"query": {  
> > > > "filtered" : {  
> > > > "query\_string" : {  
> > > > "fields": ["description", "title"],  
> > > > "query" : "Understanding"  
> > > > },  
> > > > "filter" : {  
> > > > "term" : {  
> > > > "docType" : "Event"  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> > > > 
> > > > If I remove the filter portion it returns the records (who have the  
> > > > docType fieild and the appropriate value of "Event").
> > > > 
> > > > Originally I didn't have the doctype in my mapping so I added it, but  
> > > > it didn't help :/.
> > > > 
> > > > Any ideas? Been searching but don't see a lot of examples on filtered  
> > > > queries on the web.
> > > > 
> > > > Thanks in advance.
> > > > 
> > > > -warner
> > > 
> > > --  
> > > Anne Veling  
> > > [BeyondTrees.com](http://BeyondTrees.com)  
> > > +31 6 50 969 170  
> > > @anneveling
> 
> --  
> Anne Veling  
> [BeyondTrees.com](http://BeyondTrees.com)  
> +31 6 50 969 170  
> @anneveling

---

<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:24am UTC](https://discuss.elastic.co/t/filtered-query-question/8051/7 "2017-07-06T03:24:43Z")

</div>


