所用的es版本是8.2.3 由于8.0后es弃用了RESRT Client API 但是还能用 不过会报异常 可以不用管或者直接catch 不对结果有影响
依赖 直接用springboot默认的即可
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency><dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.71</version> </dependency>
索引库操作
public class HotelIndexTest {
private RestHighLevelClient client;
@Autowired
private IHotelService hotelService;
//创建索引库
@Test
void createHotelIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("hotel");
request.source(MAPPING_TEMPLATE, XContentType.JSON);
client.indices().create(request, RequestOptions.DEFAULT);
}
//删除索引库
@Test
void deleteTest() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
client.indices().delete(request, RequestOptions.DEFAULT);
}
//查询索引库是否存在
@Test
void quarryTest() throws IOException {
GetIndexRequest request = new GetIndexRequest("hotel");
System.err.println(client.indices().exists(request, RequestOptions.DEFAULT));
}
@Test
void testClient() {
System.err.println(client);
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.100.101:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
文档操作
@SpringBootTest
public class HotelDocumentTest {
@Autowired
private IHotelService hotelService;
private RestHighLevelClient client;
@BeforeEach
void setUp() {
client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.100.101:9200")
));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
//添加文档
@Test
void testAddDocument() throws IOException {
//先从mysql数据库中查询hotel数据
Hotel hotel = hotelService.getById(38609L);
//将hotel转换为hotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
client.index(request, RequestOptions.DEFAULT);
}
//根据id查询文档
@Test
void getDocumentById() throws IOException {
GetRequest getRequest = new GetRequest("hotel", "38609");
GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
String json = response.getSourceAsString();
JSONObject jsonObject = JSON.parseObject(json);
System.out.println(jsonObject);
}
//根据id删除文档
@Test
void deleteDocumentById() throws IOException {
DeleteRequest request = new DeleteRequest("hotel", "38609");
DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);
}
//根据id修改文档
@Test
void updateDocumentById() throws IOException {
UpdateRequest request = new UpdateRequest("hotel", "38609");
//用map修改
// Map map = new HashMap() {
// {
// put("price", "999");
// put("city", "东京");
// }
// };
// request.doc(map, XContentType.JSON);
//用对象方式创建
Hotel hotel = new Hotel() {{
setBrand("七天");
}};
request.doc(JSONObject.toJSONString(hotel), XContentType.JSON);
// request.doc(
// "price", "888",
// "city", "千叶"
// );
UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
}
@Test
void blukDoc() throws IOException {
List<Hotel> list = hotelService.list();
BulkRequest bulkRequest = new BulkRequest();
for (Hotel hotel : list) {
HotelDoc hotelDoc = new HotelDoc(hotel);
bulkRequest.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc), XContentType.JSON));
}
client.bulk(bulkRequest, RequestOptions.DEFAULT);
}
}