在 ElasticSearch 中使用 Java API 时如何从 JSON DSL 构造 QueryBuilder?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25859921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to construct QueryBuilder from JSON DSL when using Java API in ElasticSearch?
提问by Armstrongya
I'm using ElasticSearch as a search service in Spring Web project which using Transport Client to communicate with ES.
我在 Spring Web 项目中使用 ElasticSearch 作为搜索服务,该项目使用 Transport Client 与 ES 进行通信。
I'm wondering if there exists a method which can construct a QueryBuilder from a JSON DSL. for example, convert this bool query DSL JSON to a QueryBuilder.
我想知道是否存在可以从 JSON DSL 构造 QueryBuilder 的方法。例如,将此布尔查询 DSL JSON 转换为 QueryBuilder。
{
"query" : {
"bool" : {
"must" : { "match" : {"content" : "quick"},
"should": { "match": {"content" : "lazy"}
}
}
}
I need this method because I have to receive user's bool string input from web front-side, and parse this bool string to a QueryBuilder. However it not suit to use QueryBuilders.boolQuery().must(matchQB).should(shouldQB).must_not(mustNotQB)
. Because we may need several must or non must query.
我需要这个方法,因为我必须从 web 前端接收用户的 bool 字符串输入,并将这个 bool 字符串解析为 QueryBuilder。然而它不适合使用QueryBuilders.boolQuery().must(matchQB).should(shouldQB).must_not(mustNotQB)
. 因为我们可能需要几个 must 或 non must 查询。
If there exist a method can construct a QueryBuilder from JSON DSL or there exists alternative solutions, it will much easier.
如果存在可以从 JSON DSL 构造 QueryBuilder 的方法或存在替代解决方案,那将会容易得多。
PS:I have found two method which can wrap a DSL String to a QueryBuilder for ES search. One is WrapperQueryBuilder, see details here. http://javadoc.kyubu.de/elasticsearch/HEAD/org/elasticsearch/index/query/WrapperQueryBuilder.htmlAnother is QueryBuilders.wrapperQuery(String DSL).
PS:我发现了两种可以将 DSL 字符串包装到 QueryBuilder 以进行 ES 搜索的方法。一种是WrapperQueryBuilder,请在此处查看详细信息。http://javadoc.kyubu.de/elasticsearch/HEAD/org/elasticsearch/index/query/WrapperQueryBuilder.html另一个是QueryBuilders.wrapperQuery(字符串DSL)。
回答by bradvido
You can use QueryBuilders.wrapperQuery(jsonQueryString);
您可以使用 QueryBuilders.wrapperQuery(jsonQueryString);
回答by halfelf
You can use setQuery
, which can receive a json format string.
您可以使用setQuery
,它可以接收一个 json 格式的字符串。
/**
* Constructs a new search source builder with a raw search query.
*/
public SearchRequestBuilder setQuery(String query) {
sourceBuilder().query(query);
return this;
}
Note this: only part of the DSL is needed, the {"query": }
part is omitted, like this:
请注意:仅需要 DSL 的{"query": }
一部分,省略该部分,如下所示:
SearchResponse searchResponse = client.prepareSearch(indices).setQuery("{\"term\": {\"id\": 1}}").execute().actionGet();
回答by Sash
It might be worth investigating low level rest client. With this you can do:
可能值得研究低级休息客户端。有了这个,你可以:
RestClient esClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
Request request = new Request("POST", "/INDEX_NAME/_doc/_search");
request.setJsonEntity(yourJsonQueryString);
Response response = esClient.performRequest(request);
String jsonResponse = EntityUtils.toString(response.getEntity());