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

2年前 (2022) 程序员胖胖胖虎阿
299 0 0

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


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

索引基本操作:

索引别名管理:

以下是视频笔记:

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

28.1.2 查询索引是否存在

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 关闭/打开索引

关闭:

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();
    }
}

打开:

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 索引修改

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 小节中的方式设置索引为只读。

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 查看索引

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 分钟。

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 添加别名

添加一个普通的别名:

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 的别名:

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 删除别名
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();
    }
}

第二种移除方式:

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 判断别名是否存在
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 获取别名
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();
    }
}

ElasticSearch 基础知识:

  1. 打算出一个 ElasticSearch 教程,谁赞成,谁反对?
  2. ElasticSearch 从安装开始
  3. ElasticSearch 第三弹,核心概念介绍
  4. ElasticSearch 中的中文分词器该怎么玩?
  5. ElasticSearch 索引基本操作
  6. ElasticSearch 文档的添加、获取以及更新
  7. ElasticSearch 文档的删除和批量操作
  8. ElasticSearch 文档路由,你的数据到底存在哪一个分片上?
  9. ElasticSearch 并发的处理方式:锁和版本控制
  10. ElasticSearch 中的倒排索引到底是什么?
  11. ElasticSearch 动态映射与静态映射
  12. ElasticSearch 四种字段类型详解
  13. ElasticSearch 中的地理类型和特殊类型
  14. ElasticSearch 23 种映射参数详解
  15. ElasticSearch 如何配置某个字段的权重?
  16. ElasticSearch 23 种映射参数详解【3】
  17. ElasticSearch 映射模版
  18. ElasticSearch 搜索入门
  19. ElasticSearch 全文搜索怎么玩?
  20. ElasticSearch 打错字还能搜索到?试试 fuzzy query!
  21. ElasticSearch 复合查询,理解 Es 中的文档评分策略!
  22. 想搜索附近评分较高的餐厅,ElasticSearch 大显身手!
  23. ElasticSearch 如何像 MySQL 一样做多表联合查询?
  24. ElasticSearch 地理位置查询与特殊查询
  25. ElasticSearch 搜索高亮与排序
  26. ElasticSearch 指标聚合
  27. ElasticSearch 桶聚合
  28. ElasticSearch 管道聚合
  29. Java 操作 ElasticSearch,so easy!
  30. ElasticSearch Java 高级客户端索引操作~






往期推荐
0
1

50+ 需求文档免费下载!

0
2

Spring Security 教程合集

0
3

接了两个私活,都是血汗钱

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

本文分享自微信公众号 - 江南一点雨(a_javaboy)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

版权声明:程序员胖胖胖虎阿 发表于 2022年9月24日 下午3:56。
转载请注明:ElasticSearch Java 高级客户端如何操作索引? | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...