我希望公开灵活的REST服务,允许客户端执行任何查询。与elasticsearch API类似:
http://localhost:9200/blog/post/_search?q=user:dilbert
我找到了 ElasticsearchTemplate 及其方法 queryForList(StringQuery query,Class clazz)。它似乎符合我的目的。
@RequestMapping(value = "/search", method = RequestMethod.GET)
public List<DynamicEntity> findAllBySearch(@RequestParam("query") String query) {
return elasticsearchTemplate.queryForList(new StringQuery(query), DynamicEntity.class);
}
但我不知道如何构建查询。我无法找到相关文档。我试图猜测它没有成功。例如:
http://localhost:8080/dynamic/search?query={"query":{"className":"invoice"}}
className 是 DynamicEntity 的一个属性。
我收到以下错误:
org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase [dfs], all shards failed; shardFailures {[feu_3VGkTgKKUziW19mXUQ][dynamic][3]: SearchParseException[[dynamic][3]: from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query_binary":"eyJxdWVyeSI6eyJjbGFzc05hbWUiOiJpbnZvaWNlIn19"}]]]; nested: QueryParsingException[[dynamic] No query registered for [query]]; }{[feu_3VGkTgKKUziW19mXUQ][dynamic][4]: SearchParseException[[dynamic][4]: from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query_binary":"eyJxdWVyeSI6eyJjbGFzc05hbWUiOiJpbnZvaWNlIn19"}]]]; nested: QueryParsingException[[dynamic] No query registered for [query]]; }{[feu_3VGkTgKKUziW19mXUQ][dynamic][1]: SearchParseException[[dynamic][1]: from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query_binary":"eyJxdWVyeSI6eyJjbGFzc05hbWUiOiJpbnZvaWNlIn19"}]]]; nested: QueryParsingException[[dynamic] No query registered for [query]]; }{[feu_3VGkTgKKUziW19mXUQ][dynamic][2]: SearchParseException[[dynamic][2]: from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query_binary":"eyJxdWVyeSI6eyJjbGFzc05hbWUiOiJpbnZvaWNlIn19"}]]]; nested: QueryParsingException[[dynamic] No query registered for [query]]; }{[feu_3VGkTgKKUziW19mXUQ][dynamic][0]: SearchParseException[[dynamic][0]: from[0],size[10]: Parse Failure [Failed to parse source [{"from":0,"size":10,"query_binary":"eyJxdWVyeSI6eyJjbGFzc05hbWUiOiJpbnZvaWNlIn19"}]]]; nested: QueryParsingException[[dynamic] No query registered for [query]]; }
我正在使用正确的方法吗?如何构造有效的查询?我不想使用任何构建器类,因为查询应该在JavaScript中在客户端上构建。
答案 0 :(得分:2)
根据您发布的错误,您的示例查询无效(即No query registered for [query]
)。
您应该使用它(即使用term
代替query
):
query={"term":{"className":"invoice"}}