Java 代码 查询elasticsearch 索引内容
时间: 2023-07-16 19:15:37 浏览: 98
你可以使用 Java High Level REST Client 来查询 Elasticsearch 索引内容。以下是一个简单的示例代码:
```java
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
SearchRequest searchRequest = new SearchRequest("your_index_name");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("your_field_name", "your_keyword"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
for (SearchHit hit : hits) {
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
System.out.println(sourceAsMap);
}
client.close();
```
这个代码将会查询名为 `your_index_name` 的索引中,包含关键字 `your_keyword` 的文档,并将查询结果输出到控制台。你需要将 `your_field_name` 替换为你想要匹配的字段名。
阅读全文