elasticsearch5-RestAPI操作


个人名片:

博主:酒徒ᝰ.
个人简介:沉醉在酒中,借着一股酒劲,去拼搏一个未来。
本篇励志:三人行,必有我师焉。

本项目基于B站黑马程序员Java《SpringCloud微服务技术栈》,SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式

【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】 点击观看

目录

四、RestAPI

ES官方提供了各种不同语言的客户端,用来操作ES。

这些客户端的本质就是组装DSL语句,通过http请求发送给ES。

官方文档地址:https://www.elastic.co/guide/en/elasticsearch/client/index.html

案例:利用JavaRestClient实现创建、删除索引库,判断索引库是否存在

  1. 准备工程
    导入项目
    导入mysql数据
    修改application.yaml中的url和password,改为自己数据库的位置和密码
  2. 创建索引库
    导入es依赖
xml 复制代码
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

更改es版本为我们当前使用的版本

xml 复制代码
<properties>
	<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>

初始化代码

java 复制代码
public class HotelIndexTest {
    private RestHighLevelClient client;

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.179.128:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
}

建立DSL的JSON参数部分

java 复制代码
package cn.itcast.hotel;

public class HotelConstants {
    public static final String MAPPING_TEMPLATE = "{\n" +
            "  \"mappings\": {\n" +
            "    \"properties\": {\n" +
            "      \"id\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"name\": {\n" +
            "        \"type\": \"text\",\n" +
            "        \"analyzer\": \"ik_max_word\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"address\": {\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"price\": {\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"score\": {\n" +
            "        \"type\": \"integer\"\n" +
            "      },\n" +
            "      \"brand\": {\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"city\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"starName\": {\n" +
            "        \"type\": \"keyword\"\n" +
            "      },\n" +
            "      \"business\": {\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"copy_to\": \"all\"\n" +
            "      },\n" +
            "      \"location\": {\n" +
            "        \"type\": \"geo_point\"\n" +
            "      },\n" +
            "      \"pic\": {\n" +
            "        \"type\": \"keyword\",\n" +
            "        \"index\": false\n" +
            "      },\n" +
            "      \"all\": {\n" +
            "        \"type\": \"text\",\n" +
            "        \"index\": true,\n" +
            "        \"analyzer\": \"ik_max_word\"\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
}

发送请求

java 复制代码
@Test
void testCreateHotelIndex() throws IOException {
    CreateIndexRequest request = new CreateIndexRequest("hotel");
    request.source(MAPPING_TEMPLATE, XContentType.JSON);
    client.indices().create(request, RequestOptions.DEFAULT);
}
  1. 删除索引库
java 复制代码
@Test
void testDeleteHotelIndex() throws IOException {
    DeleteIndexRequest request = new DeleteIndexRequest("hotel");
    client.indices().delete(request, RequestOptions.DEFAULT);
}
  1. 判断索引库是否存在
java 复制代码
@Test
void testExitsHotelIndex() throws IOException {
    GetIndexRequest request = new GetIndexRequest("hotel");
    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    System.out.println(exists);
}

总结:

JavaRestClient操作elasticsearch的流程基本类似。

核心是client.indices()方法来获取索引库的操作对象。

索引库操作的基本步骤:

初始化RestHighLevelClient

创建XxxIndexRequest。XXX是Create、Get、Delete

准备DSL( Create时需要,其它是无参)

发送请求。调用RestHighLevelClient#indices().xxx()方法,xxx是create、exists、delete

相关推荐
mit6.8242 小时前
[es自动化更新] Updatecli编排配置.yaml | dockerfilePath值文件.yml
大数据·elasticsearch·搜索引擎·自动化
Jinkxs2 小时前
Elasticsearch 简介
大数据·elasticsearch·搜索引擎
亮学长3 小时前
lodash不支持 Tree Shaking 而 lodash-es可以
大数据·前端·elasticsearch
risc1234564 小时前
Elasticsearch 线程池
java·大数据·elasticsearch
jiuweiC9 小时前
spark3 streaming 读kafka写es
elasticsearch·kafka·linq
GeminiJM16 小时前
Elasticsearch混合搜索深度解析(上):问题发现与源码探索
大数据·elasticsearch·jenkins
winds~1 天前
【Git】git的回退功能
大数据·git·elasticsearch
chevysky.cn1 天前
Elasticsearch部署和集成
大数据·elasticsearch·jenkins
SelectDB技术团队1 天前
森马服饰从 Elasticsearch 到阿里云 SelectDB 的架构演进之路
elasticsearch·阿里云·doris
Elasticsearch1 天前
Elastic 被评为 2025 年 Gartner® 可观测平台魔力象限™中的领导者
elasticsearch