Elasticsearch搜索引擎第五篇-索引管理


官网索引介绍: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices.html

创建索引

指定分片创建

PUT twitter {
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}

或者简单的写为:

PUT twitter {
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}
  • number_of_shards:表示分片数,默认5,最大1024
  • number_of_replicas:每个分片的备份数,默认1

指定mapping创建

PUT test {
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "_doc" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}

指定别名创建

PUT test {
    "aliases" : {
        "alias_1" : {},
        "alias_2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        }
    }
}

返回结果说明

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "test"
}
  • acknowledged:true表示索引创建成功
  • shards_acknowledged:所需数量的分片+副本启动成功
  • 以上两个值也会返回false,如果没有返回错误信息,则表示等待时间到了超时时间就直接返回了

删除索引

DELETE /twitter  #删除单个索引
DELETE /twitter1,twitter2  #删除多个索引
DELETE /twitter*  #通配符,删除twitter开头的索引
DELETE /_all  #删除所有索引,慎用!
DELETE /*  #删除所有索引,慎用!
  • 无法通过索引的别名进行删除索引的操作

查看索引定义信息

GET /twitter  #查看twitter索引的所有定义信息
GET /twitter/_settings  #查看该索引的_settings部分的定义
GET /twitter/_mapping  #查看该索引的_mapping部分的定义
HEAD twitter  #404表示不存在,200表示存在

修改索引的settings信息

索引的设置信息分为静态信息和动态信息两部分,静态信息不可更改,如索引的分片数,动态信息可以修改。详细的设置项请参考官网:
https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings


修改twitter索引的备份数:

PUT /twitter/_settings {
    "index" : {
        "number_of_replicas" : 2
    }
}
  • PUT /_settings:更新所有索引的_settings
  • PUT /{index}/_settings:更新一个或多个索引的_settings

设置回默认值,用null

PUT /twitter/_settings {
    "index" : {
        "refresh_interval" : null
    }
}

设置索引的读写

index.blocks.read_only:设为true,则索引以及索引的元数据只可读
index.blocks.read_only_allow_delete:设为true,只读时允许删除
index.blocks.read:设为true,则不可读
index.blocks.write:设为true,则不可写
index.blocks.metadata:设为true,则索引元数据不可读写

打开/关闭索引

关闭的索引不能进行读写操作,几乎不占集群开销。
关闭的索引可以打开,打开走的是正常的恢复流程。

POST /my_index/_close
POST /my_index/_open

索引模板

创建索引模板

在创建索引时,为每个索引写定义信息可能是一件繁琐的事情,特别是如果有相同的settings、mapping等。ES提供了索引模板功能,让你可以定义一个索引模板,模板中定义好settings、mapping、以及一个模式定义来匹配创建的索引。模板只在索引创建时被参考,修改模板不会影响已创建的索引。
定义一个模板:

PUT _template/template_1 {
  "index_patterns": ["te*", "bar*"],
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "keyword"
        },
        "created_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}
  • 定义了一个名称为template_1的索引模板
  • index_patterns:指定了哪些索引会使用该模板,允许通配符格式。这里表示创建索引是,te或bar开头的索引会使用该模板

查看索引模板

GET /_template/template_1
GET /_template/temp*
GET /_template/template_1,template_2
GET /_template

删除索引模板

DELETE /_template/template_1

索引监控

查看索引状态信息

官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html

GET /_stats
GET /index1/_stats
GET /index1,index2/_stats

查看索引段信息

官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-segments.html

GET /_segments
GET /index1/_segments
GET /index1,index2/_segments

查看索引恢复信息

官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-recovery.html

GET /_recovery?human
GET index1,index2/_recovery?human

查看索引分片存储的信息

官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-shards-stores.html

GET /index1/_shard_stores
GET /index1,index2/_shard_stores
GET /_shard_stores
GET /_shard_stores?status=green

索引状态管理

清理缓存

POST /index1/_cache/clear
POST /index1,index2/_cache/clear 
POST /_cache/clear

刷新

POST /index1/_refresh
POST /index1,index2/_refresh
POST /_refresh

Flush内存数据持久化

POST index1/_flush/

强制段合并

POST /index1/_forcemerge?only_expunge_deletes=false&max_num_segments=100&flush=true
POST /index1,index2/_forcemerge
POST /_forcemerge
  • only_expunge_deletes:是否只合并含有删除文档的段,默认false
  • max_num_segments:合并为几个段,默认1
  • flush:合并后是否刷新,默认true

索引别名

如果希望一次查询可查询多个索引,如果希望通过索引的视图来操作索引,就像数据库库中的视图一样,就可以使用索引别名。索引的别名机制,就是让我们可以以视图的方式来操作集群中的索引,这个视图可是多个索引,也可是一个索引或索引的一部分。

创建别名

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } }
    ]
}

删除索引

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } }
    ]
}

批量新增索引

POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}

为多个索引定义别名

方式一:

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}

方式二:

POST /_aliases
{
    "actions" : [
        { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
    ]
}

方式三(通配符):

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
    ]
}

注意:在这种情况下,别名是一个时间点的别名,它将对所有匹配的当前索引进行别名,当添加/删除与此模式匹配的新索引时,它不会自动更新

带过滤器的别名

过滤器通过Query DSL来定义,将作用于通过该别名来进行的所有Search, Count, Delete By Query and More Like This 操作。
第一步:首先索引中需要有字段

PUT /test1
{
  "mappings": {
    "type1": {
      "properties": {
        "user" : {
          "type": "keyword"
        }
      }
    }
  }
}

第二步:增加带有过滤器的索引

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test1",
                 "alias" : "alias2",
                 "filter" : { "term" : { "user" : "kimchy" } }
            }
        }
    ]
}

带路由(指定分片)的别名

可在别名定义中指定路由值,可和filter一起使用,用来限定操作的分片,避免不需要的其他分片操作。
指定带有路由分片的别名,查询、索引都指向该分片:

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias1",
                 "routing" : "1"
            }
        }
    ]
}

为搜索、索引指定不同的路由分片:

POST /_aliases
{
    "actions" : [
        {
            "add" : {
                 "index" : "test",
                 "alias" : "alias2",
                 "search_routing" : "1,2",
                 "index_routing" : "2"
            }
        }
    ]
}

以PUT方式来定义一个索引

PUT /{index}/_alias/{name}

PUT /logs_201305/_alias/2013

PUT /users
{
    "mappings" : {
        "user" : {
            "properties" : {
                "user_id" : {"type" : "integer"}
            }
        }
    }
}

PUT /users/_alias/user_12
{
    "routing" : "12",
    "filter" : {
        "term" : {
            "user_id" : 12
        }
    }
}

查看别名定义信息

GET /{index}/_alias/{alias}
GET /logs_20162801/_alias/*
GET /_alias/2016
GET /_alias/20*

Shrink Index 收缩索引

索引的分片数是不可更改的,如要减少分片数可以通过收缩方式收缩为一个新的索引。新索引的分片数必须是原分片数的因子值,如原分片数是8,则新索引的分片数可以为4、2、1 。

  • 先把所有主分片都转移到一台主机上,即确保一台主机上有该索引的所有数据
  • 在这台主机上创建一个新索引,分片数较小,其他设置和原索引一致
  • 把原索引的所有分片,复制(或硬链接)到新索引的目录下
  • 对新索引进行打开操作恢复分片数据
  • (可选)重新把新索引的分片均衡到其他节点上

收缩前的准备工作:

  • 将原索引设置为只读
  • 将原索引各分片的一个副本重分配到同一个节点上,并且要是健康绿色状态

以上两个步骤可以通过以下命令达成:

PUT /my_source_index/_settings {
  "settings": {
    "index.routing.allocation.require._name": "shrink_node_name", 
    "index.blocks.write": true
  }
}
  • shrink_node_name:新的es节点,即将原索引各分片的一个副本重新分配到该节点上
  • index.blocks.write:阻止写操作,只读

进行收缩:

POST my_source_index/_shrink/my_target_index {
  "settings": {
    "index.number_of_replicas": 1,
    "index.number_of_shards": 1, 
    "index.codec": "best_compression" 
  }}


监控收缩状态:

GET _cat/recovery?v
GET _cluster/health

Split Index 拆分索引

当索引的每个分片数据容量过大时,需要增加分片数量,则可以通过拆分操作将索引拆分为一个倍数分片数的新索引。能拆分为几倍由创建索引时指定的index.number_of_routing_shards 路由分片数决定。这个路由分片数决定了根据一致性hash路由文档到分片的散列空间。
如index.number_of_routing_shards = 30 ,指定的分片数是5,则可按如下倍数方式进行拆分:

  • 5 → 10 → 30 (split by 2, then by 3)
  • 5 → 15 → 30 (split by 3, then by 2)
  • 5 → 30 (split by 6)
  • 拆分的数量必须为分片数的倍数,且为index.number_of_routing_shards的因子

注意:只有在创建时指定了index.number_of_routing_shards 的索引才可以进行拆分,ES7开始将不再有这个限制


准备一个索引来做拆分

PUT my_source_index {
    "settings": {
        "index.number_of_shards" : 1,
        "index.number_of_routing_shards" : 8 
    }
}

先设置索引只读:

PUT /my_source_index/_settingsc{
  "settings": {
    "index.blocks.write": true 
  }
}

拆分:

POST my_source_index/_split/my_target_index {
  "settings": {
    "index.number_of_shards": 2
  }
}

监控拆分过程:

GET _cat/recovery?v
GET _cluster/health

Rollover Index 别名滚动

对于有时效性的索引数据,如日志,过一定时间后,老的索引数据就没有用了。我们可以像数据库中根据时间创建表来存放不同时段的数据一样,在ES中也可用建多个索引的方式来分开存放不同时段的数据。比数据库中更方便的是ES中可以通过别名滚动指向最新的索引的方式,让你通过别名来操作时总是操作的最新的索引。


ES的rollover index API 让我们可以根据满足指定的条件(时间、文档数量、索引大小)创建新的索引,并把别名滚动指向新的索引。
注意:这时的别名只能是一个索引的别名
示例:

PUT /logs-000001  {
  "aliases": {
    "logs_write": {}
  }
}

# Add > 1000 documents to logs-000001

POST /logs_write/_rollover {
  "conditions": {
    "max_age":   "7d",
    "max_docs":  1000,
    "max_size":  "5gb"
  }
}
  • 创建一个名字为logs-000001、别名为logs_write的索引
  • conditions:指定如果别名logs_write指向的索引是7天前(含)创建的或索引的文档数>=1000或索引的大小>= 5gb,则会创建一个新索引 logs-000002,并把别名logs_writer指向新创建的logs-000002索引

命名规则:
如果索引的名称是-数字结尾,如logs-000001,则新建索引的名称也会是这个模式,数值增1。
如果索引的名称不是**-数值结尾*,则在请求rollover api时需指定新索引的名称:

POST /my_alias/_rollover/my_new_index_name {
  "conditions": {
    "max_age":   "7d",
    "max_docs":  1000,
    "max_size": "5gb"
  }
}

在索引名称中使用Date math时间表达式
如果你希望生成的索引名称中带有日期,如logstash-2016.02.03-1 ,则可以在创建索引时采用时间表达式来命名:

# PUT /<logs-{now/d}-1> with URI encoding:
PUT /%3Clogs-%7Bnow%2Fd%7D-1%3E 
{
  "aliases": {
    "logs_write": {}
  }
}

PUT logs_write/_doc/1
{
  "message": "a dummy log"
}

POST logs_write/_refresh

# Wait for a day to pass

POST /logs_write/_rollover 
{
  "conditions": {
    "max_docs":   "1"
  }
}

Rollover时可对新的索引作定义:

PUT /logs-000001
{
  "aliases": {
    "logs_write": {}
  }
}

POST /logs_write/_rollover
{
  "conditions" : {
    "max_age": "7d",
    "max_docs": 1000,
    "max_size": "5gb"
  },
  "settings": {
    "index.number_of_shards": 2
  }
}

可以在实际的rollover操作前先来一个排练,使用dry_run命令,排练不会创建索引,只是检测条件是否满足

POST /logs_write/_rollover?dry_run
{
  "conditions" : {
    "max_age": "7d",
    "max_docs": 1000,
    "max_size": "5gb"
  }
}

注意:rollover是你请求它才会进行操作,并不是自动在后台进行的。你可以周期性地去请求它

猜你喜欢

转载自blog.csdn.net/supermao1013/article/details/84036463