2024-03-31 20:31:59 +08:00
|
|
|
|
package cn.itcast.hotel;
|
|
|
|
|
|
|
|
|
|
import cn.itcast.hotel.pojo.Hotel;
|
|
|
|
|
import cn.itcast.hotel.pojo.HotelDoc;
|
|
|
|
|
import cn.itcast.hotel.service.IHotelService;
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
import org.apache.http.HttpHost;
|
2024-03-31 20:38:49 +08:00
|
|
|
|
import org.elasticsearch.action.get.GetRequest;
|
|
|
|
|
import org.elasticsearch.action.get.GetResponse;
|
2024-03-31 20:31:59 +08:00
|
|
|
|
import org.elasticsearch.action.index.IndexRequest;
|
2024-03-31 20:48:07 +08:00
|
|
|
|
import org.elasticsearch.action.update.UpdateRequest;
|
2024-03-31 20:31:59 +08:00
|
|
|
|
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;
|
|
|
|
|
|
2024-03-31 20:48:07 +08:00
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
2024-03-31 20:31:59 +08:00
|
|
|
|
@SpringBootTest
|
|
|
|
|
public class HotelDocumentTest {
|
|
|
|
|
private RestHighLevelClient client;
|
|
|
|
|
@Autowired
|
|
|
|
|
private IHotelService hotelService;
|
|
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
void setup() throws Exception {
|
|
|
|
|
this.client = new RestHighLevelClient(RestClient.builder(
|
|
|
|
|
HttpHost.create("http://192.168.1.4:9200"))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterEach
|
|
|
|
|
void teardown() throws Exception {
|
|
|
|
|
this.client.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入文档,记得转换成JSON对象
|
|
|
|
|
@Test
|
|
|
|
|
void testAddDocument() throws Exception {
|
|
|
|
|
// 根据id查询酒店数据
|
|
|
|
|
Hotel hotel = hotelService.getById(61083L);
|
|
|
|
|
// 转换为文档类型,其中有经纬度转换
|
|
|
|
|
HotelDoc hotelDoc = new HotelDoc(hotel);
|
|
|
|
|
|
|
|
|
|
// 1. 准备Request对象
|
|
|
|
|
IndexRequest request = new IndexRequest("hotel").id(String.valueOf(hotelDoc.getId()));
|
|
|
|
|
// 2. 准备JSON文档
|
|
|
|
|
request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
|
|
|
|
|
// 3. 发送请求
|
|
|
|
|
client.index(request, RequestOptions.DEFAULT);
|
|
|
|
|
}
|
2024-03-31 20:38:49 +08:00
|
|
|
|
|
|
|
|
|
// 查询文档操作
|
|
|
|
|
@Test
|
|
|
|
|
void testGetDocuments() throws Exception {
|
|
|
|
|
// 准备Request
|
|
|
|
|
GetRequest request = new GetRequest("hotel", "61083");
|
|
|
|
|
// 发送请求
|
|
|
|
|
GetResponse response = client.get(request, RequestOptions.DEFAULT);
|
|
|
|
|
// 解析响应结果
|
|
|
|
|
String json = response.getSourceAsString();
|
|
|
|
|
|
|
|
|
|
System.out.println(JSON.parseObject(json, HotelDoc.class));
|
|
|
|
|
}
|
2024-03-31 20:48:07 +08:00
|
|
|
|
|
|
|
|
|
// 更新文档
|
|
|
|
|
@Test
|
|
|
|
|
void testUpdateDocument() throws IOException {
|
|
|
|
|
// 1. Request准备
|
|
|
|
|
UpdateRequest request = new UpdateRequest("hotel", "61083");
|
|
|
|
|
// 准备请求参数
|
|
|
|
|
request.doc(
|
|
|
|
|
"price", "666",
|
|
|
|
|
"startName", "四钻"
|
|
|
|
|
);
|
|
|
|
|
// 发送请求
|
|
|
|
|
client.update(request, RequestOptions.DEFAULT);
|
|
|
|
|
}
|
2024-03-31 20:31:59 +08:00
|
|
|
|
}
|