0
votes

I'm trying to use @query annotation in elasticsearch spring data to make complex query,but when I tried the following simple query I get error

@Query(value="{'bool':{'must':{'field':{'name':?0}}}}")
Segment findByName(String name);

Stack trace

[ERROR] com.innvo.aop.logging.LoggingAspect - Exception in 
com.innvo.web.rest.RouteResource.elastic() with cause = null and
exception {}
org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to
execute phase [dfs], all shards failed; shardFailures {[6JK9sflAQK6fSCuDr-
AgkQ][segment][0]: SearchParseException[[segment][0]: from[0],size[10]:  
Parse Failure [Failed to parse source         
[{"from":0,"size":10,"query_binary":
"eydib29sJzp7J211c3QnOlt7J3Rlcm0nOnsnaWQnOic1MjAwJ319XX19"}]]];
nested: QueryParsingException[[segment] Failed to parse]; nested: 
JsonParseException[Unexpected character (''' (code 39)): was expecting 
either valid name character (for unquoted name) or double-quote (for 
quoted) to start field name
at [Source: [B@4cc4c177; line: 1, column: 3]]; }{[6JK9sflAQK6fSCuDr-
AgkQ][segment][1]: SearchParseException[[segment][1]: from[0],size[10]: 
Parse Failure [Failed to parse source 
[{"from":0,"size":10,"query_binary":
"eydib29sJzp7J211c3QnOlt7J3Rlcm0nOnsnaWQnOic1MjAwJ319XX19"}]]]; nested:
QueryParsingException[[segment] Failed to parse];
nested:JsonParseException[Unexpected character (''' (code 39)): was 
expecting either valid name character (for unquoted name) or double-quote 
(for quoted) to start field name
at [Source: [B@31702d66; line: 1, column: 3]]; }{[6JK9sflAQK6fSCuDr- 
AgkQ][segment][2]: SearchParseException[[segment][2]: from[0],size[10]:
Parse Failure [Failed to parse source    
[{"from":0,"size":10,"query_binary":
"eydib29sJzp7J211c3QnOlt7J3Rlcm0nOnsnaWQnOic1MjAwJ319XX19"}]]]; nested: 
QueryParsingException[[segment] Failed to parse]; nested:
JsonParseException[Unexpected character (''' (code 39)): was expecting
either valid name character (for unquoted name) or double-quote (for
quoted) to start field name
1

1 Answers

3
votes

In the @Query annotation you simply need to use escaped double quotes instead of single quotes:

@Query(value="{\"bool\":{\"must\":{\"term\":{\"name\":\"?0\"}}}}")
Segment findByName(String name);

Also note that I've replaced field by term(or match is also possible) since field is not a valid query.