前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ElasticSearch Java 高级客户端如何操作索引?

ElasticSearch Java 高级客户端如何操作索引?

作者头像
江南一点雨
发布2021-01-28 14:57:08
1.1K0
发布2021-01-28 14:57:08
举报
文章被收录于专栏:玩转JavaEE

松哥原创的 Spring Boot 视频教程已经杀青,感兴趣的小伙伴戳这里-->Spring Boot+Vue+微人事视频教程


今天我们来继续看 ElasticSearch 中的 Java 高级客户端,来看看 RestHighLevelClient 如何操作索引?

索引基本操作:

索引别名管理:

以下是视频笔记:

注意,笔记只是视频内容的一个简要记录,因此笔记内容比较简单,完整的内容可以查看视频。

28.1.2 查询索引是否存在
代码语言:javascript
复制
public class HighLevelTest3 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        GetIndexRequest blog = new GetIndexRequest("blog2");
        boolean exists = client.indices().exists(blog, RequestOptions.DEFAULT);
        System.out.println("exists = " + exists);
        //关闭 client
        client.close();
    }
}
28.1.3 关闭/打开索引

关闭:

代码语言:javascript
复制
public class HighLevelTest4 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        CloseIndexRequest blog = new CloseIndexRequest("blog");
        CloseIndexResponse close = client.indices().close(blog, RequestOptions.DEFAULT);
        List<CloseIndexResponse.IndexResult> indices = close.getIndices();
        for (CloseIndexResponse.IndexResult index : indices) {
            System.out.println("index.getIndex() = " + index.getIndex());
        }
        //关闭 client
        client.close();
    }
}

打开:

代码语言:javascript
复制
public class HighLevelTest4 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        OpenIndexRequest blog = new OpenIndexRequest("blog");
        client.indices().open(blog, RequestOptions.DEFAULT);
        //关闭 client
        client.close();
    }
}
28.1.4 索引修改
代码语言:javascript
复制
public class HighLevelTest5 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        UpdateSettingsRequest request = new UpdateSettingsRequest("blog");
        request.settings(Settings.builder().put("index.blocks.write", true).build());
        client.indices().putSettings(request, RequestOptions.DEFAULT);
        //关闭 client
        client.close();
    }
}
28.1.5 克隆索引

被克隆的索引需要是只读索引,可以通过 28.1.4 小节中的方式设置索引为只读。

代码语言:javascript
复制
public class HighLevelTest6 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        ResizeRequest request = new ResizeRequest("blog2", "blog");
        client.indices().clone(request, RequestOptions.DEFAULT);
        //关闭 client
        client.close();
    }
}
28.1.6 查看索引
代码语言:javascript
复制
public class HighLevelTest7 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        GetSettingsRequest request = new GetSettingsRequest().indices("blog");
        //设置需要互殴去的具体的参数,不设置则返回所有参数
        request.names("index.blocks.write");
        GetSettingsResponse response = client.indices().getSettings(request, RequestOptions.DEFAULT);
        ImmutableOpenMap<String, Settings> indexToSettings = response.getIndexToSettings();
        System.out.println(indexToSettings);
        String s = response.getSetting("blog", "index.number_of_replicas");
        System.out.println(s);
        //关闭 client
        client.close();
    }
}
28.1.7 Refresh & Flush

Es 底层依赖 Lucene,而 Lucene 中有 reopen 和 commit 两种操作,还有一个特殊的概念叫做 segment。

Es 中,基本的存储单元是 shard,对应到 Lucene 上,就是一个索引,Lucene 中的索引由 segment 组成,每个 segment 相当于 es 中的倒排索引。每个 es 文档创建时,都会写入到一个新的 segment 中,删除文档时,只是从属于它的 segment 处标记为删除,并没有从磁盘中删除。

Lucene 中:

reopen 可以让数据搜索到,但是不保证数据被持久化到磁盘中。

commit 可以让数据持久化。

Es 中:

默认是每秒 refresh 一次(Es 中文档被索引之后,首先添加到内存缓冲区,refresh 操作将内存缓冲区中的数据拷贝到新创建的 segment 中,这里是在内存中操作的)。

