Elasticsearch8.x版本中RestHighLevelClient被弃用,新版本中全新的Java客户端Elasticsearch Java API Client中常用API练习

这篇具有很好参考价值的文章主要介绍了Elasticsearch8.x版本中RestHighLevelClient被弃用,新版本中全新的Java客户端Elasticsearch Java API Client中常用API练习。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Es的java API客户端

在Es7.15版本之后,es官方将它的高级客户端RestHighLevelClient标记为弃用状态。同时推出了全新的java API客户端Elasticsearch Java API Client,该客户端也将在Elasticsearch8.0及以后版本中成为官方推荐使用的客户端。

Elasticsearch Java API Client支持除Vector title search API和Find structure API之外的所有Elasticsearch API。且支持所有API数据类型,并且不再有原始JSON Value属性。它是针对Elasticsearch8.0及之后版本的客户端。

感兴趣的小伙伴可以去官方文档看看:Elasticsearch官网8.x版本下Java客户端文档

Elasticsearch Java API Client使用

  • 新建maven项目

  • maven中引入依赖坐标

        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.1.0</version>
        </dependency>
         <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </dependency>
  • 创建连接
    @Test
    public void create() throws IOException {
        // 创建低级客户端
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200)
        ).build();
        // 使用Jackson映射器创建传输层
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper()
        );
        // 创建API客户端
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 关闭ES客户端
        transport.close();
        restClient.close();
    }

索引index的基本语法

  • 创建索引
    @Test
    public void create() throws IOException {
        // 创建低级客户端
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200)
        ).build();
        // 使用Jackson映射器创建传输层
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper()
        );
        // 创建API客户端
        ElasticsearchClient client = new ElasticsearchClient(transport);
        // 创建索引
        CreateIndexResponse createIndexResponse = client.indices().create(c -> c.index("user_test"));
        // 响应状态
        Boolean acknowledged = createIndexResponse.acknowledged();
        System.out.println("索引操作 = " + acknowledged);

        // 关闭ES客户端
        transport.close();
        restClient.close();
    }
  • 查询索引
    @Test
    public void query() throws IOException {
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost",9200)
        ).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 查询索引
        GetIndexResponse getIndexResponse = client.indices().get(e -> e.index("user_test"));
        System.out.println("getIndexResponse.result() = " + getIndexResponse.result());
        System.out.println("getIndexResponse.result().keySet() = " + getIndexResponse.result().keySet());

        transport.close();
        restClient.close();
    }

如果查询的index不存在会在控制台抛出index_not_found_exception

  • 删除索引
    @Test
    public void delete() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 删除索引
        DeleteIndexResponse deleteIndexResponse = client.indices().delete(e -> e.index("user_test"));
        System.out.println("删除操作 = " + deleteIndexResponse.acknowledged());

        transport.close();
        restClient.close();
    }

文档document的操作

  • 创建User对象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
    private String sex;
    private Integer age;
}
  • 添加document
    @Test
    public void addDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 向user对象中添加数据
        User user = new User("java客户端", "男", 18);
        // 向索引中添加数据
        CreateResponse createResponse = client.create(e -> e.index("user_test").id("1001").document(user));
        System.out.println("createResponse.result() = " + createResponse.result());

        transport.close();
        restClient.close();
    }

注:index中参数为文档所属的索引名,id中参数为当文档的id,document为文档数据,新版本支持直接传入java对象。

  • 查询document
    @Test
    public void queryDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 构建请求
        GetResponse<User> getResponse = client.get(e -> e.index("user_test").id("1001"), User.class);
        System.out.println("getResponse.source().toString() = " + getResponse.source().toString());

        transport.close();
        restClient.close();
    }

