Elasticsearch怎么修改索引字段類型?


     由於ElasticSearch沒有像mysql一樣可以直接字段數據類型的方法,因此需要通過創建中間索引:data_index_1,備份數據到中間索引:data_index_1,然后刪除原索引: data_index,重新創建正確數據類型索引:data_index,再把中間索引:data_index_1的數據備份到新創建索引:data_index。語句通過kibana的 dev_tools/console 執行。

 

操作步驟如下:

1. 創建一個中間索引
2. 向中間索引備份源索引的數據(mapping)
3. 查詢確認數據是否copy過去
4. 刪除有問題的索引
5. 重新創建同名的索引(★字段類型修改正確★)
6. 從中間索引還原到源索引的數據
7. 刪除中間索引

  

獲取索引mapping,可通過到Kibana查看索引的mapping,如圖:

 

參考修改腳本,kibana執行:

 1 # 1. 創建一個中間索引
 2 #創建索引
 3 PUT demo_metric_1/
 4 
 5 # 創建Mapping
 6 POST demo_metric_1/type/_mapping
 7 {  
 8     "type": {
 9       "properties": {        
10         "log_time_date": {
11           "type": "date",
12           "format": "epoch_millis"
13         },
14         .....        
15       }
16     } 
17 }
18 
19 
20 # 2. 向中間索引備份源索引的數據
21 
22 # 重建索引
23 POST _reindex
24 {
25   "source": {
26     "index": "demo_metric"
27   },
28   "dest": {
29     "index": "demo_metric_1"
30   }
31 }
32 
33 
34 # 3.查詢確認數據是否copy過去
35 GET /demo_metric/type/_search
36 
37 GET /demo_metric_1/type/_search
38 
39 
40 # 4.刪除有問題的索引
41 # 刪除有問題的索引
42 DELETE demo_metric
43 
44 
45 # 5.重新創建同名的索引(★字段類型修改正確★)
46 #創建索引
47 PUT demo_metric/
48 
49 # 創建Mapping
50 POST demo_metric/type/_mapping
51 {  
52     "type": {
53       "properties": {        
54         "log_time_date": {
55           "type": "date",
56           "format": "epoch_millis"
57         },
58         .....        
59       }
60     } 
61 }
62 
63 
64 # 6. 從中間索引還原到源索引的數據
65 # 重建索引
66 POST _reindex
67 {
68   "source": {
69     "index": "demo_metric_1"
70   },
71   "dest": {
72     "index": "demo_metric"
73   }
74 }
75 
76 
77 # 7. 刪除中間索引
78 DELETE demo_metric_1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM