Elasticsearch刪除索引字段方法總結(jié)(附實(shí)例代碼)
1.重建索引(推薦方式)
這是最安全、最常用的方法,因?yàn)?ES 不支持直接刪除字段。
// 1. 創(chuàng)建新索引,定義不含該字段的映射
PUT /new_index
{
"mappings": {
"properties": {
"keep_field1": {"type": "text"},
"keep_field2": {"type": "keyword"}
// 不包含要?jiǎng)h除的字段
}
}
}
// 2. 使用 Reindex API 遷移數(shù)據(jù)
POST /_reindex
{
"source": {
"index": "old_index"
},
"dest": {
"index": "new_index"
},
"script": {
"source": """
// 可以在這里移除字段或轉(zhuǎn)換數(shù)據(jù)
ctx._source.remove("field_to_remove");
""",
"lang": "painless"
}
}
// 3. 刪除舊索引,使用別名切換(可選)
POST /_aliases
{
"actions": [
{"remove": {"index": "old_index", "alias": "my_index"}},
{"add": {"index": "new_index", "alias": "my_index"}}
]
}2.使用 Update By Query 置空字段
如果不重建索引,可以將字段值設(shè)為 null:
POST /your_index/_update_by_query
{
"script": {
"source": "ctx._source.remove('field_to_delete')",
"lang": "painless"
},
"query": {
"exists": {
"field": "field_to_delete"
}
}
}3.刪除映射字段(7.x+)
ES 7.x+ 支持刪除映射中的字段類(lèi)型定義(但數(shù)據(jù)還在):
// 刪除字段的映射定義
PUT /your_index/_mapping
{
"properties": {
"field_to_delete": {
"type": null // 設(shè)置為 null 來(lái)刪除映射
}
}
}4.使用 Ingest Pipeline 過(guò)濾字段
查詢時(shí)動(dòng)態(tài)排除字段:
// 創(chuàng)建管道
PUT /_ingest/pipeline/remove-field
{
"processors": [
{
"remove": {
"field": "field_to_remove"
}
}
]
}
// 查詢時(shí)使用
GET /your_index/_search
{
"pipeline": "remove-field"
}5.刪除整個(gè)索引重新創(chuàng)建
如果數(shù)據(jù)量小或可以重新導(dǎo)入:
DELETE /your_index
PUT /your_index
{
"mappings": {
"properties": {
// 新映射,不包含要?jiǎng)h除的字段
}
}
}最佳實(shí)踐建議:
數(shù)據(jù)遷移方案:
# 1. 使用索引別名,實(shí)現(xiàn)零停機(jī)
PUT /old_index/_alias/my_index
# 2. 創(chuàng)建新索引
PUT /new_index
{
"mappings": { ... }
}
# 3. 使用reindex遷移
POST /_reindex?wait_for_completion=false
{
"source": {"index": "old_index"},
"dest": {"index": "new_index"},
"script": {
"source": "ctx._source.remove('unwanted_field')"
}
}
# 4. 切換別名
POST /_aliases
{
"actions": [
{"remove": {"index": "old_index", "alias": "my_index"}},
{"add": {"index": "new_index", "alias": "my_index"}}
]
}
# 5. 刪除舊索引(可選)
DELETE /old_index使用工具輔助:
# 使用 Elasticdump 遷移數(shù)據(jù)
elasticdump \
--input=http://localhost:9200/old_index \
--output=http://localhost:9200/new_index \
--type=data \
--transform='doc._source = Object.keys(doc._source)
.filter(key => key !== "field_to_remove")
.reduce((obj, key) => {
obj[key] = doc._source[key];
return obj;
}, {})'注意事項(xiàng):
無(wú)法物理刪除:ES 不支持直接從 Lucene 索引中刪除字段
存儲(chǔ)空間:即使置空字段,原始文檔仍占用存儲(chǔ)空間
重建索引:大數(shù)據(jù)量時(shí)可能耗時(shí)較長(zhǎng),建議在低峰期進(jìn)行
版本兼容:不同 ES 版本可能有不同的字段管理策略
備份數(shù)據(jù):操作前務(wù)必備份重要數(shù)據(jù)
根據(jù)你的數(shù)據(jù)量、業(yè)務(wù)需求和停機(jī)時(shí)間要求,選擇最合適的方法。對(duì)于生產(chǎn)環(huán)境,通常推薦使用重建索引+別名切換的方案。
總結(jié)
到此這篇關(guān)于Elasticsearch刪除索引字段方法的文章就介紹到這了,更多相關(guān)Elasticsearch刪除索引字段內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)WebSocket客戶端詳細(xì)步驟
這篇文章主要介紹了如何使用Java實(shí)現(xiàn)一個(gè)功能全面的WebSocket客戶端,包括引入依賴、創(chuàng)建客戶端類(lèi)、實(shí)現(xiàn)連接、發(fā)送和接收消息、處理復(fù)雜消息、實(shí)現(xiàn)心跳機(jī)制、重連策略、異常處理、線程安全的隊(duì)列以及測(cè)試和調(diào)試,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
獲取當(dāng)前時(shí)間方式(String形式)一行代碼搞定
文章指出部分公司用varchar存儲(chǔ)時(shí)間以避免轉(zhuǎn)換,但傳統(tǒng)方法需三行代碼,建議使用一行代碼通過(guò)LocalDateTime.now()和DateTimeFormatter直接輸出格式化時(shí)間字符串(如"yyyy-MM-ddHH:mm:ss"),簡(jiǎn)化開(kāi)發(fā)流程2025-07-07
Java中JSONObject與JSONArray的使用區(qū)別詳解
這篇文章主要介紹了Java中JSONObject與JSONArray的使用區(qū)別詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Java編程實(shí)現(xiàn)對(duì)象克?。◤?fù)制)代碼詳解
這篇文章主要介紹了Java編程實(shí)現(xiàn)對(duì)象克?。◤?fù)制)代碼詳解,涉及了克隆的原因,如何實(shí)現(xiàn)克隆,克隆的一般步驟,深克隆與淺克隆的介紹等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-11-11
mybatis逆向工程與分頁(yè)在springboot中的應(yīng)用及遇到坑
最近在項(xiàng)目中應(yīng)用到springboot與mybatis,在進(jìn)行整合過(guò)程中遇到一些坑,在此將其整理出來(lái),分享到腳本之家平臺(tái)供大家參考下2018-09-09
SpringCloud turbine監(jiān)控實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了SpringCloud turbine監(jiān)控實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Android studio按鈕點(diǎn)擊頁(yè)面跳轉(zhuǎn)詳細(xì)步驟
在Android應(yīng)用程序中,頁(yè)面跳轉(zhuǎn)是非常常見(jiàn)的操作,下面這篇文章主要給大家介紹了關(guān)于Android studio按鈕點(diǎn)擊頁(yè)面跳轉(zhuǎn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06

