springboo整合elasticSearch8 java client api

Posted 阿拉的梦想

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboo整合elasticSearch8 java client api相关的知识,希望对你有一定的参考价值。

springboo整合elasticSearch8 java client api

官方文档: https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/connecting.html

依赖

gradle

dependencies 
    implementation 'co.elastic.clients:elasticsearch-java:8.1.2'
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.2'
    implementation 'jakarta.json:jakarta.json-api:2.0.1'

maven

<project>
  <dependencies>

    <dependency>
      <groupId>co.elastic.clients</groupId>
      <artifactId>elasticsearch-java</artifactId>
      <version>8.1.2</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.10.2</version>
    </dependency>
    
    <dependency>
      <groupId>jakarta.json</groupId>
      <artifactId>jakarta.json-api</artifactId>
      <version>2.0.1</version>
    </dependency>
  </dependencies>

es配置类

package com.demo.devops.document.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ES配置
 */
@Configuration
public class ElasticSearchConfig 

    @Value("$es.hostname:10.129.129.1")
    private String hostname;
    @Value("$es.port:9200")
    private int port;
    @Value("$es.username:elastic")
    private String username;
    @Value("$es.password:123456")
    private String password;

    @Bean
    public ElasticsearchClient esRestClient() 
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        // Create the low-level client
        RestClient restClient = RestClient.builder(new HttpHost(hostname, port)).setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).build();
        // Create the transport with a Jackson mapper
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        // And create the API client
        return new ElasticsearchClient(transport);
    



若无密码,可以使用下面方式:

// Create the low-level client
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
ElasticsearchClient client = new ElasticsearchClient(transport);

操作

创建索引

使用es自动设置的mapping

    @Autowired
    private ElasticsearchClient elasticsearchClient;
    ----
	//创建索引
	CreateIndexResponse createIndexResponse = client.indices().create(c -> c.index("newapi"));

设置mappings

    @Autowired
    private ElasticsearchClient elasticsearchClient;
    
	public void createDocIndex() throws IOException 
        log.info("开始新建ES索引");
        Map<String, Property> documentMap = new HashMap<>();
        documentMap.put("title", Property.of(property ->
                        property.text(TextProperty.of(p ->
                                        p.index(true)
                                                .analyzer("ik_max_word")
                                )
                        )
                )
        );
        documentMap.put("id", Property.of(property ->
                        property.long_(LongNumberProperty.of(p ->
                                        p.index(true)
                                )
                        )
                )
        );

        documentMap.put("content", Property.of(property ->
                        property.text(TextProperty.of(textProperty ->
                                        textProperty.index(true)
                                                .analyzer("ik_max_word")
                                )
                        )
                )
        );
        documentMap.put("createUserId", Property.of(property ->
                        property.keyword(KeywordProperty.of(p ->
                                        p.index(true)
                                )
                        )
                )
        );

        documentMap.put("createTime", Property.of(property ->
                        property.date(DateProperty.of(p ->
                                        p.index(true)
                                )
                        )
                )
        );
        // 创建索引
        CreateIndexResponse createIndexResponse = elasticsearchClient.indices().create(c -> 
            c.index(SystemConstant.ElasticConstants.INDEX_DOC_ALL)
                    .mappings(mappings -> mappings.properties(documentMap));
            //.aliases(SystemConstant.INDEX_DOC_ALL, aliases -> aliases.isWriteIndex(true));
            return c;
        );
        log.info("结束新建ES索引,res=", createIndexResponse.acknowledged());
    

删除索引

    public void deleteIndex(String index) throws IOException 
        log.info("开始删除索引,index=", index);
        DeleteIndexResponse response = elasticsearchClient.indices().delete(d -> d.index(index));
        log.info("开始删除索引,index=,res=", index, response.acknowledged());
    

新建文档

Doc是自定义实体类

    @Async
    public void createDoc(Doc doc) 
        log.info("开始新增到es,docNo=", doc.getDocNo());
        // 构建一个创建Doc的请求
        try 
            String newContent = convertContent(doc.getContent());
            doc.setContent(newContent);
            elasticsearchClient.index(x -> x.index(SystemConstant.ElasticConstants.INDEX_DOC_ALL).document(doc));
         catch (IOException e) 
            log.error("新增es文档异常,docNo=" + doc.getDocNo(), e);
        
        log.info("结束新增到es,docNo=", doc.getDocNo());
    