注:如果查不到控制台抛出NullPointerException

  • 修改document
    @Test
    public void modifyDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 使用map集合封装需要修改的内容
        Map<String, Object> map = new HashMap<>();
        map.put("name", "java客户端aaa");
        // 构建请求
        UpdateResponse<User> updateResponse = client.update(e -> e.index("user_test").id("1001").doc(map), User.class);
        System.out.println("updateResponse.result() = " + updateResponse.result());

        transport.close();
        restClient.close();
    }
  • 删除document
    @Test
    public void removeDocument() throws  IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 构建请求
        DeleteResponse deleteResponse = client.delete(e -> e.index("user_test").id("1001"));
        System.out.println("deleteResponse.result() = " + deleteResponse.result());

        transport.close();
        restClient.close();
    }
  • 批量添加document
    @Test
    public void batchAddDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 构建一个批量数据集合
        List<BulkOperation> list = new ArrayList<>();
        list.add(new BulkOperation.Builder().create(
                d -> d.document(new User("test2", "男", 19)).id("1002").index("user_test")).build());
        list.add(new BulkOperation.Builder().create(
                d -> d.document(new User("test3", "男", 20)).id("1003").index("user_test")).build());
        list.add(new BulkOperation.Builder().create(
                d -> d.document(new User("test4", "女", 21)).id("1004").index("user_test")).build());
        // 调用bulk方法执行批量插入操作
        BulkResponse bulkResponse = client.bulk(e -> e.index("user_test").operations(list));
        System.out.println("bulkResponse.items() = " + bulkResponse.items());

        transport.close();
        restClient.close();
    }

批量添加的核心是需要构建一个泛型为BulkOperationArrayList集合,实质上是将多个请求包装到一个集合中,进行统一请求,进行构建请求时调用bulk方法,实现批量添加效果。

  • 批量删除
    @Test
    public void batchDeleteDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 构建一个批量数据集合
        List<BulkOperation> list = new ArrayList<>();
        list.add(new BulkOperation.Builder().delete(
                d -> d.id("1002").index("user_test")).build());
        list.add(new BulkOperation.Builder().delete(
                d -> d.id("1003").index("user_test")).build());
        // 调用bulk方法执行批量插入操作
        BulkResponse bulkResponse = client.bulk(e -> e.index("user_test").operations(list));
        System.out.println("bulkResponse.items() = " + bulkResponse.items());

        transport.close();
        restClient.close();
    }
  • 全量查询
    @Test
    public void queryAllDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 全量查询
        SearchResponse<User> searchResponse = client.search(e -> e.index("user_test").query(q -> q.matchAll(m -> m)), User.class);
        HitsMetadata<User> hits = searchResponse.hits();
        for (Hit<User> hit : hits.hits()) {
            System.out.println("user = " + hit.source().toString());
        }
        System.out.println("searchResponse.hits().total().value() = " + searchResponse.hits().total().value());

        transport.close();
        restClient.close();
    }
  • 分页查询
    @Test
    public void pagingQueryDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 分页查询
        SearchResponse<User> searchResponse = client.search(
                s -> s.index("user_test")
                        .query(q -> q.matchAll(m -> m))
                        .from(2)
                        .size(2)
                , User.class);
        searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));

        transport.close();
        restClient.close();
    }

分页查询就是在全量查询的基础上增加了从第几条开始,每页显示几条

  • 排序查询
    @Test
    public void sortQueryDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 排序查询
        SearchResponse<User> searchResponse = client.search(
                s -> s.index("user_test")
                        .query(q -> q.matchAll(m -> m))
                        .sort(o -> o.field(f -> f.field("age").order(SortOrder.Asc)))
                , User.class);
        searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));

        transport.close();
        restClient.close();
    }
  • 条件查询
    @Test
    public void conditionQueryDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 条件查询
        SearchResponse<User> searchResponse = client.search(
                s -> s.index("user_test").query(q -> q.matchAll(m -> m))
                        .sort(o -> o.field(f -> f.field("age").order(SortOrder.Asc)))
                        .source(r -> r.filter(f -> f.includes("name", "age").excludes("")))
                , User.class);
        searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));

        transport.close();
        restClient.close();
    }

