• 7RestClient操作索引库


    创建索引库

    DSL语句 :  PUT /索引库名

     

     代码分为三步:

    • 1)创建Request对象。因为是创建索引库的操作,因此Request是CreateIndexRequest。

    • 2)添加请求参数,其实就是DSL的JSON参数部分。因为json字符串很长,这里是定义了静态字符串常量MAPPING_TEMPLATE,让代码看起来更加优雅。

    • 3)发送请求,client.indices()方法的返回值是IndicesClient类型,封装了所有与索引库操作有关的方法。

     其中MAPPING_TEMPLATE的内容如下
    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" +
                "        \"copy_to\": \"all\"\n" +
                "      },\n" +
                "      \"starName\":{\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"business\":{\n" +
                "        \"type\": \"keyword\"\n" +
                "      },\n" +
                "      \"location\":{\n" +
                "        \"type\": \"geo_point\"\n" +
                "      },\n" +
                "      \"pic\":{\n" +
                "        \"type\": \"keyword\",\n" +
                "        \"index\": false\n" +
                "      },\n" +
                "      \"all\":{\n" +
                "        \"type\": \"text\",\n" +
                "        \"analyzer\": \"ik_max_word\"\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";
    }
    View Code

    单元测试代码如下

    @Test
    void createHotelIndex() throws IOException {
        // 1.创建Request对象
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        // 2.准备请求的参数:DSL语句
        request.source(MAPPING_TEMPLATE, XContentType.JSON);
        // 3.发送请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }
    View Code

    删除索引库

    DSL语句:DELETE /索引库名称

    与创建索引库相比:

    • 请求方式从PUT变为DELTE

    • 请求路径不变

    • 无请求参数

    所以代码的差异,注意体现在Request对象上。依然是三步走:

    • 1)创建Request对象。这次是DeleteIndexRequest对象

    • 2)准备参数。这里是无参

    • 3)发送请求。改用delete方法

    单元测试代码

    @Test
    void testDeleteHotelIndex() throws IOException {
        // 1.创建Request对象
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        // 2.发送请求
        client.indices().delete(request, RequestOptions.DEFAULT);
    }
    View Code

     判断索引库是否存在

    DSL语句:GET / 索引库名称

    因此与删除的Java代码流程是类似的。依然是三步走:

    • 1)创建Request对象。这次是GetIndexRequest对象

    • 2)准备参数。这里是无参

    • 3)发送请求。改用exists方法

    单元测试代码如下

    @Test
    void testExistsHotelIndex() throws IOException {
            // 1.创建Request对象
        GetIndexRequest request = new GetIndexRequest("hotel");
        // 2.发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        // 3.输出
        System.err.println(exists ? "索引库已经存在!" : "索引库不存在!");
    }
    View Code

    总结

    JavaRestClient操作elasticsearch的流程基本类似。核心是client.indices()方法来获取索引库的操作对象。

    索引库操作的基本步骤:

    • 初始化RestHighLevelClient

    • 创建XxxIndexRequest。XXX是Create、Get、Delete

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

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

  • 相关阅读:
    JDBC数据库访问操作的动态监测 之 p6spy
    ZeroC Ice启用SSL通讯的配置
    heartbeat在yum系发行版本的处理资料
    VisualStudio 调试Linux
    JVM之上的语言小集
    程序人生【一些经典的资料】
    读史知今、以史为鉴 【技术商业化】
    ipython notebook 浏览器中编写数学公式和现实
    大数据 云计算 等搜集的资料
    GO 1.5 代码编译安装 [centos7 64位]
  • 原文地址:https://www.cnblogs.com/liaowenhui/p/16537654.html
Copyright © 2020-2023  润新知