Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining analyzer while querying in elasticSearch

I am pretty new to elasticsearch and just need some clarification: Can we define a analyzer while querying the search server. I tried it with the "text" and "field" query and it works fine:

Query:

curl -XPOST http://localhost:9200/test/user/_search? -d '{ "query" : { "text" : {"_all" : {"query" :"Vaibhav","analyzer" : "lowercase_keyword" }} } }' 

Result:

{"took":144,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.10848885,"hits":{"_index":"test","_type":"user","_id":"1","_score":0.10848885, "_source" : {    "first_name": "Vaibhav",    "last_name":"saini",    "password":"pwd" 

But when I try to do the same thing with term/prefix/wildcard query I get the exception:

Query:

curl -XPOST http://localhost:9200/test/user/_search? -d '{ "query" : { "term" : {"_all" : {"query" :"Vaibhav","analyzer" : "lowercase_keyword" }} } }' 

Result:

{"error":"SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[kws9J6tbQtWCMNKBm3Gpkw][test][4]: SearchParseException[[test][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\"query\" : {\n\"term\" : {\"_all\" : {\"query\" :\"Vaibhav\",\"analyzer\" : \"lowercase_keyword\" }}\n}\n}]]]; nested: QueryParsingException[[test] [term] query does not support [query]]; }{[kws9J6tbQtWCMNKBm3Gpkw][test][1]: SearchParseException[[test][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\"query\" : {\n\"term\" : {\"_all\" : {\"query\" :\"Vaibhav\",\"analyzer\" : \"lowercase_keyword\" }}\n}\n}]]]; nested: QueryParsingException[[test] [term] query does not support [query]]; }{[kws9J6tbQtWCMNKBm3Gpkw][test][2]: SearchParseException[[test][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [\n{\n\"query\" : {\n\"term\" : {\"_all\" : {\"query\" :\"Vaibhav\",\"analyzer\" : \"lowercase_keyword\" }}\n}\n}]]]; nested: QueryParsingException[[test] [term] query does not support [query]]; }]","status":500} 

So is it like we can't define analayzers while querying the elasticsearch server for some type of queries and for others we can? If not, am I doing anything wrong?

Any help is greatly appreciated.

like image 411
nikhil tyagi Avatar asked Jul 25 '12 19:07

nikhil tyagi


1 Answers

The term, prefix, and wildcard queries expect the value specified in the query to be already analyzed.

like image 114
imotov Avatar answered Sep 23 '22 04:09

imotov