includes是显示的字段,excludes是排除的字段

  • 组合查询
    @Test
    public void combinationQueryDocument() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 组合查询
        SearchResponse<User> searchResponse = client.search(
                s -> s.index("user_test").query(q -> q.bool(b -> b
                        .must(m -> m.match(u -> u.field("age").query(21)))
                        .must(m -> m.match(u -> u.field("sex").query("男")))
                        .mustNot(m -> m.match(u -> u.field("sex").query("女")))
                ))
                , User.class);
        searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));

        transport.close();
        restClient.close();
    }
    @Test
    public void combinationQueryDocument2() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 组合查询
        SearchResponse<User> searchResponse = client.search(
                s -> s.index("user_test").query(q -> q.bool(b -> b
                        .should(h -> h.match(u -> u.field("age").query(19)))
                        .should(h -> h.match(u -> u.field("sex").query("男")))
                ))
                , User.class);
        searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));

        transport.close();
        restClient.close();
    }

must是必须满足所有条件,should只要满足一个就行

  • 范围查询
    @Test
    public void scopeQueryDocument2() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 范围查询,gte()表示取大于等于,gt()表示大于,lte()表示小于等于
        SearchResponse<User> searchResponse = client.search(s -> s.index("user_test").query(q -> q
                        .range(r -> r.field("age").gte(JsonData.of(20)).lt(JsonData.of(21))))
                , User.class);
        searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));

        transport.close();
        restClient.close();
    }
  • 模糊查询
    @Test
    public void fuzzyQueryDocument2() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 模糊查询,fuzziness表示差几个可以查询出来
        SearchResponse<User> searchResponse = client.search(s -> s.index("user_test").query(q -> q
                        .fuzzy(f -> f.field("name").value("tst").fuzziness("2")))
                , User.class);
        searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));

        transport.close();
        restClient.close();
    }
  • 高亮查询
    @Test
    public void highlightQueryDocument2() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 高亮查询
        SearchResponse<User> searchResponse = client.search(s -> s.index("user_test").query(q -> q
                        .term(t -> t.field("name").value("test3")))
                        .highlight(h -> h.fields("name", f -> f.preTags("<font color='red'>").postTags("</font>")))
                , User.class);
        searchResponse.hits().hits().forEach(h -> System.out.println(h.source().toString()));

        transport.close();
        restClient.close();
    }
  • 聚合查询
    @Test
    public void aggregateQueryDocument2() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 聚合查询,取最大年龄
        SearchResponse<User> searchResponse = client.search(s -> s.index("user_test").aggregations("maxAge", a ->a.max(m -> m.field("age")))
                , User.class);
        searchResponse.aggregations().entrySet().forEach(f -> System.out.println(f.getKey() + ":" + f.getValue().max().value()));

        transport.close();
        restClient.close();
    }
  • 分组查询
    @Test
    public void groupQueryDocument2() throws IOException {
        RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);

        // 分组查询
        SearchResponse<User> searchResponse = client.search(s -> s.index("user_test")
                        .aggregations("ageGroup", a ->a.terms(t -> t.field("age")))
                , User.class);
        searchResponse.aggregations().get("ageGroup").lterms().buckets().array().forEach(f -> System.out.println(f.key() + ":" + f.docCount()));

        transport.close();
        restClient.close();
    }

分组查询实质上是聚合查询的一种

看到这里的小伙伴点个免费的赞吧,感谢支持!!!文章来源地址https://www.toymoban.com/news/detail-403398.html

