Elastic Search 索引操作

新建索引 version 8.2

索引设置

索引设置分为静态设置动态设置

静态配置只能在索引创建时或对一个closed index进行操作。

动态配置可以通过 update-index-settings 来对一个活动的索引进行设置。

静态配置

  • index.number_of_shards 索引的主分片数量,默认为1,只能在索引创建时配置,对closed index进行修改不会起作用。
  • index.number_of_routing_shards  number_of_routing_shards = 30
         
      , 分割因子可以为 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级别的属性上查询。

索引分片分配操作

该模块给每个索引提供配置用来控制给节点分配索引分片

索引操作

创建索引

获取索引信息

获取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
                }
            }
        }
    }
}
View Code

 

更新索引

更新索引设置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"
      }
    }
  }
}
'
View Code

给已经存在的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"
        }
      }
    }
  }
}
'
View Code
  • 对已存在的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
      }
    }
  }
}
'
View Code

修改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
    }
  }
}
'
View Code
  • 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"
      }
    }
  }
}
'
View Code

给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"
    }
  }
}
'
View Code

 

刷新索引

可以使最近的变更对搜索可见,可认为是强制刷新

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"

 

posted @ 2022-06-07 14:16  hhanhao  阅读(904)  评论(0)    收藏  举报