批量新建文档

    /**
     * 批量新增文档到Es
     *
     * @throws IOException
     */
    public void bulkCreateDocument() throws IOException 
        log.info("开始批量新增doc到es");
        List<Doc> docList = docService.list().stream().filter(x -> !DocStateEnum.DELETED.getState().equals(x.getState())).collect(Collectors.toList());
        log.info("批量新增doc到es,查询到doc数量=", docList.size());

        //构建一个批量操作BulkOperation的集合
        List<BulkOperation> bulkOperations = new ArrayList<>();
        //向集合添加数据
        for (Doc doc : docList) 
            bulkOperations.add(new BulkOperation.Builder().create(d -> d.document(doc).index(SystemConstant.ElasticConstants.INDEX_DOC_ALL)).build());
        
        //使用bulk方法执行批量操作并获得响应
        BulkResponse response = elasticsearchClient.bulk(e -> e.index(SystemConstant.ElasticConstants.INDEX_DOC_ALL).operations(bulkOperations));
        //打印结果
        log.info("新增完成,耗时=ms", response.took());
    

删除文档

    @Async
    public void deleteDoc(Doc doc) 
        log.info("开始删除es文档,docNo=", doc.getDocNo());
        // 构建一个创建Doc的请求
        try 
            SearchResponse<Doc> response = elasticsearchClient.search(s -> s
                            .index(SystemConstant.ElasticConstants.INDEX_DOC_ALL)
                            .query(q -> q.term(
                                    t -> t
                                            .field(SystemConstant.ElasticConstants.FIELD_DOC_NO)
                                            .value(doc.getDocNo())
                            ))
                    , Doc.class);
            if (response.hits().total().value() == 0) 
                return;
            
            elasticsearchClient.delete(x -> x.index(SystemConstant.ElasticConstants.INDEX_DOC_ALL).id(response.hits().hits().get(0).id()));
         catch (IOException e) 
            log.error("删除es文档异常,docNo=" + doc.getDocNo(), e);
        
        log.info("结束删除es文档,docNo=", doc.getDocNo());
    

更新文档

    /**
     * 更新文档
     */
    @Async
    @Override
    public void updateDoc(Doc doc) 
        log.info("开始修改es文档,docNo=", doc.getDocNo());
        try 
            SearchResponse<Doc> response = elasticsearchClient.search(s -> s
                            .index(SystemConstant.ElasticConstants.INDEX_DOC_ALL)
                            .query(q -> q.term(
                                    t -> t
                                            .field(SystemConstant.ElasticConstants.FIELD_DOC_NO)
                                            .value(doc.getDocNo())
                            ))
                    , Doc.class);
            assert response.hits().total() != null;
            if (response.hits().total().value() == 0) 
                //新增
                createDoc(doc);
             else 
                //更新
                String newContent = convertContent(doc.getContent());
                doc.setContent(newContent);
                elasticsearchClient.update(e -> e.index(SystemConstant.ElasticConstants.INDEX_DOC_ALL).id(response.hits().hits().get(0).id()).doc(doc), Doc.class);
            
         catch (IOException e) 
            log.error("更新es文档异常,docNo=" + doc.getDocNo(), e);
        
        log.info("结束修改es文档,docNo=", doc.getDocNo());
    

查询一个文档

    /**
     * 根据文档docNo查询一个文档
     *
     * @param docNo
     * @return
     * @throws IOException
     */
    @Override
    public Doc searchOneDoc(Long docNo) throws IOException 
        SearchResponse<Doc> response = elasticsearchClient.search(s -> s
                        .index(SystemConstant.ElasticConstants.INDEX_DOC_ALL)
                        .query(q -> q.term(
                                t -> t
                                        .field(SystemConstant.ElasticConstants.FIELD_DOC_NO)
                                        .value(docNo)
                        ))
                , Doc.class);
        if (response.hits().total().value() == 0) 
            return null;
        
        return response.hits().hits().get(0).source();
    

文档检索

    /**
     * 文档检索
     *
     * @param param
     * @return
     * @throws IOException
     */
    springboo整合elasticSearch8 java client api

Swagger2 常用使用 及 SpringBoo 整合 Swagger2

手把手教你用VUE开发后台管理系统:搭建SpringBoo 2.xt环境

Spring Boot 整合 Prometheus

Spring Boot 整合 Prometheus

Spring Boot 整合 Prometheus