到了这里,关于Elasticsearch8.x版本中RestHighLevelClient被弃用,新版本中全新的Java客户端Elasticsearch Java API Client中常用API练习的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • vscode | linux | c++ intelliense 被弃用解决方案

    每日一句,vscode用的爽是爽,主要是可配置太强了。如果也很会研究,可以直接去咸鱼接单了 废话少说,直接整。 用着用着说是c++ intelliense被弃用,很多辅助功能无法使用,像查看定义、查看引用、函数跳转、智能提示…… 归根结底,还是太菜了,但真的很需要这些辅助啊

    2024年02月12日
    浏览(27)
  • Android Handler被弃用,那么以后怎么使用Handler,或者类似的功能

    Android API30左右,Android应用在使用传统写法使用Handler类的时候会显示删除线,并提示相关的方法已经被弃用,不建议使用。 Android studio中的显示和建议: 看下官方API关于此处的解释:  简要说就是如果在实例化Handler的时候不提供Looper, 可能导致操作丢失(Handler 没有预估到新

    2023年04月21日
    浏览(28)
  • WebSecurityConfigurerAdapter被弃用Spring Security基于组件化的配置和使用

    在Spring Security 5.7及之后的版本中 WebSecurityConfigurerAdapter 将被启用,安全框架将转向基于组件的安全配置。 spring security官方文档 Spring Security without the WebSecurityConfigurerAdapter 如果使用的Spring Boot版本高于低于2.7.0、Spring Security版本高于5.7,就会出现如下的提示: 1、被启用的原因

    2024年02月02日
    浏览(29)
  • Unity打包APK错误:‘android.enableR8‘选项已被弃用,不应再使用

    Unity打包APK错误:\\\'android.enableR8’选项已被弃用,不应再使用 在Unity游戏开发中,我们经常需要将游戏打包成APK文件以在Android设备上进行测试或发布。然而,有时候在打包APK的过程中,可能会遇到一些错误。其中一个常见的错误是 “The option ‘android.enableR8’ is deprecated and sh

    2024年02月08日
    浏览(36)
  • Python错题集-7:DeprecationWarning: Conversion of an array with ndim(被弃用警告)

    DeprecationWarning: Conversion of an array with ndim 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)   X[i] = np.random.normal(loc=Ex, scale=np.abs(Enn), size=1) DeprecationWarning: Conversion of an array with ndim  是一个警告,通常出

    2024年04月09日
    浏览(32)
  • iOS中获取MCC和MNC的方法及iOS 16中CTCarrier被弃用的替代方案

    一、使用公共API获取MCC和MNC 在iOS中,我们可以使用CoreTelephony框架来获取用户的移动国家代码(MCC)和移动网络代码(MNC)。具体操作步骤如下: 在Xcode项目中,点击项目目标,进入“General”选项卡,在“Frameworks, Libraries, and Embedded Content”下点击“+”按钮,搜索并添加 Cor

    2024年02月11日
    浏览(78)
  • springBoot整合ElasticSearch8.x版本

    导入依赖   dependency         groupIdcom.fasterxml.jackson.core/groupId         artifactIdjackson-databind/artifactId         version2.13.2/version   /dependency     dependency         groupIdorg.glassfish/groupId         artifactIdjakarta.json/artifactId         version2.0.1/version   /dependency           dependency  

    2023年04月21日
    浏览(28)
  • Elasticsearch8.x版本Java客户端Elasticsearch Java API Client中常用API练习

    在Es7.15版本之后,es官方将它的高级客户端RestHighLevelClient标记为弃用状态。同时推出了全新的java API客户端Elasticsearch Java API Client,该客户端也将在Elasticsearch8.0及以后版本中成为官方推荐使用的客户端。 Elasticsearch Java API Client支持除Vector title search API和Find structure API之外的所有

    2024年04月11日
    浏览(33)
  • Elasticsearch-RestHighLevelClient基础操作

    该篇文章参考下面博主文章 Java中ElasticSearch的各种查询(普通,模糊,前缀,高亮,聚合,范围) 【es】java使用es中三种查询用法from size、search after、scroll 删除索引会把索引中已经创建好的数据也删除,就好像我们在mysql中删除库,会把库的数据也删除掉一样。 类似关闭数据

    2024年02月08日
    浏览(30)
  • Elasticsearch8 - Docker安装Elasticsearch8.12.2

    最近在学习 ES,所以需要在服务器上装一个单节点的 ES 服务器环境:centos 7.9 目前最新版本是 8.12.2 新增配置文件 elasticsearch.yml 解释一下,前三行是开启远程访问和跨域,最后一行是开启密码访问 Networking | Elasticsearch Guide [8.12] | Elastic 在宿主机创建容器的挂载目录,我的目录

    2024年04月15日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包