要查詢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數據的基本示例。您可以根據自己的需求進行進一步的查詢和結果處理。