Java High Level RESRT Client API 入门级操作Elasticsearch

阅读: 评论:0

Java High Level RESRT Client API 入门级操作Elasticsearch

Java High Level RESRT Client API 入门级操作Elasticsearch

所用的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;@Autowiredprivate IHotelService hotelService;//创建索引库@Testvoid createHotelIndex() throws IOException {CreateIndexRequest request = new CreateIndexRequest("hotel");request.source(MAPPING_TEMPLATE, XContentType.JSON);client.indices().create(request, RequestOptions.DEFAULT);}//删除索引库@Testvoid deleteTest() throws IOException {DeleteIndexRequest request = new DeleteIndexRequest("hotel");client.indices().delete(request, RequestOptions.DEFAULT);}//查询索引库是否存在@Testvoid quarryTest() throws IOException {GetIndexRequest request = new GetIndexRequest("hotel");println(client.indices().exists(request, RequestOptions.DEFAULT));}@Testvoid testClient() {println(client);}@BeforeEachvoid setUp() {this.client = new RestHighLevelClient(RestClient.ate("192.168.100.101:9200")));}@AfterEachvoid tearDown() throws IOException {this.client.close();}
}

文档操作

@SpringBootTest
public class HotelDocumentTest {@Autowiredprivate IHotelService hotelService;private RestHighLevelClient client;@BeforeEachvoid setUp() {client = new RestHighLevelClient(RestClient.ate("192.168.100.101:9200")));}@AfterEachvoid tearDown() throws IOException {client.close();}//添加文档@Testvoid testAddDocument() throws IOException {//先从mysql数据库中查询hotel数据Hotel hotel = ById(38609L);//将hotel转换为hotelDocHotelDoc hotelDoc = new HotelDoc(hotel);IndexRequest request = new IndexRequest("hotel").Id().toString());request.JSONString(hotelDoc), XContentType.JSON);client.index(request, RequestOptions.DEFAULT);}//根据id查询文档@Testvoid getDocumentById() throws IOException {GetRequest getRequest = new GetRequest("hotel", "38609");GetResponse response = (getRequest, RequestOptions.DEFAULT);String json = SourceAsString();JSONObject jsonObject = JSON.parseObject(json);System.out.println(jsonObject);}//根据id删除文档@Testvoid deleteDocumentById() throws IOException {DeleteRequest request = new DeleteRequest("hotel", "38609");DeleteResponse delete = client.delete(request, RequestOptions.DEFAULT);}//根据id修改文档@Testvoid 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.JSONString(hotel), XContentType.JSON);
//        request.doc(
//                "price", "888",
//                "city", "千叶"
//        );UpdateResponse update = client.update(request, RequestOptions.DEFAULT);}@Testvoid 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().toString()).JSONString(hotelDoc), XContentType.JSON));}client.bulk(bulkRequest, RequestOptions.DEFAULT);}
}

本文发布于:2024-02-01 01:44:48,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170672309032955.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:JPA jpa Enty
标签:入门级   操作   Level   High   Java
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23