ElasticSearch入门- 设置分片副本数量及putMapping

本文介绍了如何在ElasticSearch中设置索引的分片和副本数量,强调了分片数量创建后无法更改的重要性,并提供了创建索引时指定分片个数的代码示例。同时,解释了在非生产环境中通常不保留副本的原因,并给出了添加type映射的方法以及动态修改副本数量的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在之前的 一篇文章 中,写到如何创建mapping。里面只是简单的创建了一个mapping。其实,这种比较重要并且一旦建立无法修改的操作还是需要仔细规划的。

今天我介绍设置index的分片数量及副本数量,即创建索引的如何指定分片的个数及副本的个数。分片的个数在创建之后是 无法再增加和减少 的,除非你另外建一个索引库,而 副本是可以在运行的时候,动态增加和减少 。因此,在创建索引库时,规划好分片(Shard)是非常重要的。

1、如何在创建index时,指定分片的个数?

其实代码也很简单。

Settings settings = ImmutableSettings.settingsBuilder()
//5个主分片
                .put("number_of_shards", 5)
                //测试环境,减少副本提高速度
                .put("number_of_replicas", 0).build();

 在非生产环境,这个副本最好设置成0,即不保留副本,因为每增加一个副本,写数据的成本就增加一倍。本来elasticsearch写的速度就比较慢。

2、如果在创建Index时,顺便把type的mapping也创建?

//首先创建索引库
CreateIndexResponse  indexresponse = client.admin().indices()
//这个索引库的名称还必须不包含大写字母
.prepareCreate("testindex").setSettings(settings)
//这里直接添加type的mapping
.addMapping("testType", getMapping())

 有几个type就addMapping几个。

综合起来的代码:

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.donlianli.es.ESUtils;
/**
 * 创建索引时,指定分片及副本
 * 并且创建type一起执行
 * @author donlianli@126.com
 */
public class PutMappingTest {
public static void  main(String[] argv) throws Exception{
Client client = ESUtils.getClient();
Settings settings = ImmutableSettings.settingsBuilder()
//5个主分片
                .put("number_of_shards", 5)
                //测试环境,减少副本提高速度
                .put("number_of_replicas", 0).build();
//首先创建索引库
CreateIndexResponse  indexresponse = client.admin().indices()
//这个索引库的名称还必须不包含大写字母
.prepareCreate("testindex").setSettings(settings)
//这里直接添加type的mapping
.addMapping("testType", getMapping())
.execute().actionGet();
System.out.println(indexresponse.acknowledged());
}
/**
 * mapping 一旦定义,之后就不能修改。
 * @return
 * @throws Exception
 */
private static XContentBuilder getMapping() throws Exception{
XContentBuilder mapping = jsonBuilder()  
       .startObject()  
         .startObject("test")  
         .startObject("properties")         
           .startObject("id")
           		.field("type", "long")
           		.field("store", "yes")
           	.endObject()    
           	
           .startObject("type")
           		.field("type", "string")
           		.field("index", "not_analyzed")
           	.endObject()  
           	
           .startObject("catIds")
           		.field("type", "integer")
           .endObject()  
         .endObject()  
        .endObject()  
      .endObject();  
return mapping;
}
}

 3、如果修改副本的数量?

import org.elasticsearch.action.admin.indices.settings.UpdateSettingsResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import com.donlianli.es.ESUtils;
/**
 * 更新副本个数
 * @author donlianli@126.com
 */
public class UpdateReplicaTest {
public static void  main(String[] argv) throws Exception{
Client client = ESUtils.getClient();
Settings settings =  ImmutableSettings.settingsBuilder()
                //可以更新的配置还有很多,见elasticsearch官网
                .put("number_of_replicas", 2).build();
//首先创建索引库
UpdateSettingsResponse  updateSettingsResponse = client.admin().indices()
.prepareUpdateSettings("testindex").setSettings(settings).execute().actionGet();
System.out.println(updateSettingsResponse);
}

}
对这类话题感兴趣?欢迎发送邮件至  donlianli@126.com
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值