@Query注释不适用于自定义弹性搜索查询

时间:2019-03-01 13:33:18

标签: spring-boot elasticsearch

我正在将Elasticsearch与java springboot java一起使用。当我使用curl运行查询时,它工作正常,但是当我尝试使用Elastic search存储库运行查询时,出现异常。

public interface UserRepository extends ElasticsearchRepository<ActionsElastic, String> {

@Query("{\"query\" : { \"bool\" : { \"must\" : [ { \"term\" : { \"userId\" : ?0 } }, { \"term\" : { \"actionType\" : \"?01\" } } ] } },\n" +
        "\t\"sort\": [ { \"bookmarkedAt\" : { \"order\" : \"?2\" } } ] }")
List<ActionsElastic> findByUserIdAndActionTypeAndActionFilterOrder(Long userId, String actionType, String order, Pageable pageable);

}

卷曲-

curl -X POST \
http://localhost:9200/actions/actions/_search \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: c06a825f-a9b2-49a8-9955-2d37cd2e3eae' \
-d '{"query" :  
{ "bool" : 
{ "must" : 
[ { "term" : 
{ "userId" : 1122 } }, { "term" : { "actionType" : "like" } } ] } 
},"sort": [ { "bookmarkedAt" : { "order" : "asc" } } ] }'

curl返回数据,但是在运行spring项目时我收到

  

原因:org.elasticsearch.common.ParsingException:没有为[查询]注册的[查询]

     在

  org.elasticsearch.index.query.QueryParseContext.parseInnerQueryBuilder(QueryParseContext.java:128)〜[na:na]
  在org.elasticsearch.index.query.WrapperQueryBuilder.doRewrite(WrapperQueryBuilder.java:167)〜[elasticsearch-6.4.2.jar:6.4.2]
  在org.elasticsearch.index.query.AbstractQueryBuilder.rewrite(AbstractQueryBuilder.java:263)〜[elasticsearch-6.4.2.jar:6.4.2]
  在org.elasticsearch.search.builder.SearchSourceBuilder.rewrite(SearchSourceBuilder.java:879)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.search.internal.ShardSearchLocalRequest.rewrite(ShardSearchLocalRequest.java:244)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.search.internal.ShardSearchTransportRequest.rewrite(ShardSearchTransportRequest.java:171)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.search.SearchService.createSearchContext(SearchService.java:530)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.search.SearchService.createContext(SearchService.java:479)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:461)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.search.SearchService.executeDfsPhase(SearchService.java:226)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.action.search.SearchTransportService $ 5.messageReceived(SearchTransportService.java:332)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.action.search.SearchTransportService $ 5.messageReceived(SearchTransportService.java:329)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:69)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.transport.TransportService $ 7.doRun(TransportService.java:662)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.common.util.concurrent.ThreadContext $ ContextPreservingAbstractRunnable.doRun(ThreadContext.java:675)〜[elasticsearch-6.4.2.jar:6.4.2]       在org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)〜[elasticsearch-6.4.2.jar:6.4.2]       ...省略了3个常见框架

注意-我不想通过Elasticsearch使用JAVA API

{
"timestamp": "2019-03-01T13:31:31.537+0000",
"status": 500,
"error": "Internal Server Error",
"message": "all shards failed",
"path": "/content-actions/v1/actions"
}

1 个答案:

答案 0 :(得分:0)

@Query批注中,您只需指定DSL查询的query结构内部的任何内容,即没有sortaggs等,基本上是这样的:

@Query("{ \"bool\" : { \"must\" : [ { \"term\" : { \"userId\" : ?0 } }, { \"term\" : { \"actionType\" : \"?01\" } } ] } }")
List<ActionsElastic> findByUserIdAndActionTypeAndActionFilterOrder(Long userId, String actionType, String order, Pageable pageable);