# Filtered query not working

**URL:** <https://discuss.elastic.co/t/filtered-query-not-working/6684>\
**Category:** Elasticsearch\
**Created:** [February 13, 2012, 7:54am UTC](https://discuss.elastic.co/t/filtered-query-not-working/6684 "2012-02-13T07:54:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![T\_Vinod\_Gupta](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@T\_Vinod\_Gupta](https://discuss.elastic.co/u/T_Vinod_Gupta)\
**Post date:** [February 13, 2012, 7:54am UTC](https://discuss.elastic.co/t/filtered-query-not-working/6684/1 "2012-02-13T07:54:53Z")

</div>

Hi,  
I am a newbie to ES. Can someone tell, what i could be doing wrong in this  
query -

curl -XGET '[http://localhost:920/twitter/\_search?pretty=true](http://localhost:920/twitter/_search?pretty=true)' -d '  
{  
"fields" : ["name","screen\_name","followers\_count"],  
"query" : {  
"filtered" : {  
"query" : { "matchAll" : {} },  
"filter" : { "range" : {  
"followers\_count" : { "from" : 100, "to" : 10000 }  
}  
}  
}  
}  
}'  
{  
"took" : 12,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 0,  
"max\_score" : null,  
"hits" : []  
}

i have 2 elements in my index -  
curl -XGET '[http://localhost:920/twitter/\_search?pretty=true](http://localhost:920/twitter/_search?pretty=true)' -d '  
{  
"query" : { "matchAll" : {} } }'  
{  
"took" : 2,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 2,  
"max\_score" : 1.0,  
"hits" : [ {  
"\_index" : "twitter",  
"\_type" : "user",  
"\_id" : "kimchy",  
"\_score" : 1.0, "\_source" : { "name" : "Shay Banon" }  
}, {  
"\_index" : "twitter",  
"\_type" : "audience",  
"\_id" : "18142273",  
"\_score" : 1.0, "\_source" :  
{"twitter\_id":"2953","screen\_name":"Lamoureux71","name":"Charles  
Lamoureux","location":"San Clemente CA","bio":"Married father of twins,  
working in the automotive  
industry.","followers\_count":401,"friends\_count":854,"statuses\_count":836,"listed\_count":12}  
} ]  
}  
}

---

<div class="post-metadata">

**Author:** ![T\_Vinod\_Gupta](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@T\_Vinod\_Gupta](https://discuss.elastic.co/u/T_Vinod_Gupta)\
**Post date:** [February 13, 2012, 3:39pm UTC](https://discuss.elastic.co/t/filtered-query-not-working/6684/2 "2012-02-13T15:39:04Z")

</div>

I did some more investigation and it seems ES is treating my integer fields  
as string fields and attempting string filter/comparison. e.g. this query  
works fine -  
curl -XGET '[http://localhost:920/twitter/audience/\_search?pretty=true](http://localhost:920/twitter/audience/_search?pretty=true)' -d '  
{ query : { range : { followers\_count : { from : 100, to : 50000 } } } }'  
{  
"took" : 12,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 1,  
"max\_score" : 1.0,  
"hits" : [ {  
"\_index" : "twitter",  
"\_type" : "audience",  
"\_id" : "18142273",  
"\_score" : 1.0, "\_source" :  
{"twitter\_id":"2953","screen\_name":"Lamoureux71","name":"Charles  
Lamoureux","location":"San Clemente CA","bio":"Married father of twins,  
working in the automotive  
industry.","followers\_count":401,"friends\_count":854,"statuses\_count":836,"listed\_count":12}  
} ]  
}  
}

i am wondering where am i going wrong?  
this is how i added the entry to ES -  
IndexRequestBuilder irb = client.prepareIndex("twitter",  
"audience", "18142273")  
.setSource(jsonBuilder()  
.startObject()  
.field("twitter\_id", "2953")  
.field("screen\_name", "Lamoureux71")  
.field("name", "Charles Lamoureux")  
.field("location", "San Clemente CA")  
.field("bio", "Married father of twins, working in  
the automotive industry.")  
.field("followers\_count", 401)  
.field("friends\_count", 854)  
.field("statuses\_count", 836)  
.field("listed\_count", 12)  
.endObject()  
)  
.execute()  
.actionGet();

thanks

On Sun, Feb 12, 2012 at 11:54 PM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com)wrote:

> Hi,  
> I am a newbie to ES. Can someone tell, what i could be doing wrong in this  
> query -
> 
> curl -XGET '[http://localhost:920/twitter/\_search?pretty=true](http://localhost:920/twitter/_search?pretty=true)' -d '  
> {  
> "fields" : ["name","screen\_name","followers\_count"],  
> "query" : {  
> "filtered" : {  
> "query" : { "matchAll" : {} },  
> "filter" : { "range" : {  
> "followers\_count" : { "from" : 100, "to" : 10000 }  
> }  
> }  
> }  
> }  
> }'  
> {  
> "took" : 12,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 0,  
> "max\_score" : null,  
> "hits" :   
> }
> 
> i have 2 elements in my index -  
> curl -XGET '[http://localhost:920/twitter/\_search?pretty=true](http://localhost:920/twitter/_search?pretty=true)' -d '  
> {  
> "query" : { "matchAll" : {} } }'  
> {  
> "took" : 2,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 2,  
> "max\_score" : 1.0,  
> "hits" : [ {  
> "\_index" : "twitter",  
> "\_type" : "user",  
> "\_id" : "kimchy",  
> "\_score" : 1.0, "\_source" : { "name" : "Shay Banon" }  
> }, {  
> "\_index" : "twitter",  
> "\_type" : "audience",  
> "\_id" : "18142273",  
> "\_score" : 1.0, "\_source" :  
> {"twitter\_id":"2953","screen\_name":"Lamoureux71","name":"Charles  
> Lamoureux","location":"San Clemente CA","bio":"Married father of twins,  
> working in the automotive  
> industry.","followers\_count":401,"friends\_count":854,"statuses\_count":836,"listed\_count":12}  
> } ]  
> }  
> }

---

<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:** [February 13, 2012, 6:29pm UTC](https://discuss.elastic.co/t/filtered-query-not-working/6684/3 "2012-02-13T18:29:48Z")

</div>

Hi, you would need to post (use gist) your mappings to understand what  
exactly is going on.

Are you using dynamic mappings or did you predefine your mappings? If  
you are using dynamic mappings and the type was declared as a string  
in JSON (surrounded by quotes), then Elasticsearch will always treat  
that field as a string.

--  
Ivan

On Mon, Feb 13, 2012 at 7:39 AM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com) wrote:

> I did some more investigation and it seems ES is treating my integer fields  
> as string fields and attempting string filter/comparison. e.g. this query  
> works fine -  
> curl -XGET '[http://localhost:920/twitter/audience/\_search?pretty=true](http://localhost:920/twitter/audience/_search?pretty=true)' -d '  
> { query : { range : { followers\_count : { from : 100, to : 50000 } } } }'  
> {  
> "took" : 12,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 1,  
> "max\_score" : 1.0,  
> "hits" : [ {  
> "\_index" : "twitter",  
> "\_type" : "audience",  
> "\_id" : "18142273",  
> "\_score" : 1.0, "\_source" :  
> {"twitter\_id":"2953","screen\_name":"Lamoureux71","name":"Charles  
> Lamoureux","location":"San Clemente CA","bio":"Married father of twins,  
> working in the automotive  
> industry.","followers\_count":401,"friends\_count":854,"statuses\_count":836,"listed\_count":12}  
> } ]  
> }  
> }
> 
> i am wondering where am i going wrong?  
> this is how i added the entry to ES -  
> IndexRequestBuilder irb = client.prepareIndex("twitter", "audience",  
> "18142273")  
> .setSource(jsonBuilder()  
> .startObject()  
> .field("twitter\_id", "2953")  
> .field("screen\_name", "Lamoureux71")  
> .field("name", "Charles Lamoureux")  
> .field("location", "San Clemente CA")  
> .field("bio", "Married father of twins, working in  
> the automotive industry.")  
> .field("followers\_count", 401)  
> .field("friends\_count", 854)  
> .field("statuses\_count", 836)  
> .field("listed\_count", 12)  
> .endObject()  
> )  
> .execute()  
> .actionGet();
> 
> thanks
> 
> On Sun, Feb 12, 2012 at 11:54 PM, T Vinod Gupta [tvinod@readypulse.com](mailto:tvinod@readypulse.com)  
> wrote:
> 
> > Hi,  
> > I am a newbie to ES. Can someone tell, what i could be doing wrong in this  
> > query -
> > 
> > curl -XGET '[http://localhost:920/twitter/\_search?pretty=true](http://localhost:920/twitter/_search?pretty=true)' -d '  
> > {  
> > "fields" : ["name","screen\_name","followers\_count"],  
> > "query" : {  
> > "filtered" : {  
> > "query" : { "matchAll" : {} },  
> > "filter" : { "range" : {  
> > "followers\_count" : { "from" : 100, "to" : 10000 }  
> > }  
> > }  
> > }  
> > }  
> > }'  
> > {  
> > "took" : 12,  
> > "timed\_out" : false,  
> > "\_shards" : {  
> > "total" : 5,  
> > "successful" : 5,  
> > "failed" : 0  
> > },  
> > "hits" : {  
> > "total" : 0,  
> > "max\_score" : null,  
> > "hits" :   
> > }
> > 
> > i have 2 elements in my index -  
> > curl -XGET '[http://localhost:920/twitter/\_search?pretty=true](http://localhost:920/twitter/_search?pretty=true)' -d '  
> > {  
> > "query" : { "matchAll" : {} } }'  
> > {  
> > "took" : 2,  
> > "timed\_out" : false,  
> > "\_shards" : {  
> > "total" : 5,  
> > "successful" : 5,  
> > "failed" : 0  
> > },  
> > "hits" : {  
> > "total" : 2,  
> > "max\_score" : 1.0,  
> > "hits" : [ {  
> > "\_index" : "twitter",  
> > "\_type" : "user",  
> > "\_id" : "kimchy",  
> > "\_score" : 1.0, "\_source" : { "name" : "Shay Banon" }  
> > }, {  
> > "\_index" : "twitter",  
> > "\_type" : "audience",  
> > "\_id" : "18142273",  
> > "\_score" : 1.0, "\_source" :  
> > {"twitter\_id":"2953","screen\_name":"Lamoureux71","name":"Charles  
> > Lamoureux","location":"San Clemente CA","bio":"Married father of twins,  
> > working in the automotive  
> > industry.","followers\_count":401,"friends\_count":854,"statuses\_count":836,"listed\_count":12}  
> > } ]  
> > }  
> > }

---

<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 14, 2012, 2:06pm UTC](https://discuss.elastic.co/t/filtered-query-not-working/6684/4 "2012-02-14T14:06:18Z")

</div>

Yea, as Ivan suggested, make sure you did not, by mistake, indexed the first json in that index with a string for the numeric value, in this case, it will be used as a string. You can use the get mapping API to see what types are actually used for the dynamic mappings derived from your data.

On Monday, February 13, 2012 at 8:29 PM, Ivan Brusic wrote:

> Hi, you would need to post (use gist) your mappings to understand what  
> exactly is going on.
> 
> Are you using dynamic mappings or did you predefine your mappings? If  
> you are using dynamic mappings and the type was declared as a string  
> in JSON (surrounded by quotes), then Elasticsearch will always treat  
> that field as a string.
> 
> --  
> Ivan
> 
> On Mon, Feb 13, 2012 at 7:39 AM, T Vinod Gupta \<[tvinod@readypulse.com](mailto:tvinod@readypulse.com) ([mailto:tvinod@readypulse.com](mailto:tvinod@readypulse.com))\> wrote:
> 
> > I did some more investigation and it seems ES is treating my integer fields  
> > as string fields and attempting string filter/comparison. e.g. this query  
> > works fine -  
> > curl -XGET '[http://localhost:920/twitter/audience/\_search?pretty=true](http://localhost:920/twitter/audience/_search?pretty=true)' -d '  
> > { query : { range : { followers\_count : { from : 100, to : 50000 } } } }'  
> > {  
> > "took" : 12,  
> > "timed\_out" : false,  
> > "\_shards" : {  
> > "total" : 5,  
> > "successful" : 5,  
> > "failed" : 0  
> > },  
> > "hits" : {  
> > "total" : 1,  
> > "max\_score" : 1.0,  
> > "hits" : [ {  
> > "\_index" : "twitter",  
> > "\_type" : "audience",  
> > "\_id" : "18142273",  
> > "\_score" : 1.0, "\_source" :  
> > {"twitter\_id":"2953","screen\_name":"Lamoureux71","name":"Charles  
> > Lamoureux","location":"San Clemente CA","bio":"Married father of twins,  
> > working in the automotive  
> > industry.","followers\_count":401,"friends\_count":854,"statuses\_count":836,"listed\_count":12}  
> > } ]  
> > }  
> > }
> > 
> > i am wondering where am i going wrong?  
> > this is how i added the entry to ES -  
> > IndexRequestBuilder irb = client.prepareIndex("twitter", "audience",  
> > "18142273")  
> > .setSource(jsonBuilder()  
> > .startObject()  
> > .field("twitter\_id", "2953")  
> > .field("screen\_name", "Lamoureux71")  
> > .field("name", "Charles Lamoureux")  
> > .field("location", "San Clemente CA")  
> > .field("bio", "Married father of twins, working in  
> > the automotive industry.")  
> > .field("followers\_count", 401)  
> > .field("friends\_count", 854)  
> > .field("statuses\_count", 836)  
> > .field("listed\_count", 12)  
> > .endObject()  
> > )  
> > .execute()  
> > .actionGet();
> > 
> > thanks
> > 
> > On Sun, Feb 12, 2012 at 11:54 PM, T Vinod Gupta \<[tvinod@readypulse.com](mailto:tvinod@readypulse.com) ([mailto:tvinod@readypulse.com](mailto:tvinod@readypulse.com))\>  
> > wrote:
> > 
> > > Hi,  
> > > I am a newbie to ES. Can someone tell, what i could be doing wrong in this  
> > > query -
> > > 
> > > curl -XGET '[http://localhost:920/twitter/\_search?pretty=true](http://localhost:920/twitter/_search?pretty=true)' -d '  
> > > {  
> > > "fields" : ["name","screen\_name","followers\_count"],  
> > > "query" : {  
> > > "filtered" : {  
> > > "query" : { "matchAll" : {} },  
> > > "filter" : { "range" : {  
> > > "followers\_count" : { "from" : 100, "to" : 10000 }  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }'  
> > > {  
> > > "took" : 12,  
> > > "timed\_out" : false,  
> > > "\_shards" : {  
> > > "total" : 5,  
> > > "successful" : 5,  
> > > "failed" : 0  
> > > },  
> > > "hits" : {  
> > > "total" : 0,  
> > > "max\_score" : null,  
> > > "hits" :   
> > > }
> > > 
> > > i have 2 elements in my index -  
> > > curl -XGET '[http://localhost:920/twitter/\_search?pretty=true](http://localhost:920/twitter/_search?pretty=true)' -d '  
> > > {  
> > > "query" : { "matchAll" : {} } }'  
> > > {  
> > > "took" : 2,  
> > > "timed\_out" : false,  
> > > "\_shards" : {  
> > > "total" : 5,  
> > > "successful" : 5,  
> > > "failed" : 0  
> > > },  
> > > "hits" : {  
> > > "total" : 2,  
> > > "max\_score" : 1.0,  
> > > "hits" : [ {  
> > > "\_index" : "twitter",  
> > > "\_type" : "user",  
> > > "\_id" : "kimchy",  
> > > "\_score" : 1.0, "\_source" : { "name" : "Shay Banon" }  
> > > }, {  
> > > "\_index" : "twitter",  
> > > "\_type" : "audience",  
> > > "\_id" : "18142273",  
> > > "\_score" : 1.0, "\_source" :  
> > > {"twitter\_id":"2953","screen\_name":"Lamoureux71","name":"Charles  
> > > Lamoureux","location":"San Clemente CA","bio":"Married father of twins,  
> > > working in the automotive  
> > > industry.","followers\_count":401,"friends\_count":854,"statuses\_count":836,"listed\_count":12}  
> > > } ]  
> > > }  
> > > }

---

<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:39am UTC](https://discuss.elastic.co/t/filtered-query-not-working/6684/5 "2017-07-06T03:39:27Z")

</div>


