{ "query": { "bool": { "must": [ { "match": { "object_id.keyword": "Eth-Trunk5.24860201" } } , { "range": { "kpi1": { "gt": "0" } } } ], "must_not": [ ], "should": [ ] } }, "from": 0, "size": 10, "sort": [ ], "aggs": { } } 转为java语言
时间: 2023-07-02 20:06:48 浏览: 130
以下是将该查询转换为Java语言的示例代码:
```java
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
// 构建bool查询
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 添加must条件
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("object_id.keyword", "Eth-Trunk5.24860201");
boolQuery.must(matchQuery);
// 添加range条件
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("kpi1").gt("0");
boolQuery.must(rangeQuery);
// 设置from和size
int from = 0;
int size = 10;
// 构建查询
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(boolQuery);
searchSourceBuilder.from(from);
searchSourceBuilder.size(size);
searchRequest.source(searchSourceBuilder);
// 发送查询请求并获取结果
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
```
其中,`client` 是 Elasticsearch 客户端对象,可以使用以下代码创建:
```java
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")
)
);
```
这里的地址和端口号需要根据实际情况进行修改。
阅读全文