• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

ElasticSearch 根据环境自动创建动态索引

武飞扬头像
……攻城狮
帮助3

        我的客户端的版本是7.13.0,对应springboot与spring-data-elasticsearch的版本如下:(2.5.8与4.2.7)

学新通

        引入依赖:

  1.  
    <dependency>
  2.  
    <groupId>org.springframework.boot</groupId>
  3.  
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  4.  
    </dependency>

   

@Document(indexName = "#{@active}" "_" ElasticSarchConstants.ES_INDEX_PRODUCT)
其中#{@active}"根据开发环境动态切换:
  1.  
    package com.qihong.common.es.config;
  2.  
     
  3.  
    import lombok.Getter;
  4.  
     
  5.  
    import org.springframework.beans.factory.annotation.Value;
  6.  
    import org.springframework.context.annotation.Bean;
  7.  
    import org.springframework.context.annotation.Configuration;
  8.  
    import org.springframework.stereotype.Component;
  9.  
     
  10.  
    /**
  11.  
    * @author zhg
  12.  
    * @create 2022/6/9
  13.  
    */
  14.  
    @Configuration
  15.  
    @Component
  16.  
    @Getter
  17.  
    public class ActiveBean
  18.  
    {
  19.  
     
  20.  
    //读取环境配置
  21.  
    @Value("${spring.profiles.active}")
  22.  
    private String active;
  23.  
     
  24.  
    @Bean
  25.  
    public String active(){
  26.  
    return active;
  27.  
    }
  28.  
    }
学新通

 1、创建es mapping类 

  1.  
    package com.qihong.common.es.domain;
  2.  
     
  3.  
    import com.qihong.common.es.constant.ElasticSarchConstants;
  4.  
    import lombok.Data;
  5.  
     
  6.  
    import org.springframework.beans.factory.annotation.Value;
  7.  
    import org.springframework.context.annotation.Configuration;
  8.  
    import org.springframework.data.annotation.Id;
  9.  
    import org.springframework.data.elasticsearch.annotations.*;
  10.  
    import org.springframework.data.elasticsearch.core.geo.GeoPoint;
  11.  
     
  12.  
    /**
  13.  
    * @author zhg
  14.  
    * @create 2022/6/9
  15.  
    */
  16.  
     
  17.  
    @Data
  18.  
    @Configuration
  19.  
    @Setting(replicas = 0,shards = 1)
  20.  
    @Document(indexName = "#{@active}" "_" ElasticSarchConstants.ES_INDEX_PRODUCT)
  21.  
    public class ProductStoreEs {
  22.  
     
  23.  
    @Id
  24.  
    @Field(type = FieldType.Long)
  25.  
    private Long productId;
  26.  
     
  27.  
    @Field(type = FieldType.Text,analyzer = "ik_smart")
  28.  
    private String productName;
  29.  
     
  30.  
    @Field(type = FieldType.Text,analyzer = "ik_smart")
  31.  
    private String storeName;
  32.  
     
  33.  
    //经纬度保存
  34.  
    @GeoPointField
  35.  
    private GeoPoint location;
  36.  
     
  37.  
    @Field(type = FieldType.Long)
  38.  
    private Long siteId;
  39.  
     
  40.  
    @Field(type = FieldType.Long)
  41.  
    private Long storeId;
  42.  
     
  43.  
    @Field(type = FieldType.Integer)
  44.  
    private Integer status;
  45.  
    }
学新通

2、启动服务模块自动创建索引,与mapping

  1.  
    package com.qihong.common.es.config;
  2.  
     
  3.  
    import org.reflections.Reflections;
  4.  
    import org.springframework.beans.factory.annotation.Autowired;
  5.  
    import org.springframework.context.ApplicationListener;
  6.  
    import org.springframework.context.annotation.Configuration;
  7.  
    import org.springframework.context.event.ContextRefreshedEvent;
  8.  
    import org.springframework.data.elasticsearch.annotations.Document;
  9.  
    import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
  10.  
     
  11.  
    import java.util.Set;
  12.  
     
  13.  
    /**
  14.  
    * @author zhg
  15.  
    * @create 2022/5/20
  16.  
    */
  17.  
     
  18.  
    @Configuration
  19.  
    public class ElasticSearchStartCreateIndex implements ApplicationListener<ContextRefreshedEvent> {
  20.  
     
  21.  
    @Autowired
  22.  
    private ElasticsearchRestTemplate restTemplate;
  23.  
     
  24.  
    @Override
  25.  
    public void onApplicationEvent(ContextRefreshedEvent event) {
  26.  
    Reflections reflections = new Reflections("com.qihong.common.es.domain");
  27.  
    Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Document.class);
  28.  
    for (Class<?> clazz: typesAnnotatedWith) {
  29.  
    if(!restTemplate.indexOps(clazz).exists()){
  30.  
    restTemplate.indexOps(clazz).create();
  31.  
    restTemplate.indexOps(clazz).putMapping(clazz);
  32.  
    }
  33.  
    }
  34.  
     
  35.  
    }
  36.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhiiea
系列文章
更多 icon
同类精品
更多 icon
继续加载