在 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,值有: true、checksum、false | |
| codec | 默认使用LZ4压缩存储数据,可使用best_compression更高的压缩比,但降低存储字段性能。 | |
| routing_partition_size | 自定义可转换的路由分片数。 默认值1,必须小于number_of_shards(除非它也是1),只能索引创建时设置。 | |
索引软删除,默认true。支持版本:[6.5.0,7.6.0) ,低于集合的版本无此配置,大于集合的版本不推荐修改 | ||
| soft_deletes.retention_lease.period | 分片历史过期前的保留周期,默认12h | |
| load_fixed_bitset_filters_eagerly | 是否为嵌套查询预加载缓存筛选器,默认true,值有:true、false | |
| 动态索引设置(Dynamic index setting) | ||
| hidden | 索引是否默认隐藏,如隐藏,需要在请求带参expand_wildcards才可查询。默认值为false,值有:true、false | |
| 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 | 用于NGramTokenizer和NGramTokenFilter的,min_gram和max_gram之间的最大差值 ,默认值1 | |
| max_shingle_diff | 用于shingle token filter的,max_shingle_size和min_shingle_size之间的最大差值,默认值3 | |
| max_refresh_listeners | 每个索引分片上的最大刷新监听器数量。 | |
| analyze.max_token_count | 用于_analyze API的最大标记值,默认值10000 | |
| highlight.max_analyzed_offset | 用于高亮请求的可分析最大字符数量,仅对无offsets和term vectors的文本高亮请求有效。默认值1000000 | |
| max_terms_count | 用于词语查询最大词语数量,默认值65536 | |
| max_regex_length | 用于正则查询的最大正则长度,默认值1000 | |
| routing.allocation.enable | 控制索引的分片分配。默认值all,值有:all、primaries、new_primaries、none | |
| routing.rebalance.enable | 允许索引分片重平衡,默认值all,值有:all、primaries、replicas、none | |
| 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_shards 和 number_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
}
}
- 加上映射配置一起
PUT /test
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"field1": { "type": "text" }
}
}
}
- 配置索引别名
{
"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如下,涉及索引本身的 CRUD 等操作:
| API文档 | Http方法 | URL | 路径参数 | 查询参数 | 请求体 |
|---|---|---|---|---|---|
| 创建索引 (opens new window) | PUT | /{index} | {index} 索引名字 | include_type_name wait_for_active_shardsmaster_timeouttimeout | aliases mappings |
| 删除索引 (opens new window) | DELETE | /{index} | {index} 索引名字 | allow_no_indicesexpand_wildcardsignore_unavailablemaster_timeouttimeout | 无 |
| 获取索引 (opens new window) | GET | /{index} | {index}索引名字 | allow_no_indicesexpand_wildcardsflat_settingsinclude_defaultsinclude_type_nameignore_unavailablelocalmaster_timeout | 无 |
| 索引存在 (opens new window) | HEAD | /{index} | {index}索引名字,可多个名字用逗号分隔 | allow_no_indicesexpand_wildcardsflat_settingsinclude_defaultsinclude_type_nameignore_unavailablelocal | 无 |
| 关闭索引 (opens new window) | POST | /{index}/_close | {index}索引名字,可多个名字用逗号分隔 | allow_no_indicesexpand_wildcardsignore_unavailablewait_for_active_shardsmaster_timeouttimeout | 无 |
| 开启索引 (opens new window) | POST | /{index}/_open | {index}索引名字,可多个名字用逗号分隔。也可使用 _all 或 * 代表所有 | allow_no_indicesexpand_wildcardsignore_unavailablewait_for_active_shardsmaster_timeouttimeout | 无 |
| 收缩索引 (opens new window) | POST 或 PUT | /{index}/_shrink/{target_index} | {index} 源索引名字{target_index} 目标索引名字 | wait_for_active_shardsmaster_timeouttimeout | aliasessettingsmax_primary_shard_size |
| 拆分索引 (opens new window) | POST 或 PUT | /{index}/_split/{target-index} | {index} 源索引名字{target_index} 目标索引名字 | wait_for_active_shardsmaster_timeouttimeout | aliasessettings |
| 复制索引 (opens new window) | POST 或 PUT | /{index}/_clone/{target-index} | {index} 源索引名字{target_index} 目标索引名字 | wait_for_active_shardsmaster_timeouttimeout | aliasessettings |
| 翻转索引 (opens new window) | POST | /{rollover-target}/_rollover 或 /{rollover-target}/_rollover/{target-index} | {rollover-target} 待翻转索引名字。{target-index} 可选的目标索引名字 | dry_runinclude_type_namewait_for_active_shardsmaster_timeouttimeout | aliasesconditionsmappingssettings |
| 冻结索引 (opens new window) | POST | /{index}/_freeze | {index}索引名字 | 无 | 无 |
| 解冻索引 (opens new window) | POST | /{index}/_unfreeze | {index}索引名字 | 无 | 无 |
| 解析索引 (opens new window) | GET | /_resolve/index/{name} | {name}索引名字,可多个名字用逗号分隔 | expand_wildcards | 无 |
基于以上表中,我们可以再总结一下。
索引的 CRUD 操作为:
- 增:创建索引
PUT /{index} - 删:删除索引
DELETE /{index} - 查:获取索引
GET /{index} - 改:不涉及直接的 API,且只能修改索引的属性,而且需要看属性是静态属性还是动态属性等。
索引存在性操作:
- 索引存在
HEAD /{index}
elasticsearch 中有这个索引,就响应
200,没有这个索引,就响应404。
索引开、关操作:
- 关闭
POST /{index}/_close - 开启
POST /{index}/_open
关闭索引后,将不能读写。开关状态可以在
GET /_cat/indices/customer?v中查看,值为open或close
索引冻结、解冻操作:
- 冻结
POST /{index}/_freeze - 解冻
POST /{index}/_unfreeze
冻结索引后,将不能写。冻结状态可以在
GET /{index}中查看,位于{index}->settings->index->frozen,值为false或``true`。
索引缩、扩操作:
- 收缩
POST /{index}/_shrink/{target_index} - 拆分
POST /{index}/_split/{target_index}
减少或增加索引的分片数。
其他操作:
复制
POST /{index}/_clone/{target_index}翻转
POST /{rollover-target}/_rollover 或 /{rollover-target}/_rollover/{target-index}解析
GET /_resolve/index/{name}