目录
快速入门
@SpringBootTest
class SearchfDocTest {
private RestHighLevelClient client;
//查询所有文档
@Test
public void testMatchAll() throws IOException {
//1. 准备请求对象
SearchRequest searchRequest = new SearchRequest("hotel");
//2. 准备DSL
searchRequest.source().query(QueryBuilders.matchAllQuery());
//3. 发送请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//4. 解析响应解锁
SearchHits hits = response.getHits();
//4.1 获取搜索到的总条数
long value = hits.getTotalHits().value;
System.out.println("共搜索到" + value + "条数据");
SearchHit[] hitsArr = hits.getHits();
for (SearchHit hit : hits) {
//获取文档soure
String sourceAsString = hit.getSourceAsString();
//反序列化
HotelDoc hotel = JSON.parseObject(sourceAsString, HotelDoc.class);
System.out.println(hotel);
}
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.184.152:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
标签:hits,RestClient,void,hotel,client,文档,ES
From: https://www.cnblogs.com/czzz/p/17738170.html