# Questions about analysis with TermQueryBuilder and PrefixQueryBuilder

**URL:** <https://discuss.elastic.co/t/questions-about-analysis-with-termquerybuilder-and-prefixquerybuilder/11193>\
**Category:** Elasticsearch\
**Created:** [March 18, 2013, 4:10pm UTC](https://discuss.elastic.co/t/questions-about-analysis-with-termquerybuilder-and-prefixquerybuilder/11193 "2013-03-18T16:10:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [March 18, 2013, 4:10pm UTC](https://discuss.elastic.co/t/questions-about-analysis-with-termquerybuilder-and-prefixquerybuilder/11193/1 "2013-03-18T16:10:26Z")

</div>

Input data:

{ "create" : { "\_index" : "fizzbuzz", "\_type" : "person", "\_id" :  
"7214560012" } }  
{ "telno" : "7214560012", "gn" : "Aurelio", "sn" : "Phzee", "o" :  
"Philadelphia Fizzies" }  
{ "create" : { "\_index" : "fizzbuzz", "\_type" : "person", "\_id" :  
"7214560013" } }  
{ "telno" : "7214560013", "gn" : "Ognit", "sn" : "Ferglaps", "o" :  
"Philadelphia Fizzies" }  
{ "create" : { "\_index" : "fizzbuzz", "\_type" : "person", "\_id" :  
"7214560014" } }  
{ "telno" : "7214560014", "gn" : "Bill", "sn" : "Barf", "o" : "Philadelphia  
Fizzies" }  
{ "create" : { "\_index" : "fizzbuzz", "\_type" : "person", "\_id" :  
"7214560015" } }  
{ "telno" : "7214560015", "gn" : "Chuck", "sn" : "Wagon", "o" : "Dallas  
Debbies" }

Explicitly added settings and mappings that were specified before any of  
the data was loaded:

{  
"settings" : {  
"index" : {  
"number\_of\_shards" : 1,  
"analysis" : {  
"char\_filter" : { },  
"filter" : {  
"english\_snowball\_filter" : {  
"type" : "snowball",  
"language" : "English"  
}  
},  
"analyzer" : {  
"english\_standard\_analyzer" : {  
"type" : "custom",  
"tokenizer" : "standard",  
"filter" : ["standard", "lowercase"]  
},  
"english\_stemming\_analyzer" : {  
"type" : "custom",  
"tokenizer" : "standard",  
"filter" : [ "standard", "lowercase", "english\_snowball\_filter"  
]  
}  
}  
}  
}  
},  
"mappings" : {  
"person" : {  
"\_all" : {  
"enabled" : false  
},  
"properties" : {  
"telno" : {  
"type" : "string",  
"analyzer" : "english\_standard\_analyzer"  
},  
"gn" : {  
"type" : "string",  
"analyzer" : "english\_standard\_analyzer"  
},  
"sn" : {  
"type" : "string",  
"analyzer" : "english\_stemming\_analyzer"  
},  
"o" : {  
"type" : "string",  
"analyzer" : "english\_stemming\_analyzer"  
}  
}  
}  
}  
}

To verify the mappings, the gn field is lowercased with no stop word  
removal, but is not stemmed:

$ curl -XGET '[http://localhost:9200/fizzbuzz/\_analyze?field=gn&pretty=true](http://localhost:9200/fizzbuzz/_analyze?field=gn&pretty=true)'  
-d "Bobby and Debbie"  
{  
"tokens" : [ {  
"token" : "bobby",  
"start\_offset" : 0,  
"end\_offset" : 5,  
"type" : "",  
"position" : 1  
}, {  
"token" : "and",  
"start\_offset" : 6,  
"end\_offset" : 9,  
"type" : "",  
"position" : 2  
}, {  
"token" : "debbie",  
"start\_offset" : 10,  
"end\_offset" : 16,  
"type" : "",  
"position" : 3  
} ]  
}

The "o" field is also lowercased with no stop filter, and is also stemmed  
using the English snowball analyzer:

$ curl -XGET '[http://localhost:9200/fizzbuzz/\_analyze?field=o&pretty=true](http://localhost:9200/fizzbuzz/_analyze?field=o&pretty=true)'  
-d "Bobby and Debbie"  
{  
"tokens" : [ {  
"token" : "bobbi",  
"start\_offset" : 0,  
"end\_offset" : 5,  
"type" : "",  
"position" : 1  
}, {  
"token" : "and",  
"start\_offset" : 6,  
"end\_offset" : 9,  
"type" : "",  
"position" : 2  
}, {  
"token" : "debbi",  
"start\_offset" : 10,  
"end\_offset" : 16,  
"type" : "",  
"position" : 3  
} ]  
}

A simple query of "sn" matching the value "PHZEE". The MatchQueryBuilder  
was used to match the value. The value is analyzed at query time the same  
way that it is analyzed at index build time, and the expected record is  
found:

$ curl -XGET '[http://localhost:9200/fizzbuzz/person/\_search?pretty=true](http://localhost:9200/fizzbuzz/person/_search?pretty=true)' -d'  
{  
"from" : 0,  
"size" : 20,  
"query" : {  
"match" : {  
"sn" : {  
"query" : "PHZEE",  
"type" : "boolean"  
}  
}  
},  
"version" : true,  
"explain" : false  
}'

{  
"took" : 0,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 1,  
"successful" : 1,  
"failed" : 0  
},  
"hits" : {  
"total" : 1,  
"max\_score" : 1.6931472,  
"hits" : [ {  
"\_index" : "fizzbuzz",  
"\_type" : "person",  
"\_id" : "7214560012",  
"\_version" : 1,  
"\_score" : 1.6931472, "\_source" : { "telno" : "7214560012", "gn" :  
"Aurelio", "sn" : "Phzee", "o" : "Philadelphia Fizzies" }  
} ]  
}  
}

Now a TermQueryBuilder was used, but it no longer matches PHZEE:

$ curl -XGET '[http://localhost:9200/fizzbuzz/person/\_search?pretty=true](http://localhost:9200/fizzbuzz/person/_search?pretty=true)' -d'  
{  
"from" : 0,  
"size" : 20,  
"query" : {  
"term" : {  
"sn" : "PHZEE"  
}  
},  
"version" : true,  
"explain" : false  
}'

{  
"took" : 0,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 1,  
"successful" : 1,  
"failed" : 0  
},  
"hits" : {  
"total" : 0,  
"max\_score" : null,  
"hits" : []  
}  
}

But it matches phzee since I lowercased the term:

$ curl -XGET '[http://localhost:9200/fizzbuzz/person/\_search?pretty=true](http://localhost:9200/fizzbuzz/person/_search?pretty=true)' -d'  
{  
"from" : 0,  
"size" : 20,  
"query" : {  
"term" : {  
"sn" : "phzee"  
}  
},  
"version" : true,  
"explain" : false  
}'  
{  
"took" : 1,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 1,  
"successful" : 1,  
"failed" : 0  
},  
"hits" : {  
"total" : 1,  
"max\_score" : 1.6931472,  
"hits" : [ {  
"\_index" : "fizzbuzz",  
"\_type" : "person",  
"\_id" : "7214560012",  
"\_version" : 1,  
"\_score" : 1.6931472, "\_source" : { "telno" : "7214560012", "gn" :  
"Aurelio", "sn" : "Phzee", "o" : "Philadelphia Fizzies" }  
} ]  
}  
}

