一、修改副本數(shù)
PUT test/_settings
{
? ? "index": {
? ? ? ? "number_of_replicas" : 1
? ? }
}
二、修改分片數(shù)
ElasticSearch中的數(shù)據(jù)會(huì)被分別存儲(chǔ)在不同的分片上,索引庫的分片數(shù)量是在索引庫創(chuàng)建的時(shí)候通過settings去設(shè)置的,如果不設(shè)置,分片數(shù)默認(rèn)是5,分片數(shù)一旦確定就不能改變。如果執(zhí)行下面語句會(huì)報(bào)錯(cuò)
PUT test/_settings
{
? ? "index": {
? ? ? ? "number_of_shards" : 1
? ? }
}
隨著數(shù)據(jù)量的增大,每個(gè)分片中的數(shù)據(jù)量也會(huì)不斷增加,為了不使每個(gè)分片中的數(shù)據(jù)量過大,就需要增加分片的數(shù)量,但是分片數(shù)在索引庫創(chuàng)建之初就已經(jīng)確定,并且不能改變。
為了解決這個(gè)問題,ElasticSearch中設(shè)置了重新索引機(jī)制來實(shí)現(xiàn)。本文將通過一個(gè)案例進(jìn)行演示如何通過重新索引修改分片數(shù)。
什么是重新索引
簡單來說,重新索引就是創(chuàng)建一個(gè)和原索引庫結(jié)構(gòu)屬性都基本一樣的新的索引庫,然后將原索引庫中的數(shù)據(jù)復(fù)制到新的索引庫當(dāng)中。在新的索引庫中,除了需要變更的地方,比如某些字段的數(shù)據(jù)類型和分片數(shù),其他的所有屬性都一樣。
重新索引的具體實(shí)現(xiàn)
1. 創(chuàng)建學(xué)生索引庫
下列是創(chuàng)建索引庫students1的命令,這里創(chuàng)建一個(gè)關(guān)于學(xué)生信息的索引庫,指定分片數(shù)為3。
DELETE students1
?
PUT students1
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"student":{
"dynamic":"strict",
"properties":{
"id":{"type": "text", "store": true},
"name":{"type": "text","store": true},
"age":{"type": "integer","store": true},
"times": {"type": "text", "index": false}
}
}
}
}
2. 插入數(shù)據(jù)
創(chuàng)建好索引庫students1后,插入一條數(shù)據(jù),為后面的驗(yàn)證做數(shù)據(jù)準(zhǔn)備。
PUT students1/student/1?
{ "id" : "1",
?"name" : "張三",?
?"age" : 20 ,?
?"times" : "2020-02-01"?
}
3. 新建索引庫students2
下列是創(chuàng)建索引庫students2的命令,與students1不同的地方是指定分片數(shù)為5,其他的屬性都和students1一樣。
DELETE students2?
?
PUT students2?
{ "settings":
? ?{ "number_of_shards":5,?
? ?"number_of_replicas":1?
? ?},
? "mappings":
? { "student":{ "dynamic":"strict", "properties":{ "id":{"type": "text", "store": true}, "name":{"type": "text","store": true}, "age":{"type": "integer","store": true}, "times": {"type": "text", "index": false} } }?
?} ?
}
4. 拷貝數(shù)據(jù)
通過下面命令,將students1中的數(shù)據(jù)復(fù)制到students2中,這是重新索引的核心步驟。
POST _reindex?
{?
?"source": { ?
? ?"index": "students1"?
? },?
?"dest": {?
? ?"index": "students2"?
? }?
}?
小結(jié)
本文通過一個(gè)簡單的案例,先創(chuàng)建索引庫students1和索引庫students2,在創(chuàng)建students2的語句中指定新的分片數(shù),然后將students1中數(shù)據(jù)復(fù)制到students2中,實(shí)現(xiàn)重新索引,以達(dá)到修改索引庫分片數(shù)的目的。如果生產(chǎn)中需要實(shí)現(xiàn)重新索引,需要按照實(shí)際
索引庫的情況修改創(chuàng)建索引庫的命令,在實(shí)現(xiàn)重新索引之后,要將程序中的索引庫指向新的索引庫。另外,本文中的操作命令是通過在kibana中的命令窗口中執(zhí)行的,本文重點(diǎn)在介紹如何通過重新索引修
三、增加字段
PUT ods_big_epid/_mapping/ods_big_epid
{
? "properties":{
? ? "reportDate":{
? ? ? "type": "date",
? ? ? "format":"yyyy-MM-dd HH:mm:ss"
? ? }
? }文章來源:http://www.zghlxwxcb.cn/news/detail-450453.html
————————————————
版權(quán)聲明:本文為CSDN博主「半桶水的碼農(nóng)」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_44688301/article/details/116127245文章來源地址http://www.zghlxwxcb.cn/news/detail-450453.html
到了這里,關(guān)于ElasticSearch修改分片數(shù)和副本數(shù)及增加字段的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!