java结合es查询
时间: 2024-06-12 20:10:24 浏览: 66
Java结合ES查询可以使用ES官方提供的Java API,具体步骤如下:
1. 引入ES Java API的依赖包;
2. 创建ES客户端连接;
3. 构建查询条件;
4. 执行查询并处理结果。
以下是一个简单的示例代码:
```
// 引入依赖包
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.10.2</version>
</dependency>
// 创建ES客户端连接
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 构建查询条件
SearchRequest searchRequest = new SearchRequest("index_name");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("field_name", "query_text"));
searchRequest.source(searchSourceBuilder);
// 执行查询并处理结果
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
// 处理查询结果
}
// 关闭客户端连接
client.close();
```
阅读全文