我正在从ES 1.7迁移到6.0,并且我试图找到一种方法来执行将简单查询传递给http://::1:8862的搜索请求。为了使查询易于阅读(和维护),我们希望使用Json字符串,而不是使用SearchSourceBuilder构建它们。这是可能的,但我现在没有找到一个好方法。
示例查询:
{
"filter": {
"bool": {
"must": [
{
"term": {
"status": "Success"
}
},
{
"term": {
"type": "someType"
}
},
{
"range": {
"endDateTime": {
"lte": "someDateTime"
}
}
}
]
}
}
}
示例代码:
val searchRequest = buildSearchRequest(indexName, indexType, query)
val searchResponse = restHighLevelClient.search(searchRequest)
我知道可以使用LowLevelClient发送查询但它返回的是Response而不是SearchResponse。以前有人遇到过这个问题吗?任何变通方法或解决方案?
答案 0 :(得分:3)
我有点晚了,但我对你的问题有一个很好的解决方案。
首先,您需要使用类似的东西构建查询:
private QueryBuilder buildQuery(MyObject object) {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
QueryBuilder statusTerm = QueryBuilders.termQuery("status", object.getStatus());
QueryBuilder typeTerm = QueryBuilders.termQuery("type", object.getType());
QueryBuilder boolQuery = QueryBuilders.boolQuery()
.must(statusTerm)
.must(typeTerm);
queryBuilder.should(boolQuery);
return queryBuilder;
}
然后,您无法进行搜索并返回SearchResponse:
private SearchResponse search(QueryBuilder query) throws IOException {
SearchRequest request = new SearchRequest(elasticSearchClient.getElasticsearchClientProperties().getIndices());
SearchSourceBuilder searchRequestBuilder = request.source();
searchRequestBuilder.query(query);
//SearchSourcebuilder allow you to set your query into your request
return this.elasticSearchClient.search(request);
}
最后可以有一个同时调用...的方法:
public void processSearch(MyObject object) {
QueryBuilder query = builQuery(object);
SearchResponse response = search(query);
List<FoundObjects> objects = parseResponse(response);
}
我希望这可以提供帮助。祝你好运!