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

一、ES 8.x 整合springBoot

1、导入依赖
<dependency>
	<groupId>com.fasterxml.jackson.coregroupId>
	<artifactId>jackson-databindartifactId>
	<version>2.12.3version>
dependency>

<dependency>
	<groupId>co.elastic.clientsgroupId>
	<artifactId>elasticsearch-javaartifactId>
	<version>8.7.1version>
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、批量插入(更新)文档

   @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 即为成功 
   }

你可能感兴趣的:(java,elasticsearch,开发语言,maven)