要查询Elasticsearch(ES)数据,您可以使用Java的Elasticsearch REST客户端或Java的Elasticsearch高级客户端。
使用Elasticsearch REST客户端:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.12.1</version>
</dependency>
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http")).build();
Request request = new Request("GET", "/your-index/_search");
request.setJsonEntity("{\"query\": {\"match_all\": {}}}");
Response response = restClient.performRequest(request);
String responseBody = EntityUtils.toString(response.getEntity());
System.out.println(responseBody);
使用Elasticsearch高级客户端:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest searchRequest = new SearchRequest("your-index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
for (SearchHit hit : hits) {
String sourceAsString = hit.getSourceAsString();
System.out.println(sourceAsString);
}
这是查询ES数据的基本示例。您可以根据自己的需求进行进一步的查询和结果处理。