feat(新增): 查询文档操作

This commit is contained in:
bunny 2024-03-31 20:38:49 +08:00
parent 5d5a36b752
commit ad8fd9ed7b
2 changed files with 15 additions and 0 deletions

View File

@ -5,6 +5,8 @@ import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService; import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClient;
@ -49,4 +51,17 @@ public class HotelDocumentTest {
// 3. 发送请求 // 3. 发送请求
client.index(request, RequestOptions.DEFAULT); client.index(request, RequestOptions.DEFAULT);
} }
// 查询文档操作
@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));
}
} }