新增文档、查询文档、删除文档、修改文档
package cn.itcast.hotel;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.impl.HotelService;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.List;
@SpringBootTest
public class ElasticsearchDocumentTest {
private RestHighLevelClient client;
@Autowired
private HotelService service;
@BeforeEach
public void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://8.130.89.67:9200")
)
);
}
// TODO 添加数据
@Test
public void testIndexDocument() throws IOException {
List<Hotel> list = service.list();
for (Hotel hotel : list) {
// 1.创建request对象
IndexRequest request=new IndexRequest("hotel").id(hotel.getId().toString());
HotelDoc hotelDoc=new HotelDoc(hotel);
// 2.准备JSON文档
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
// 3.发送请求
client.index(request, RequestOptions.DEFAULT);
}
}
// TODO 查询数据
@Test
public void testGetDocument() throws IOException {
List<Hotel> list = service.list();
for (Hotel hotel : list) {
GetRequest request=new GetRequest("hotel",hotel.getId().toString());
GetResponse response=client.get(request,RequestOptions.DEFAULT);
String source = response.getSourceAsString();
System.out.println(source);
}
}
// TODO 修改数据
@Test
public void testUpdateDocument() throws IOException {
// 1.创建request对象
UpdateRequest request=new UpdateRequest("hotel","36934");
request.doc(
"city","下海"
);
// 2.删除文档
client.update(request,RequestOptions.DEFAULT);
}
// TODO 删除数据
@Test
public void testDeleteDocument() throws IOException {
DeleteRequest request=new DeleteRequest("hotel","36934");
client.delete(request,RequestOptions.DEFAULT);
}
@AfterEach
public void tearDown() throws IOException {
this.client.close();
}
}
询到的文档数据是json,需要反序列化为java对象。
局部更新。只更新部分字段
总结
初始化RestHighLevelClient
创建XxxRequest。XXX是Index、Get、Update、Delete
准备参数(Index和Update时需要)插入和更新
发送请求。调用RestHighLevelClient#.xxx()方法,xxx是index、get、update、delete
解析结果(Get时需要
批量导入文档
// TODO 批量添加数据
@Test
public void testListDocument() throws IOException {
// 1.创建Bulk请求
BulkRequest request=new BulkRequest();
List<Hotel> hotels = service.list();
for (Hotel hotel : hotels) {
HotelDoc hotelDoc=new HotelDoc(hotel);
// 2.添加要批量提交的请求:这里添加了两个新增文档的请求
request
.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc),XContentType.JSON));
}
// 3.发起bulk请求
client.bulk(request,RequestOptions.DEFAULT);
}