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;
import org.assertj.core.data.Index;
import org.elasticsearch.action.bulk.BulkRequest;
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
class HotelDocumentTest {
private RestHighLevelClient client;
@Autowired
private IHotelService hotelService;
// @Test
// void testAddDocument() throws IOException {
// // 1.查询数据库hotel数据
// Hotel hotel = hotelService.getById(61083L);
// // 2.转换为HotelDoc
// HotelDoc hotelDoc = new HotelDoc(hotel);
// // 3.转JSON
// String json = JSON.toJSONString(hotelDoc);
//
// // 1.准备Request
// IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
// // 2.准备请求参数DSL,其实就是文档的JSON字符串
// request.source(json, XContentType.JSON);
// // 3.发送请求
// client.index(request, RequestOptions.DEFAULT);
// }
@Test
void testAddDocument() throws IOException{
//1查询数据库hotel数据
Hotel hotel = hotelService.getById(61083L);
//2转换为HotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
//3.转JSON
String json = JSON.toJSONString(hotelDoc);
//1.准备Request
IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
//2.准备请求参数DSL,其实就是文档JSON字符串
request.source(json,XContentType.JSON);
//3.发送i请求
client.index(request,RequestOptions.DEFAULT);
}
自动加载
@BeforeEach
void setUp() {
client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.64.140:9200")
));
}
@AfterEach
void tearDown() throws IOException {
client.close();
}
}