es 建索引时指定分片数据和副本数

本文介绍了如何使用Elasticsearch进行高效的数据索引配置,包括指定分片数量、副本数量、刷新时间间隔等参数,同时详细展示了如何设置索引映射以确保不同类型字段的正确索引。

1指定分片数量  "number_of_shards":8

2指定副本数量   "number_of_replicas" : 1

3指定刷新时间30s,写入数据时,查看到的数据与真实数据有30s的延时   "refresh_interval":"30s"

4禁用_all 字段    "_all":{ "enabled": false}


curl -XDELETE 'http://'${es_node}':9200/index_name'${year_dt}''

curl -XPUT 'http://'${es_node}':9200/index_name'${year_dt}'?pretty' -H 'Content-Type: application/json' -d'
{
 "settings" : {
        "number_of_shards":8,
        "number_of_replicas" : 1,
        "refresh_interval":"30s"
    },
    "mappings" : {
      "type_name":{
    "properties":{
        "bill_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "opupk":{
            "type":"string",
            "index":"not_analyzed"
        },
        "invoice_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "customer_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "zone_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "vip_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "vip_zone_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "bukrs":{
            "type":"string",
            "index":"not_analyzed"
        },
        "bill_period":{
            "type":"string",
            "index":"not_analyzed"
        },
        "payment_term":{
            "type":"string",
            "index":"not_analyzed"
        },
        "payment_due_dt":{
            "type":"string",
            "index":"not_analyzed"
        },
        "vktyp":{
            "type":"string",
            "index":"not_analyzed"
        },
        "fee_amt1":{
            "type":"string",
            "index":"not_analyzed"
        },
        "currency_code1":{
            "type":"string",
            "index":"not_analyzed"
        },
        "fee_amt2":{
            "type":"string",
            "index":"not_analyzed"
        },
        "currency_code2":{
            "type":"string",
            "index":"not_analyzed"
        },
        "receipt_batch_number":{
            "type":"string",
            "index":"not_analyzed"
        },
        "bu_type":{
            "type":"string",
            "index":"not_analyzed"
        },
        "unique_id":{
            "type":"string",
            "index":"not_analyzed"
        },
        "bg_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "depart":{
            "type":"string",
            "index":"not_analyzed"
        },
        "charge_item_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "region_type_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "tax_rule_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "kostl":{
            "type":"string",
            "index":"not_analyzed"
        },
        "geber":{
            "type":"string",
            "index":"not_analyzed"
        },
        "partner_zone_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "partner_bg_code":{
            "type":"string",
            "index":"not_analyzed"
        },
        "partner_bukrs":{
            "type":"string",
            "index":"not_analyzed"
        },
        "original_gl_dt":{
            "type":"string",
            "index":"not_analyzed"
        },
        "zzcustom_01":{
            "type":"string",
            "index":"not_analyzed"
        },
        "zzcustom_02":{
            "type":"string",
            "index":"not_analyzed"
        },
        "zzcustom_03":{
            "type":"string",
            "index":"not_analyzed"
        },
        "zzcustom_04":{
            "type":"string",
            "index":"not_analyzed"
        },
        "zzcustom_05":{
            "type":"string",
            "index":"not_analyzed"
        },
        "batch_no":{
            "type":"string",
            "index":"not_analyzed"
        },
        "caculate_dt":{
            "type":"string",
            "index":"not_analyzed"
        },
        "rebate_begin_dt":{
            "type":"string",
            "index":"not_analyzed"
        },
        "rebate_end_dt":{
            "type":"string",
            "index":"not_analyzed"
        },
        "bill_end_inv_flg":{
            "type":"string",
            "index":"not_analyzed"
        },
        "invoice_type":{
            "type":"string",
            "index":"not_analyzed"
        },
        "create_tm":{
            "type":"string",
            "index":"not_analyzed"
        }
        },
                "_all":{ "enabled": false}
                }
    }
}';
Spring Boot整合Elasticsearch通常会利用`ElasticsearchRestTemplate`这个客户端工具来操作Elasticsearch。要使用它创建索引、设置分片副本,你可以按照以下步骤进行: 1. 首先,你需要添加Elasticsearch的依赖到你的Spring Boot项目中。如果你还没有添加,可以在`pom.xml`中加入以下内容: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 2. 创建一个配置类,例如`ElasticsearchConfig.java`,配置Elasticsearch的相关信息: ```java @Configuration public class ElasticsearchConfig { @Value("${spring.data.elasticsearch.cluster-name:your-cluster-name}") private String clusterName; @Bean public RestHighLevelClient elasticsearchClient() { return new PreemptiveRestHighLevelClient( RestClient.builder(new HttpHost("localhost", ElasticsearchProperties.DEFAULT_PORT, "http"))); } @Bean public ElasticsearchOperations elasticsearchOperations(RestHighLevelClient client) { return new ElasticsearchRepositorySupport(client); } } ``` 3. 现在你有了`ElasticsearchOperations`实例,可以用来创建索引。假设有一个名为`Product`的对象,你可以这样做: ```java @Autowired private ElasticsearchOperations esOperations; public void createIndex() { IndexRequest request = new IndexRequest("products") .settings(Settings.builder() .put(IndexSettings.INDEX_NUMBER_OF_SHARDS, 5) // 设置分片.put(IndexSettings.INDEX_NUMBER_OF_REPLICAS, 1) // 设置副本数 .build()); try { esOperations.index(request, Product.class); // 使用Product作为映射类型 } catch (DocumentExistsException e) { System.out.println("索引已存在"); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值