Also, the following prefix query does not work. It's as if the prefix PHZ  
is not being analyzed:

$ curl -XGET '[http://localhost:9200/fizzbuzz/person/\_search?pretty=true](http://localhost:9200/fizzbuzz/person/_search?pretty=true)' -d'  
{  
"from" : 0,  
"size" : 20,  
"query" : {  
"prefix" : {  
"sn" : "PHZ"  
}  
},  
"version" : true,  
"explain" : false  
}'

{  
"took" : 1,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 1,  
"successful" : 1,  
"failed" : 0  
},  
"hits" : {  
"total" : 0,  
"max\_score" : null,  
"hits" : []  
}  
}

So I analyze it myself using the highly custom analyzer known as the Java  
String.toLowerCase method, and now it works fine:

$ curl -XGET '[http://localhost:9200/fizzbuzz/person/\_search?pretty=true](http://localhost:9200/fizzbuzz/person/_search?pretty=true)' -d'  
{  
"from" : 0,  
"size" : 20,  
"query" : {  
"prefix" : {  
"sn" : "phz"  
}  
},  
"version" : true,  
"explain" : false  
}'

{  
"took" : 1,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 1,  
"successful" : 1,  
"failed" : 0  
},  
"hits" : {  
"total" : 1,  
"max\_score" : 1.0,  
"hits" : [ {  
"\_index" : "fizzbuzz",  
"\_type" : "person",  
"\_id" : "7214560012",  
"\_version" : 1,  
"\_score" : 1.0, "\_source" : { "telno" : "7214560012", "gn" :  
"Aurelio", "sn" : "Phzee", "o" : "Philadelphia Fizzies" }  
} ]  
}  
}

I would expect that with snowball stemming, since Debbies is indexed as  
debi, the prefix "debbie" is not found. And I've verified this to be true.  
And since I always use the lowercase token filter, my use of  
String.toLowerCase is working as well as can be expected with stemming.

But the gn field is not stemmed, and the following prefix also returns no  
hits:

$ curl -XGET '[http://localhost:9200/fizzbuzz/person/\_search?pretty=true](http://localhost:9200/fizzbuzz/person/_search?pretty=true)' -d'  
{  
"from" : 0,  
"size" : 20,  
"query" : {  
"prefix" : {  
"gn" : "CHU"  
}  
},  
"version" : true,  
"explain" : false  
}'

Did I miss something in the documentation, or is neither a TermQueryBuilder  
nor a PrefixQueryBuilder analyzed by default, and must always have some  
form of explicit analysis?

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![brian\_yoder](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@brian\_yoder](https://discuss.elastic.co/u/brian_yoder)\
**Post date:** [March 18, 2013, 6:21pm UTC](https://discuss.elastic.co/t/questions-about-analysis-with-termquerybuilder-and-prefixquerybuilder/11193/2 "2013-03-18T18:21:46Z")

</div>

OK, I missed something. I stumbled across  
[http://stackoverflow.com/questions/11657505/defining-analyzer-while-querying-in-elasticsearchto](http://stackoverflow.com/questions/11657505/defining-analyzer-while-querying-in-elasticsearchto) find the answer.

--  
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).  
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, 2:46am UTC](https://discuss.elastic.co/t/questions-about-analysis-with-termquerybuilder-and-prefixquerybuilder/11193/3 "2017-07-06T02:46:00Z")

</div>


