elasticsearch spring data @query注释

时间:2016-01-07 13:06:34

标签: elasticsearch spring-data spring-data-elasticsearch

我正在尝试在elasticsearch spring数据中使用@query注释来进行复杂查询,但是当我尝试以下简单查询时出现错误

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

堆栈跟踪

[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 个答案:

答案 0 :(得分:2)

@Query注释中,您只需使用转义双引号而不是单引号:

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

另请注意,我已将field替换为term(或match也可能),因为field不是有效查询。