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

这篇具有很好参考价值的文章主要介绍了ElasticSearch 根据环境自动创建动态索引。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

elasticsearch 自动创建索引,ElasticSearch,java,java,开发语言

        引入依赖:

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

   

@Document(indexName = "#{@active}"+"_"+ElasticSarchConstants.ES_INDEX_PRODUCT)
其中#{@active}"根据开发环境动态切换:
package com.qihong.common.es.config;

import lombok.Getter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author zhg
 * @create 2022/6/9
 */
@Configuration
@Component
@Getter
public class ActiveBean
{

      //读取环境配置
      @Value("${spring.profiles.active}")
      private String active;

      @Bean
      public String active(){
            return active;
      }
}

 1、创建es mapping类 

package com.qihong.common.es.domain;

import com.qihong.common.es.constant.ElasticSarchConstants;
import lombok.Data;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;

/**
 * @author zhg
 * @create 2022/6/9
 */

@Data
@Configuration
@Setting(replicas = 0,shards = 1)
@Document(indexName = "#{@active}"+"_"+ElasticSarchConstants.ES_INDEX_PRODUCT)
public class ProductStoreEs {

    @Id
    @Field(type = FieldType.Long)
    private Long productId;

    @Field(type = FieldType.Text,analyzer = "ik_smart")
    private String productName;

    @Field(type = FieldType.Text,analyzer = "ik_smart")
    private String storeName;

    //经纬度保存
    @GeoPointField
    private GeoPoint location;

    @Field(type = FieldType.Long)
    private Long siteId;

    @Field(type = FieldType.Long)
    private Long storeId;

    @Field(type = FieldType.Integer)
    private Integer status;
}

2、启动服务模块自动创建索引,与mapping文章来源地址https://www.toymoban.com/news/detail-554548.html

package com.qihong.common.es.config;

import org.reflections.Reflections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import java.util.Set;

/**
 * @author zhg
 * @create 2022/5/20
 */

@Configuration
public class ElasticSearchStartCreateIndex implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        Reflections reflections = new Reflections("com.qihong.common.es.domain");
        Set<Class<?>> typesAnnotatedWith = reflections.getTypesAnnotatedWith(Document.class);
        for (Class<?> clazz: typesAnnotatedWith) {
            if(!restTemplate.indexOps(clazz).exists()){
                restTemplate.indexOps(clazz).create();
                restTemplate.indexOps(clazz).putMapping(clazz);
            }
        }

    }
}

到了这里,关于ElasticSearch 根据环境自动创建动态索引的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • yyds,Elasticsearch Template自动化管理新索引创建

    一、什么是Elasticsearch Template? Elasticsearch Template是一种将预定义模板应用于新索引的功能。在索引创建时,它可以自动为新索引应用已定义的模板。Template功能可用于定义索引的映射、设置和别名等。它是一种自动化管理索引创建的方式,使用户可以在大量索引上快速而一致

    2023年04月08日
    浏览(29)
  • elasticsearch索引操作,索引创建、索引更新、索引删除

    创建索引 更新索引,添加字段 注意更新索引时与创建索引大致一样,只是更新索引时候的url不同,需要在后面加一个 _mapping 路径,同时请求的json里面不需要 mappings 路径,只需要 properties 即可 更新索引,修改配置 同理在更新setting的时候和更新maping的时候一样 获取索引结构

    2024年02月11日
    浏览(34)
  • elasticsearch创建索引模板

    在es数据保存数据,数据量很大时,如日志记录,监控数据等,需要按月或者按日分索引保存,需要创建统一模板来规定数据格式。 可以通过kibana创建 创建模板 创建template之后创建索引

    2024年02月11日
    浏览(36)
  • elasticSearch创建索引库、映射、文档

    创建索引库 使用postman或curl这样的工具创建 参数: number_of_shards:设置分片的数量,在集群中通常设置多个分片,表示一个索引库将拆分成多片分别存储不同的结点,提高了ES的处理能力和高可用性,入门程序使用单机环境,这里设置为1。 number_of_replicas:设置副本的数量,设

    2024年02月04日
    浏览(36)
  • ElasticSearch 创建索引超时(ReadTimeoutError)

    在 Python 中调用 client.indices.create 来创建 ElasticSearch 索引时,报如下错误: 在查阅网络资料时,一开始以为是字面意思上的连接超时问题,但调大 timeout 参数也一样报错,而且之前用相同方法创建类似索引时并未出现该问题。 最后,受博文(https://blog.csdn.net/ckq707718837/article

    2024年04月22日
    浏览(52)
  • elasticsearch中手动创建索引失败

    想通过kibana的dev tool工具或者cerebro工具的rest来手动创建索引 创建失败,错误代码403,错误提示:“reason”: “action [indices:admin/create] is unauthorized for user [分配的es用户名]”} 报错提示: 每个用户在分配用户和角色时被固定了索引格式,如果你的请求操作的目标索引不存在于

    2024年02月13日
    浏览(33)
  • 使用elasticsearch创建索引时报错

    最近在使用时创建索引的时候 , 出现以上的错误 我的代码如下 : 这个时候出现错误 错误详细信息如下: 解决方法 : 这是因为在使用create方法时 , 会有两个选择 , 其中一个已经过时了 解决 : 查看自己导入的包是哪个 , 使用这个包就可以正常运行了

    2024年02月11日
    浏览(30)
  • Elasticsearch 创建索引mapping修改、修复

    背景:原始index名字是indexName,mapping设置错误,且已经有数据进去,想要修改mapping结构 返回结果:表示新建索引成功 下面表示把indexName的数据同步到indexName_1

    2024年02月11日
    浏览(30)
  • Elasticsearch的索引和映射:动态映射

    Elasticsearch是一个基于分布式搜索和分析引擎,可以提供实时的、可扩展的、高性能的搜索功能。它是一个基于Lucene的搜索引擎,具有高度可扩展性和高性能。Elasticsearch支持多种数据类型,如文本、数字、日期等,并提供了丰富的查询功能。 在Elasticsearch中,数据是通过索引和

    2024年02月20日
    浏览(39)
  • Elasticsearch如何创建索引,添加,删除,更新文档

    了解es基本概念:elasticsearch(es)背景故事与基本概念 安装es:Linux安装Elasticsearch详细教程 安装kibana:Linux安装Kibana详细教程 熟悉Json 熟悉REST接口 检查 es 及 Kibana 是否运行正常 创建一个名为 twitter 的索引(index),并插入一个文档(document) 在关系型数据库中,需要使用DDL语

    2023年04月08日
    浏览(56)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包