Elasticsearch 从入门到学会之四(索引API-索引管理)

889 阅读7分钟

在 Elasticsearch 中,索引可被认作一种文档的优化集合,且每个文档都是字段的集合,字段是包含你数据的键值对。

也就是:索引 → 文档 → 字段 → 数据。

一个 Elasticsearch 索引只是一个或多个物理分片的逻辑组,其中每个分片实际上是一个独立索引。通过将索引中的文档分布在多个分片上,并将这些分片分布在多个节点上。

也就是:节点 → 分片 → 文档(索引)。

分片分为主分片和副本,每个文档都属于一个主分片,然后副分片是主分片的复制。

核心概念类比:

概念类比
索引(indices)数据库(Database)
类型(type)表(Table)
文档(document)行(Row )
字段(field)列(Columns)

6.0.0开始,单个索引只能有一个类型,7.0.0开始不建议使用。8.0.0后完全不支持。

索引设置

索引设置分为:

  • 静态索引设置:创建时设置,或索引关闭后可修改的。
  • 动态索引设置:直接用更新索引 API 就可以修改。

相关设置参数见下表:

分类参数说明
静态索引设置(Static index settings)
number_of_shards索引的主分片数,默认为1,上限为1024。只能索引创建时设置
number_of_routing_shards用于拆分 (split)索引路由分片数
shard.check_on_startup打开前对分片进行检测,损坏会中断打开,默认值为false,值有: truechecksumfalse
codec默认使用LZ4压缩存储数据,可使用best_compression更高的压缩比,但降低存储字段性能。
routing_partition_size自定义可转换的路由分片数。 默认值1,必须小于number_of_shards(除非它也是1),只能索引创建时设置
soft_deletes.enabled索引软删除,默认true。支持版本:[6.5.0,7.6.0) ,低于集合的版本无此配置,大于集合的版本不推荐修改
soft_deletes.retention_lease.period分片历史过期前的保留周期,默认12h
load_fixed_bitset_filters_eagerly是否为嵌套查询预加载缓存筛选器,默认true,值有:truefalse
动态索引设置(Dynamic index setting)
hidden索引是否默认隐藏,如隐藏,需要在请求带参expand_wildcards才可查询。默认值为false,值有:truefalse
number_of_replicas每个主分片的副本(备份数),默认值为1
auto_expand_replicas基于集群中数据节点数量,自动扩展备份数,设置为以连接符分隔的下限和上限(如,0-5)或者使用 all 作为上限(如,0-all)。默认为 false(即禁用)。
search.idle.after搜索空闲前等待时间,默认值30s
refresh_interval刷新操作周期,让索引最近变更可被搜索。默认值1s,可设置为-1表示禁用刷新
max_result_window从索引搜索数据的最大值(即from + size的值),默认值10000
max_inner_result_window索引内部命中和最高命中聚集最大值(即from + size的值),默认值100
max_rescore_window搜索索引重打分(rescore)请求时的window_size最大值(默认与max_result_window一样默认10000
max_docvalue_fields_search允许查询docvalue_fields最大值,默认值100
max_script_fields允许查询script_fields最大值,默认值32
max_ngram_diff用于NGramTokenizerNGramTokenFilter的,min_grammax_gram之间的最大差值 ,默认值1
max_shingle_diff用于shingle token filter的,max_shingle_sizemin_shingle_size之间的最大差值,默认值3
max_refresh_listeners每个索引分片上的最大刷新监听器数量。
analyze.max_token_count用于_analyze API的最大标记值,默认值10000
highlight.max_analyzed_offset用于高亮请求的可分析最大字符数量,仅对无offsetsterm vectors的文本高亮请求有效。默认值1000000
max_terms_count用于词语查询最大词语数量,默认值65536
max_regex_length用于正则查询的最大正则长度,默认值1000
routing.allocation.enable控制索引的分片分配。默认值all,值有:allprimariesnew_primariesnone
routing.rebalance.enable允许索引分片重平衡,默认值all,值有:allprimariesreplicasnone
gc_deletes已删除文档的版本号可用于进一步版本化操作的时长,默认值60s
default_pipeline索引的默认摄取节点(ingest node)管道。可使用参数pipeline重载。特定参数_none表明不会运行摄取节点管道。
final_pipeline索引的最终摄取节点(ingest node)管道。特定参数_none表明不会运行摄取节点管道。

这么多设置,一般使用都不会进行配置。可能常用的有: index.number_of_shards(主分片数)、index.number_of_replicas(主分片的副本数)。

创建示例

1.创建一个索引,带配置number_of_shardsnumber_of_replicas 。主分片设置为 3,主分片的副本设置为 2。路径当中为索引名称,后面的json为请求体内容,索引名称要符合以下规则:

索引名字必须符合以下约定:

  • 只能是小写字符

  • 不能包含字符:\/*?"<>| (空格)、,#

  • 7.0 之前索引可以包含冒号(:),但在 7.0 之后不推荐。

  • 不能以 -_+ 开头

  • 不能是 ...

  • 长度不能超过 255 字节(注意是字节,所以多字节字符会更快达到 255 的限制)

  • 名字以 . 开头不推荐,除非由插件管理的隐藏索引和内部索引

PUT /my-index-000001

{

 "settings": {

  "index": {

   "number_of_shards": 3, 

   "number_of_replicas": 2

  }

 }

}

进一步,不用显示指定 index 部分:

PUT /my-index-000001
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 2
  }
}
  1. 加上映射配置一起
PUT /test
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "field1": { "type": "text" }
    }
  }
}
  1. 配置索引别名
{
  "aliases": {
    "alias_1": {},
    "alias_2": {
      "filter": {
        "term": { "user.id": "kimchy" }
      },
      "routing": "shard-1"
    }
  }
}

响应示例:

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "test"
}

acknowledged 表明在集群中索引是否成功创建,同时 shards_acknowledged 表明在超时之前,是否为每个分片启动了必需的分片副本数量。

需要注意的就是:两个值都可能是 false,但索引也是能创建成功的,这里只表明超时前的操作状态。

索引管理 API

进一步,与索引管理相关的API如下:

API文档Http方法URL路径参数查询参数请求体
创建索引PUT/{index}{index} 索引名字include_type_name
wait_for_active_shards
master_timeout
timeout
aliases
mappings
删除索引DELETE/{index}{index} 索引名字allow_no_indices
expand_wildcards
ignore_unavailable
master_timeout
timeout
获取索引GET/{index}{index}索引名字allow_no_indices
expand_wildcards
flat_settings
include_defaults
include_type_name
ignore_unavailable
local
master_timeout
索引存在HEAD/{index}{index}索引名字,可多个名字用逗号分隔allow_no_indices
expand_wildcards
flat_settings
include_defaults
include_type_name
ignore_unavailable
local
关闭索引POST/{index}/_close{index}索引名字,可多个名字用逗号分隔allow_no_indices
expand_wildcards
ignore_unavailable
wait_for_active_shards
master_timeout
timeout
开启索引POST/{index}/_open{index}索引名字,可多个名字用逗号分隔。也可使用 _all* 代表所有allow_no_indices
expand_wildcards
ignore_unavailable
wait_for_active_shards
master_timeout
timeout
收缩索引POSTPUT/{index}/_shrink/{target_index}{index} 源索引名字
{target_index} 目标索引名字
wait_for_active_shards
master_timeout
timeout
aliases
settings
max_primary_shard_size
拆分索引POSTPUT/{index}/_split/{target-index}{index} 源索引名字
{target_index} 目标索引名字
wait_for_active_shards
master_timeout
timeout
aliases
settings
复制索引POSTPUT/{index}/_clone/{target-index}{index} 源索引名字
{target_index} 目标索引名字
wait_for_active_shards
master_timeout
timeout
aliases
settings
翻转索引POST/{rollover-target}/_rollover 或 /{rollover-target}/_rollover/{target-index}{rollover-target} 待翻转索引名字。{target-index} 可选的目标索引名字dry_run
include_type_name
wait_for_active_shards
master_timeout
timeout
aliases
conditions
mappings
settings
冻结索引POST/{index}/_freeze{index}索引名字
解冻索引POST/{index}/_unfreeze{index}索引名字
解析索引GET/_resolve/index/{name}{name}索引名字,可多个名字用逗号分隔expand_wildcards

基于以上表的数据,我们可以进一步总结如下。

  1. 索引的 CRUD 操作为:
  • 增:创建索引 PUT /{index}
  • 删:删除索引 DELETE /{index}
  • 查:获取索引 GET /{index}
  • 改:没有直接修改索引的 API,有修改索引属性的 API,且需要看属性是静态属性还是动态属性等。
  1. 索引存在性操作:
  • 索引存在 HEAD /{index}

elasticsearch 中有这个索引,就响应 200,没有这个索引,就响应 404

  1. 索引开、关操作:
  • 关闭 POST /{index}/_close
  • 开启 POST /{index}/_open

关闭索引后,将不能读写。开关状态可以在 GET /_cat/indices/customer?v 中查看,值为 openclose

  1. 索引冻结、解冻操作:
  • 冻结 POST /{index}/_freeze
  • 解冻 POST /{index}/_unfreeze

冻结索引后,将不能写。冻结状态可以在 GET /{index} 中查看,位于 {index}->settings->index->frozen,值为falsetrue

  1. 索引缩、扩操作:
  • 收缩 POST /{index}/_shrink/{target_index}
  • 拆分 POST /{index}/_split/{target_index}

生成一个新的索引,并减少或增加索引的分片数。

  1. 其他操作:
  • 复制 POST /{index}/_clone/{target_index}

复制生成一个新的索引。

  • 翻转 POST /{rollover-target}/_rollover 或 /{rollover-target}/_rollover/{target-index}

符合指定的条件时,将索引别名的写数据指向新的索引,以达到类似数据归档的效果。

  • 解析 GET /_resolve/index/{name}

获取指定名字的相关索引的名字、别名、数据流。

通过以上总结,我们可以看出来,针对索引管理的 API,基本只能索引本身直接操作,而不怎么涉及更明细的东西。且,我们可以再次发现,Elasticsearch 的操作关键字,都是以下划线 _ 开头的,比如 _clone

下一篇我们将学习索引的映射管理。

欢迎关注我的博客:阿呜的边城

欢迎关注我的公众号:阿呜的编程