• spring boot集成Elasticsearch 7.16.3


    环境:Elasticsearch 版本 7.16.3

    Elasticsearch for windows下载地址
    windows
    若依
    spring boot版本 2.6.0

    在这里插入图片描述

    pom文件添加

    		<dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            </dependency>
            
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>8.12.0</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    自定义配置类

    import org.apache.http.HttpHost;
    import org.apache.http.client.config.RequestConfig;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestClientBuilder;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
    
    @Configuration
    public class ESConfig extends AbstractElasticsearchConfiguration {
    
        @Override
        public RestHighLevelClient elasticsearchClient() {
            // 设置连接超时和读取超时时间(单位:毫秒)
            int connectionTimeout = 60000; // 60 秒
            int readTimeout = 60000; // 60 秒
    
            // 创建 RequestConfig
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(connectionTimeout)
                    .setSocketTimeout(readTimeout)
                    .build();
    
            // 创建 RestClientBuilder
            RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200))
                    .setHttpClientConfigCallback(httpClientBuilder ->
                            httpClientBuilder.setDefaultRequestConfig(requestConfig));
    
            // 创建 RestHighLevelClient
            RestHighLevelClient client = new RestHighLevelClient(builder);
    
            return client;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    实体类

    indexName = “zbinfo” //索引名

    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;
    import org.springframework.data.elasticsearch.annotations.Field;
    import org.springframework.data.elasticsearch.annotations.FieldType;
    
    @Document(indexName = "zbinfo",shards = 1,replicas = 0)
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class ESZbInfo {
    
        @Id
        @Field(type = FieldType.Keyword)
        private Long id;
    
        @Field(type = FieldType.Long)
        private Long nid;
    
        /** 标题 */
        @Field(type = FieldType.Text)
        private String title;
    
        @Field(type = FieldType.Text)
        private String content;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    创建操作的Repository

    import com.dataDemo.system.domain.ESZbInfo;
    import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface ESZbInfoRepstitory extends ElasticsearchRepository<ESZbInfo, String> {
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    操作CRUD

    	@Autowired
        private ESZbInfoRepstitory esZbInfoRepstitory;
        
     	@Autowired
        private ElasticsearchOperations elasticsearchOperations;
    
    //批量插入
    public void insertEsZbInfo(){
            List<ZbInfoTxy> list= zbInfoTxyService.selectZbInfoTxyListLocal(new ZbInfoTxy());
            Integer count = 0;
            List<ESZbInfo> lists = new ArrayList<>();
            if(list.size() != 0){
                for(ZbInfoTxy zbInfoTxy:list){
                    System.out.println("数量----"+count+"-----date---"+ DateUtils.getTime());
                    ESZbInfo zbInfo = new ESZbInfo();
                    zbInfo.setId(zbInfoTxy.getNid());
                    zbInfo.setTitle(zbInfoTxy.getTitle());               
                    zbInfo.setContent(zbInfoTxy.getContent());
                    lists.add(zbInfo);
                    count++;
                }
            }
            //批量插入方法
            esZbInfoRepstitory.saveAll(lists);
        }
    
    //单个插入
     esZbInfoRepstitory.save(new ZbInfoTxy ());
    
    	//删除
        public void deleteEsAll(){
            System.out.println("删除全部:");
            esZbInfoRepstitory.deleteAll();
        }
    
    //查询 --根据关键词查询
        public void selectEsZbInfo(String title){
            System.out.println("根据关键词查询:");
            /**
             * //单条件模糊查询
             * NativeSearchQuery query = new NativeSearchQueryBuilder()
             *                 .withQuery(QueryBuilders.matchPhraseQuery("title", title))
             *                 .build();
             *
             * //多条件匹配查询
             *  NativeSearchQuery query = new NativeSearchQueryBuilder()
             *                 .withQuery(QueryBuilders.boolQuery()
             *                         .must(QueryBuilders.matchPhraseQuery("title", title))
             *                         .must(QueryBuilders.matchQuery("city", 98)))
             *                 .build();
             *
             * //检索一个字段在两个字段中出现过的
             * NativeSearchQuery query = new NativeSearchQueryBuilder()
             *         .withQuery(QueryBuilders.boolQuery()
             *                 .must(QueryBuilders.multiMatchQuery(title, "title", "content")))
             *         .build();
             */
            NativeSearchQuery query = new NativeSearchQueryBuilder()
                    .withQuery(QueryBuilders.boolQuery()
                            .must(QueryBuilders.matchPhraseQuery("title", title))
                            .must(QueryBuilders.matchQuery("city", 98)))
                    .build();
            SearchHits<ESZbInfo> searchHits = elasticsearchOperations.search(query, ESZbInfo.class);
            List<ESZbInfo> list = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());
            if(list.size() != 0){
                for(ESZbInfo esZbInfo:list){
                    System.out.println(esZbInfo);
                }
            }
        }
    
    //根据id查询
        public void selectEsById(String id){
            Optional<ESZbInfo> optionalById = this.esZbInfoRepstitory.findById(id);
            System.out.println("单个查询---"+optionalById);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    //查询es状态
    http://localhost:9200/_cluster/health

    //查询索引的zbinfo映射字段
    http://localhost:9200/zbinfo/_mapping

    如果插入es的数据量过大,可调整Elasticsearch 目录下/config/elasticsearch.yml 配置文件
    添加 http.max_content_length: 200mb(可根据需求更改)
    然后重启es

  • 相关阅读:
    解码癌症预测的密码:可解释性机器学习算法SHAP揭示XGBoost模型的预测机制
    SpringBoot开发之Spring基础
    烧了 300 张 H100,新版开源 LLM 排行榜发布:中国模型 Qwen-72B 仍是第一!
    Pygame 精准检测图像碰撞
    设计模式-相关内容
    FFmpeg入门
    Java-面向对象的特征之一:封装
    【从0开始学架构】访问层架构设计
    Redis
    软考知识点---10数据库基础
  • 原文地址:https://blog.csdn.net/qq_41687670/article/details/136383021