Elastic Search 索引操作
新建索引 version 8.2
索引设置
索引设置分为静态设置和动态设置。
静态配置只能在索引创建时或对一个closed index进行操作。
动态配置可以通过 update-index-settings 来对一个活动的索引进行设置。
静态配置
- index.number_of_shards 索引的主分片数量,默认为1,只能在索引创建时配置,对closed index进行修改不会起作用。
- index.number_of_routing_shards
- 配合number_of_shards 一起使用,用于对文档路由到主分片的操作。
- 当进行 splitting 一个已存在的索引时,可以控制分割次数(和每个原始分片的分割个数)。路由分片值指定了在内部使用一致性hash去跨分片分配文档的散列空间 如原分片个数为5,
- , 分割因子可以为 2 或 3。即可以按照如下分裂:
- 5 -> 10 -> 30 (split by 2, then by 3)
- 5 -> 15 -> 30(split by 3, then by 2)
- 5-30 (split by 6)
- index.codec 默认压缩存储数据的压缩方式为 LZ4压缩,可以指定为
best_compression,更高的压缩比例,但降低了存储字段性能。
动态配置
- index.number_of_replicas 每个分片的副本数量,默认1.
- index.auto_expand_replicas 在集群中基于数据节点数量进行自动拓展的副本数。可以理解为动态副本数。默认为false,可以设置副本数范围如 0-5 或 0-all 。
- index.search.idle.after 一个分片不能接收搜索或获取请求的时间,知道它任务搜索空闲时。
- index.refresh_interval 执行刷新的时间间隔,默认1s,-1 禁止关闭自动刷新,将最近的变更应用到索引,已使搜索可见,如更新文档,并非立即可以搜索到,需要刷新后才可以。
- index.max_result_window 搜索结果的最大便宜量 from + size。 默认为10000.
- index.max_inner_result_window inner hits 搜索的最大结果
- index.max_rescore_window rescore 请求的最大 window_size 值
- index.max_terms_count terms 查询中 terms的最大值 ,默认65536。 index.query.default_field 一些查询的默认搜索字段,如 ,即默认在哪些字段上执行查询。* 代表在索引term级别的属性上查询。
索引分片分配操作
该模块给每个索引提供配置用来控制给节点分配索引分片
- Shard allocation filtering: 控制哪个分片分配给哪个节点.
- Delayed allocation: 为由于节点离线的而未分配的分片提供延迟分配
- Total shards per node: 每个节点中相同索引的分片数量的硬性限制
- Data tier allocation: Controls the allocation of indices to data tiers.
索引操作
创建索引
获取索引信息
获取setting信息
http://172.20.2.39:9200/.kibana_7.16.0_001/_settings
获取mapping 信息
http://172.20.2.39:9200/.kibana_7.16.0_001/_mapping
获取stat信息
GET /<target>/_stats/<index-metric>
GET /<target>/_stats
GET /_stats
curl -X GET "localhost:9200/my-index-000001/_stats/doc?pretty" { "_shards": { "total": 72, "successful": 72, "failed": 0 }, "_all": { "primaries": { "docs": { "count": 3902960221, "deleted": 845383676 } }, "total": { "docs": { "count": 3902960221, "deleted": 845383676 } } }, "indices": { "patent_cn_full_text_20220225": { "uuid": "74k1sPuHQJSGQHb286M8SA", "primaries": { "docs": { "count": 915543165, "deleted": 220324399 } }, "total": { "docs": { "count": 915543165, "deleted": 220324399 } } }, "en_patent": { "uuid": "uBCzz--XSDq4jW_AlimVMA", "primaries": { "docs": { "count": 2987417056, "deleted": 625059277 } }, "total": { "docs": { "count": 2987417056, "deleted": 625059277 } } } } }
更新索引
更新索引设置settings
可以实时更新动态配置信息
PUT /<target>/_settings
curl -X PUT "localhost:9200/my-index-000001/_settings?pretty" -H 'Content-Type: application/json' -d' { "index" : { "number_of_replicas" : 2 } } '
更新映射mapping信息
- 适用于新增field,对于已有属性,部分f属性不支持更改(Object 类型除外,可以添加properties)
curl -X PUT "localhost:9200/my-index-000001/_mapping?pretty" -H 'Content-Type: application/json' -d' { "properties": { "email": { "type": "keyword" } } } '
- 给已经存在的属性添加 multi-fields 属性
添加一个 类型为 text 的field
curl -X PUT "localhost:9200/my-index-000001?pretty" -H 'Content-Type: application/json' -d' { "mappings": { "properties": { "city": { "type": "text" } } } } '
给已经存在的field 添加 类型的 keyword 的 multi-fields
curl -X PUT "localhost:9200/my-index-000001/_mapping?pretty" -H 'Content-Type: application/json' -d' { "properties": { "city": { "type": "text", "fields": { "raw": { "type": "keyword" } } } } } '
- 对已存在的fields ,可以对支持更新的mapping参数 进行修改
创建field
curl -X PUT "localhost:9200/my-index-000001?pretty" -H 'Content-Type: application/json' -d' { "mappings": { "properties": { "user_id": { "type": "keyword", "ignore_above": 20 } } } } '
修改field 的 parameter
curl -X PUT "localhost:9200/my-index-000001/_mapping?pretty" -H 'Content-Type: application/json' -d' { "properties": { "user_id": { "type": "keyword", "ignore_above": 100 } } } '
- rename fields 重命名苏醒
重命名属性会使已索引数据失效,可以通过给field添加一个alias 来避免重命名
curl -X PUT "localhost:9200/my-index-000001?pretty" -H 'Content-Type: application/json' -d' { "mappings": { "properties": { "user_identifier": { "type": "keyword" } } } } '
给field创建新别名
curl -X PUT "localhost:9200/my-index-000001/_mapping?pretty" -H 'Content-Type: application/json' -d' { "properties": { "user_id": { "type": "alias", "path": "user_identifier" } } } '
刷新索引
可以使最近的变更对搜索可见,可认为是强制刷新
curl -X POST "localhost:9200/my-index-000001,my-index-000002/_refresh?pretty"
curl -X POST "localhost:9200/_refresh?pretty"
关闭索引
关闭索引后,不能索引文档或者搜索文档,关闭的文档可以不再保存用于索引和搜索相关的内部数据结果信息,可以减小集群的负荷。
curl -X POST "localhost:9200/my-index-000001/_close?pretty"

浙公网安备 33010602011771号