Resthighlevelclient被弃用后,ES 8.x 最新用法 java api

这篇具有很好参考价值的文章主要介绍了Resthighlevelclient被弃用后,ES 8.x 最新用法 java api。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、ES 8.x 整合springBoot

1、导入依赖
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.12.3</version>
</dependency>

<dependency>
	<groupId>co.elastic.clients</groupId>
	<artifactId>elasticsearch-java</artifactId>
	<version>8.7.1</version>
</dependency>
2、创建 ElasticsearchClient
@Configuration
public class ElasticsearchClientConfig {
    @Bean
    public ElasticsearchClient restHighLevelClient() {
        RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200)
        ).build();
        ElasticsearchTransport elasticsearchTransport = new RestClientTransport(restClient,new JacksonJsonpMapper());

        return new ElasticsearchClient(elasticsearchTransport);
    }
}

二、ES java api

1、搜索

    @Resource
    ElasticsearchClient elasticsearchClient;
    @Test
    void contextLoads() throws IOException {
        // 指定索引,设置查询语句 
        SearchRequest searchRequest = 
            new SearchRequest.Builder().index("demo_comment_message")
            .query(Query.of(t -> t.bool(
                s -> s.must(
                        k -> k.match(
                                v -> v.field("desc").query("第一"))
                ).must(v-> v.range(m ->m.field("diggCount").lte(JsonData.of(100))))
        ))).build();
        // 执行查询语句
        SearchResponse<Object> search = elasticsearchClient.search(searchRequest,Object.class);
		// 获取返回结果
        for (Hit<Object> hit : search.hits().hits()) {
            System.out.println(hit.source());
        }
    }

2、单个插入(更新)文档

    @Resource
    ElasticsearchClient elasticsearchClient;

    @Test
    void contextLoads() throws IOException {
      IndexRequest<Product> indexRequest = 
          new IndexRequest.Builder<Product()
          .index("demo_comment_message") // 索引
          .id(product.getVideoId())  // 指定文档Id  也可以不指定,使用ES 自己生成的Id
          .document(product).build();  // 文档内容 (product) 
        // 运行插入语句
        IndexResponse index = elasticsearchClient.index(indexRequest);
        System.out.println(indexRequest.toString());
    }

3、批量插入(更新)文档文章来源地址https://www.toymoban.com/news/detail-624373.html

   @Resource
   ElasticsearchClient elasticsearchClient;

   @Test
   void contextLoads() throws IOException {
       // 构建插入 List
       List<Product> productList = new ArrayList<>();

       for (int i = 3; i < 10; i++) {
           Product product = new Product();
           product.setCommentId("Demo_" + i);
           product.setDesc("第" + i + "次 测试");
           product.setDiggCount(i * 10L);
           product.setVideoId("Code_" + i);
           productList.add(product);
       }

       BulkRequest.Builder builder = new BulkRequest.Builder().index("demo_comment_message"); // 指定索引
       
       for (Product product : productList) {
           builder.operations(op -> op.index(in -> in.id(product.getVideoId()).document(product)));
       }
       // 运行批量操作
       BulkResponse bulk = elasticsearchClient.bulk(builder.build());
       System.out.println(bulk.errors()); // 返回 false 即为成功 
   }

到了这里,关于Resthighlevelclient被弃用后,ES 8.x 最新用法 java api的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

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

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

    2024年02月12日
    浏览(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日
    浏览(32)
  • Android Handler被弃用,那么以后怎么使用Handler,或者类似的功能

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

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

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

    2024年02月08日
    浏览(39)
  • 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日
    浏览(33)
  • iOS中获取MCC和MNC的方法及iOS 16中CTCarrier被弃用的替代方案

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

    2024年02月11日
    浏览(81)
  • ES聚合查询 基于RestHighLevelClient依赖 Java操作

    一、介绍 (偏自我理解)         1.ES聚合查询通用流程                 1.分组 ( 好比Mysql --- group by )                 2.组内聚合 也叫 组内指标( 好比Mysql --- SUM()、COUNT()、AVG()、MAX()、MIN() )         2.桶(我要是es开发者,我起名叫啥都行)                 1.满足特

    2024年02月06日
    浏览(37)
  • Java使用Springboot集成Es官方推荐(RestHighLevelClient)

    SpringBoot集成ElasticSearch的四种方式(主要讲解ES官方推荐方式) TransportClient:这种方式即将弃用 官方将在8.0版本彻底去除 Data-Es:Spring提供的封装的方式,由于是Spring提供的,所以每个SpringBoot版本对应的ElasticSearch,具体这么个对应的版本,自己去官网看 ElasticSearch SQL:将Elasti

    2023年04月08日
    浏览(28)
  • 【Elasticsearch学习笔记五】es常用的JAVA API、es整合SpringBoot项目中使用、利用JAVA代码操作es、RestHighLevelClient客户端对象

    目录 一、Maven项目集成Easticsearch 1)客户端对象 2)索引操作 3)文档操作 4)高级查询 二、springboot项目集成Spring Data操作Elasticsearch 1)pom文件 2)yaml 3)数据实体类 4)配置类 5)Dao数据访问对象 6)索引操作 7)文档操作 8)文档搜索 三、springboot项目集成bboss操作elasticsearch

    2023年04月09日
    浏览(38)
  • Java(102):ES7.14,RestHighLevelClient创建索引时报错 create is deprecated

    一、Maven引用 二、遇到问题:ES7.14,RestHighLevelClient创建索引时报错 create  is deprecated \\\'create(org.elasticsearch.action.admin.indices.create.CreateIndexRequest, org.elasticsearch.client.RequestOptions)\\\' is deprecated   出现原因 : 这是因为在使用create方法时 , 会有两个选择 , 其中一个已经过时了 client.indic

    2023年04月09日
    浏览(30)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包