es的索引管理

1. 索引创建

  1. PUT index
  2. {
  3. "settings":{
  4. "number_of_shards":3,
  5. "number_of_replicas":1
  6. },
  7. "mappings":{
  8. "properties":{
  9. "title":{
  10. "type":"text",
  11. "analyzer":"ik_max_words",
  12. "search_analyzer":"ik_smart"
  13. },
  14. "tag":{
  15. "type":"keyword"
  16. }
  17. }
  18. }
  19. }

2. 添加字段

  1. PUT index/_mapping
  2. {
  3. "properties":{
  4. "indexAt":{
  5. "type":"date",
  6. "format":"yyyy-MM-dd HH:mm:ss"
  7. }
  8. }
  9. }

3. 删除索引

  1. DELETE index

4. 打开/关闭索引

4.1 打开索引

  1. POST index/_open

4.2 关闭索引

  1. POST index/_close

5. 重建索引

  1. POST /_reindex
  2. {
  3. "source":{
  4. "index":"sourceIndexName",
  5. "query":{
  6.  
  7. },
  8. "sort":{
  9. "fieldName":"asc/desc"
  10. },
  11. "size":5000,
  12. "_source":[
  13. "fieldName1",
  14. "fieldName2",
  15. "fieldName3",
  16. ]
  17. },
  18. "dest":{
  19. "index":"destIndexName",
  20. "version_type":"internal(default)/external",
  21. "op_type":"create"
  22. }
  23. }

说明:

  • version_type
  • internal:表示迁移全部数据,且完全覆盖冲突文档(即使目标文档版本新于源索引文档)。
  • external:表示迁移全部数据,且更新旧版本冲突文档。
  • op_type:
  • create:表示仅创建目标索引不存在的文档。

6. 别名管理

6.1 添加别名

  1. POST /_aliases
  2. {
  3. "actions":[
  4. {
  5. "add":{
  6. "index":"indexName1",
  7. "alias":"aliasName"
  8. }
  9. },
  10. {
  11. "add":{
  12. "index":"indexName2",
  13. "alias":"aliasName"
  14. }
  15. }
  16. ]
  17. }

6.2 删除别名

  1. POST /_aliases
  2. {
  3. "actions":[
  4. {
  5. "remove":{
  6. "index":"indexName",
  7. "alias":"aliasName"
  8. }
  9. }
  10. ]
  11. }

6.3 重命名

  1. POST /_aliases
  2. {
  3. "actions":[
  4. {
  5. "remove":{
  6. "index":"indexName",
  7. "alias":"aliasName1"
  8. }
  9. },
  10. {
  11. "add":{
  12. "index":"indexName",
  13. "alias":"aliasName2"
  14. }
  15. }
  16. ]
  17. }

7. 合并索引

  1. POST index/_forcemerge?max_num_segments=1

8. 缩小索引

8.1 先把索引设置为只读

  1. PUT source_index/_setting
  2. {
  3. "settings":{
  4. "index.routing.allocation.require._name":"node-1",
  5. "index.blocks.write":true
  6. }
  7. }

8.2 执行shrink

  1. POST source_index/_shrink/target_index
  2. {
  3. "settings":{
  4. "index.number_of_replicas":1,
  5. "index.number_of_shards":1,
  6. "index.codec":"best_compression"
  7. }
  8. }

文章来自:java架构

发布了277 篇原创文章 · 获赞 65 · 访问量 38万+

猜你喜欢

转载自blog.csdn.net/ailiandeziwei/article/details/104620460