RestClient操作文档
初始化RestClient
在elasticsearch提供的API中,与elasticsearch一切交互都封装在一个名为RestHighLevelClient的类中,必须先完成这个对象的初始化,建立与elasticsearch的连接。
分为三步:
1)引入es的RestHighLevelClient依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
2)因为SpringBoot默认的ES可能版本不一致,所以我们需要覆盖默认的ES版本:
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
3)初始化RestHighLevelClient:
初始化的代码如下:
//可以使用配置类交给ioc
@Configuration
public class ESConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
return new RestHighLevelClient(
RestClient.builder(
new HttpHost("00.0xx.x0x.xxx", 9200, "http")
));
}
}
索引库相关操作
@SpringBootTest
class HotelIndexTest {
@Autowired
private RestHighLevelClient client;
//创建索引库
@Test
void testCreateIndex() throws Exception {
// 1.准备Request PUT /hotel
CreateIndexRequest request = new CreateIndexRequest("hotel");
// 2.准备请求参数
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 3.发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
//删除索引库
@Test
void testDeleteIndex() throws IOException {
// 1.准备Request
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
// 2.判断存不存在
GetIndexRequest indexRequest = new GetIndexRequest("hotel");
boolean exists = client.indices().exists(indexRequest, RequestOptions.DEFAULT);
if (!exists) {
System.out.println("不存在哦!");
return;
}
// 3.发送请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
//判断是否存在
@Test
void testGetIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("hotel");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists ? "存在" : "不存在");
}
}
Document文档操作
@SpringBootTest
class HotelDocumentTest {
@Autowired
private RestHighLevelClient client;
@Autowired
private IHotelService hotelService;
@Test
void testAddDoc() throws IOException {
//1.查询数据库hotel数据
Hotel hotel = hotelService.getById(61083);
//2.转换为HotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
//3.转换成json
String jsonString = JSONObject.toJSONString(hotelDoc);
//4.准备request
IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
//5.这是发送的文档
request.source(jsonString, XContentType.JSON);
client.index(request, RequestOptions.DEFAULT);
}
@Test
void testGetDoc() throws IOException {
//1.准备Request
GetRequest request = new GetRequest("hotel").id("61083");
//2.发送请求
GetResponse response = client.get(request, RequestOptions.DEFAULT);
//3.解析响应
String source = response.getSourceAsString();
//json转换成对象
HotelDoc hotelDoc = JSON.parseObject(source, HotelDoc.class);
System.out.println(hotelDoc);
}
@Test
void testDeleteDoc() throws IOException {
DeleteRequest request = new DeleteRequest("hotel", "61083");
client.delete(request, RequestOptions.DEFAULT);
}
@Test
void testUpdateDoc() throws IOException {
UpdateRequest request = new UpdateRequest("hotel", "61083");
request.doc(
"price", "999"
);
client.update(request, RequestOptions.DEFAULT);
}
@Test
void testBulkNewRequest() throws IOException {
//分页打包添加
int page = 1;
int size = 100;
Page<Hotel> hotelPage = hotelService.page(new Page<>(page, size));
for (long i = 1; i <= hotelPage.getPages(); i++) {
Page<Hotel> hotelData = hotelService.page(new Page<>(i, size));
BulkRequest bulkRequest = new BulkRequest();
for (Hotel hotel : hotelData.getRecords()) {
HotelDoc hotelDoc = new HotelDoc(hotel);
String hotelString = JSONObject.toJSONString(hotelDoc);
bulkRequest.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(hotelString, XContentType.JSON));
}
client.bulk(bulkRequest, RequestOptions.DEFAULT);
}
}
}