flush 将内存中的数据持久化到磁盘中。一般来说,flush 的时间间隔比较久,默认 30 分钟。

代码语言:javascript
复制
public class HighLevelTest8 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        RefreshRequest request = new RefreshRequest("blog");
        client.indices().refresh(request, RequestOptions.DEFAULT);
        FlushRequest flushRequest = new FlushRequest("blog");
        client.indices().flush(flushRequest, RequestOptions.DEFAULT);
        //关闭 client
        client.close();
    }
}
28.1.9 索引别名

索引的别名类似于 MySQL 中的视图。

28.1.9.1 添加别名

添加一个普通的别名:

代码语言:javascript
复制
public class HighLevelTest9 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
        IndicesAliasesRequest.AliasActions aliasAction = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD);
        aliasAction.index("books").alias("books_alias");
        indicesAliasesRequest.addAliasAction(aliasAction);
        client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
        //关闭 client
        client.close();
    }
}

添加一个带 filter 的别名:

代码语言:javascript
复制
public class HighLevelTest9 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
        IndicesAliasesRequest.AliasActions aliasAction = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.ADD);
        aliasAction.index("books").alias("books_alias2").filter("{\"term\": {\"name\": \"java\"}}");
        indicesAliasesRequest.addAliasAction(aliasAction);
        client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
        //关闭 client
        client.close();
    }
}

现在,books 索引将存在两个别名,其中,books_alias2 自动过滤 name 中含有 java 的文档。

28.1.9.2 删除别名
代码语言:javascript
复制
public class HighLevelTest9 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        IndicesAliasesRequest indicesAliasesRequest = new IndicesAliasesRequest();
        IndicesAliasesRequest.AliasActions aliasAction = new IndicesAliasesRequest.AliasActions(IndicesAliasesRequest.AliasActions.Type.REMOVE);
        aliasAction.index("books").alias("books_alias");
        indicesAliasesRequest.addAliasAction(aliasAction);
        client.indices().updateAliases(indicesAliasesRequest, RequestOptions.DEFAULT);
        //关闭 client
        client.close();
    }
}

第二种移除方式:

代码语言:javascript
复制
public class HighLevelTest9 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        DeleteAliasRequest deleteAliasRequest = new DeleteAliasRequest("books", "books_alias2");
        client.indices().deleteAlias(deleteAliasRequest, RequestOptions.DEFAULT);
        //关闭 client
        client.close();
    }
}
28.1.9.3 判断别名是否存在
代码语言:javascript
复制
public class HighLevelTest9 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        GetAliasesRequest books_alias = new GetAliasesRequest("books_alias");
        //指定查看某一个索引的别名,不指定,则会搜索所有的别名
        books_alias.indices("books");
        boolean b = client.indices().existsAlias(books_alias, RequestOptions.DEFAULT);
        System.out.println(b);
        //关闭 client
        client.close();
    }
}
28.1.9.4 获取别名
代码语言:javascript
复制
public class HighLevelTest9 {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
                new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http"),
                new HttpHost("localhost", 9202, "http")
        ));
        GetAliasesRequest books_alias = new GetAliasesRequest("books_alias");
        //指定查看某一个索引的别名,不指定,则会搜索所有的别名
        books_alias.indices("books");
        GetAliasesResponse response = client.indices().getAlias(books_alias, RequestOptions.DEFAULT);
        Map<String, Set<AliasMetadata>> aliases = response.getAliases();
        System.out.println("aliases = " + aliases);
        //关闭 client
        client.close();
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 江南一点雨 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 28.1.2 查询索引是否存在
  • 28.1.3 关闭/打开索引
  • 28.1.4 索引修改
  • 28.1.5 克隆索引
  • 28.1.6 查看索引
  • 28.1.7 Refresh & Flush
  • 28.1.9 索引别名
    • 28.1.9.1 添加别名
      • 28.1.9.2 删除别名
        • 28.1.9.3 判断别名是否存在
          • 28.1.9.4 获取别名
          相关产品与服务
          云数据库 MySQL
          腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档