欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > 【Elasticsearch】04-RestAPI

【Elasticsearch】04-RestAPI

2025/11/22 22:12:54 来源:https://blog.csdn.net/qq_45722630/article/details/144180447  浏览:    关键词:【Elasticsearch】04-RestAPI

1. 引入依赖

<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

2. 覆盖SpringBoot中的版本

需要在父工程里面进行修改,防止版本覆盖。

  <properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><elasticsearch.version>7.12.1</elasticsearch.version></properties>

3. 初始化RestHighLevelClient

RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.150.101:9200")
));

4. 创建索引

    @Testvoid testCreate() {// 1. 准备 Request对象CreateIndexRequest request = new CreateIndexRequest("items");// 2. 准备请求参数request.source(MAPPING_TEMPLATE, XContentType.JSON);// 3. 发送请求try {restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}}

5. Mapping操作

    @Testvoid testCreate() {// 1. 准备 Request对象CreateIndexRequest request = new CreateIndexRequest("items");// 2. 准备请求参数request.source(MAPPING_TEMPLATE, XContentType.JSON);// 3. 发送请求try {restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}}@Testvoid testGet() {GetIndexRequest request = new GetIndexRequest("items");try {// GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists);} catch (IOException e) {throw new RuntimeException(e);}}@Testvoid testDelete() {DeleteIndexRequest request = new DeleteIndexRequest("items");try {restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}}

6. 文档操作

@SpringBootTest(properties = "spring.profiles.active=local")
public class ElasticDocumentTest {private RestHighLevelClient client;@Resourceprivate IItemService itemService;@BeforeEachpublic void init() {client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://nuaamvp.cn:9200")));}@AfterEachvoid tearDown() {if (client!=null) {try {client.close();} catch (IOException e) {throw new RuntimeException(e);}}}@Testvoid testIndexDoc() {Item item = itemService.getById(317578L);ItemDoc itemDoc = BeanUtil.copyProperties(item, ItemDoc.class);// 1. 创建对象IndexRequest request = new IndexRequest("items").id(itemDoc.getId());// 2. 准备请求参数request.source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);try {// 3. 发送请求client.index(request, RequestOptions.DEFAULT);} catch (IOException e) {throw new RuntimeException(e);}}@Testvoid testGetDoc() throws IOException {// 1. 准备请求对象GetRequest request = new GetRequest("items", "317578");// 2. 发送请求GetResponse response = client.get(request, RequestOptions.DEFAULT);// 3. 获取响应结果String json = response.getSourceAsString();ItemDoc itemDoc = JSONUtil.toBean(json, ItemDoc.class);System.out.println(itemDoc);}@Testvoid testDeleteDoc() throws IOException {DeleteRequest request = new DeleteRequest("items", "317578");client.delete(request, RequestOptions.DEFAULT);}@Testvoid testUpdateDoc() throws IOException {UpdateRequest request = new UpdateRequest("items", "317578");request.doc("price", 198,"commentCount", 128);client.update(request, RequestOptions.DEFAULT);}
}

7.数据批量导入

    @Testvoid testLoadItemDocs() throws IOException {// 分页查询商品数据int pageNo = 1;int size = 1000;while (true) {Page<Item> page = itemService.lambdaQuery().eq(Item::getStatus, 1).page(new Page<Item>(pageNo, size));// 非空检验List<Item> items = page.getRecords();if (CollUtils.isEmpty(items)) {return ;}log.info("加载第{}页数据,共{}条",pageNo, items.size());// 1. 创建请求对象BulkRequest request = new BulkRequest("items");for(Item item:items) {// 2.1 itemDocItemDoc itemDoc = BeanUtil.copyProperties(item, ItemDoc.class);request.add(new IndexRequest().id(itemDoc.getId()).source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON));}// 3. 发送请求client.bulk(request, RequestOptions.DEFAULT);// 翻页pageNo ++;}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词