Elasticsearch 高手之路
  • Introduction
  • First Chapter
  • 1.ElasticSearch 5.x 安装
    • 1.1.Window 环境
    • 1.2.Linux 环境
  • 2.基础学前班
  • 3.基础
    • 3.1.配置文件
    • 3.2.Mapping
      • 3.2.1.字段的数据类型
        • 3.2.1.1.核心数据类型
        • 3.2.1.2.复合数据类型
        • 3.2.1.3.Geo地理数据类型
        • 3.2.1.4.特定数据类型
      • 3.2.2.Meta-Fields(元字段)
        • _index,_uid,_type,_id 元字段
        • _source,_size 元字段
        • _all, _field_names元字段
        • _parent,_routing 元字段
        • _meta 元字段
      • 3.2.3.Mapping parameters(映射参数)
        • analyzer(分析器)
        • normalizer(归一化)
        • boost(提升)权重
        • Coerce(强制类型转换)
        • copy_to(合并参数)
        • doc_values(文档值)
        • dynamic(动态设置)
        • enabled(开启字段)
        • fielddata(字段数据)
        • format (日期格式)
        • ignore_above(忽略超越限制的字段)
        • ignore_malformed(忽略格式不对的数据)
        • include_in_all(_all 查询包含字段)
        • index_options(索引设置)
        • index (索引)
        • fields(字段)
        • norms (标准信息)
        • null_value(空值)
        • position_increment_gap(短语位置间隙)
        • properties (属性)
        • search_analyzer (搜索分析器)
        • similarity (相似度模型)
        • store(存储)
        • term_vectors(词根信息)
      • 3.2.4.Dynamic Mapping(动态映射)
        • _default_ mapping(mapping中的_default_)
        • Dynamic field mapping(动态字段映射)
        • Dynamic templates(动态模板)
        • Override default template(覆盖默认模板)
    • 3.3. Analysis(分析)
      • 3.3.1.Anatomy of an analyzer(分析器的分析)
      • 3.3.2.Testing analyzers(测试分析器)
      • 3.3.3.Analyzers(分析器)
        • Configuring built-in analyzers(配置内置分析器)
        • Standard Analyzer(标准分析器)
        • Simple Analyzer(简单分析器)
        • Whitespace Analyzer(空格分析器)
        • Stop Analyzer(停止词分词器)
        • Keyword Analyzer(关键词分析器)
        • Pattern Analyzer(模式分析器)
        • Language Analyzers(语言分析器)
        • Fingerprint Analyzer(指纹分析器)
        • Custom Analyzer(自定义分析器)
      • 3.3.4. Tokenizers(分词器)
        • Standard Tokenizer(标准分词器)
        • Letter Tokenizer
        • Lowercase Tokenizer (小写分词器)
        • Whitespace Tokenizerr (空格分词器)
        • UAX URL Email Tokenizer
        • Classic Tokenizer
        • Thai Tokenizer(泰语分词器)
        • NGram Tokenizer
        • Edge NGram Tokenizer
        • Keyword Tokenizer(关键词分词器)
        • Pattern Tokenizer(模式分词器)
        • Path Hierarchy Tokenizer(路径层次分词器)
      • 3.3.5.Token Filters(词语过滤器)
      • 3.3.5.补充1:Token Filters(词语过滤器)
      • 3.3.5.补充2:Token Filters(词语过滤器)
      • 3.3.6.Character Filters(字符过滤器)
        • HTML Strip Character Filter(HTML标签过滤)
        • Mapping Character Filter(字符替换映射)
        • Pattern Replace Character Filter(正则替换字符)
    • 3.4. APIs
      • 3.4.1.索引 APIs (Indices APIs)
        • 创建/删除/获取->索引
        • 启动关闭/缩小/滚动->索引
        • 提交/获取/获取字段->映射
        • 索引->别名/是否存在/类型存在
        • 更新索引/获取->设置(未完成)
        • 分析器、索引模板(未完成)
        • Shadow replica indices 卷影副本索引
        • 索引->统计信息/段
        • 索引->恢复/分片存储
        • 清理缓存/刷新/同步刷新
        • 重新加载/强制合并
      • 3.4.2.文档 APIs (Document APIs)
        • 读写文档(Reading and Writing documents)
        • 索引文档 API
        • 获取/批量获取->文档
        • 删除/根据查询API删除
        • 更新/根据查询API更新
        • Bulk API(批量操作)
        • Reindex API(复制索引)
        • Term Vectors(词条向量)/Multi termvectors API
        • ?refresh
      • 3.4.3.搜索 APIs (Search APIs)
        • Search / URI Search
        • Request Body Search(未完成)
          • Query / From / Size
          • Sort / Source filtering
          • Fields / Script Fields / Doc value Fields
          • Post filter
          • Highlighting
          • Rescoring / Search Type
          • Scroll
          • Preference / Explain
          • Version / Index Boost
          • min_score / Named Queries
          • Inner hits / Search After
          • Field Collapsing 字段折叠
        • Search 模板/Multi Search 模板
        • Search Shards API
        • Suggesters
          • Term suggester
          • Phrase Suggester
          • Completion Suggester
          • Context Suggester
          • 返回suggester的类型
        • Multi Search API
        • Count API
        • Validate API
        • Explain API
        • Profile API
          • Profiling Queries
          • Profiling Aggregations
          • Profiling Considerations
        • Percolator / Field stats API
        • Field Capabilities API
    • 3.5.Query DSL(DSL方式查询)
      • 3.5.1.查询和过滤上下文
      • 3.5.2.Match All 查询
      • 3.5.3.全文搜索(Full Text Search)
        • 匹配查询(Match Query)
        • 短语匹配查询(Match Phrase Query)
        • 短语前缀匹配查询(Match Phrase Prefix Query)
        • 多字段查询(Multi Match Query)
        • 常用术语查询(Common Terms Query)
        • (Query String Query) 未完成
      • 3.5.4.Term级别查询(Term level queries)
        • Term 查询
        • Terms 查询
        • Range 查询(范围查询)
        • Exists 查询(非空值查询)
        • Prefix 查询(前缀查询)
        • Wildcard 查询(通配符查询)
        • Regexp 查询(正则表达式查询)
        • Fuzzy 查询(模糊查询)
        • Type Query(类型查询)
        • Ids Query(ID 查询)
      • 3.5.5.复合查询(Compound queries)
        • Constant Score 查询
        • Bool 查询
        • Dis Max 查询
        • Function Score 查询
        • Boosting 查询
        • Indices 查询
      • 3.5.6.Joining 查询(连接查询)
        • Nested Query(嵌套查询)
        • Has Child Query
        • Has Parent Query
        • Parent Id Query
      • 3.5.7.地理位置查询 (Geo queries)
        • GeoShape Query(地理形状查询)
        • Geo Bounding Box Query(地理边框查询)
        • Geo Distance Query(地理距离查询)
        • Geo Distance Range Query(地理距离范围查询)
        • Geo Polygon Query(地理多边形查询)
      • 3.5.8.专业查询(Specialized queries)
      • 3.5.9.Span 查询
        • Span Term 查询
        • Span Multi Term 查询
        • Span First 查询
        • Span Near 查询
        • Span Or 查询
        • Span Not 查询
        • Span Containing 查询
        • Span Within 查询
        • Span Field Masking 查询
    • 3.6.Aggregations(聚合分析)
      • 3.6.1.量度聚合(Metric Aggregations)
        • 平均值聚合(Avg Aggregation)
        • 基数聚合(Cardinality Aggregation)
        • 扩展统计聚合( Extended Stats Aggregation)
        • 地理边界聚合(Geo Bounds Aggregation)
        • 地理重心聚合(Geo Centroid Aggregation)
        • 最大值聚合(Max Aggregation)
        • 最小值聚合(Min Aggregation)
        • Percentiles Aggregation
        • Percentile Ranks Aggregation
        • Scripted Metric Aggregation
        • Stats Aggregation
        • 总和聚合(Sum Aggregation)
        • Top hits Aggregation
        • Value Count Aggregation
      • 3.6.2.桶聚合(Bucket Aggregations)
        • 邻接矩阵聚合(Adjacency Matrix Aggregation)
        • Children Aggregation
        • 日期直方图聚合(Date Histogram Aggregation)
        • 日期范围聚合(Date Range Aggregation)
        • 多元化的采样器聚集(Diversified Sampler Aggregation)
        • 过滤器聚合(Filter Aggregation)
        • 多过滤器聚合(Filters Aggregation)
        • 地理距离聚合(Geo Distance Aggregation)
        • GeoHash网格聚合(GeoHash grid Aggregation)
        • 全局聚合(Global Aggregation)
        • 直方图聚合(Histogram Aggregation)
        • IP范围聚合(IP Range Aggregation)
        • 丢失字段聚合(Missing Aggregation)
        • 嵌套聚合(Nested Aggregation)
        • 范围聚合(Range Aggregation)
        • Reverse nested Aggregation
        • 采样聚合(Sampler Aggregation)
        • Significant Terms Aggregation
      • 3.6.3.管道聚合(Pipeline Aggregations)
        • 平均值桶聚合( Avg Bucket Aggregation)
        • 导数聚合(Derivative Aggregation)
        • 最大值桶聚合(Max Bucket Aggregation)
        • 最小值桶聚合(Min Bucket Aggregation)
        • 总和桶聚合(Sum Bucket Aggregation)
        • 统计桶聚合(Stats Bucket Aggregation)
        • 扩展信息桶聚合(Extended Stats Bucket Aggregation)
        • 百分数桶聚合(Percentiles Bucket Aggregation)
        • Moving Average Aggregation
        • 累积汇总聚合(Cumulative Sum Aggregation)
        • 桶脚本聚合(Bucket Script Aggregation)
        • 桶选择器聚合(Bucket Selector Aggregation)
        • 串行差异聚合(Serial Differencing Aggregation)
      • 3.6.4.矩阵聚合(Matrix Aggregations)
        • 矩阵统计(Matrix Stats)
      • 3.6.5.缓存频繁聚合(Caching heavy aggregations)
      • 3.6.6.仅返回需要聚合的结果(Returning only aggregation results)
      • 3.6.7.聚合元数据(Aggregation Metadata)
      • 3.6.8.返回聚合的类型(Returning the type of the aggregation)
    • Glossary of terms (词汇表)
    • 未完成的任务
  • 4.基础补充总结
    • 3.2.Mapping
    • 3.3.分析器与定义自己的分析器(Analyzer)
  • 原理
  • 实战
    • 结构化搜索
    • 聚合分析
    • 数据建模
    • 应用场景
  • PHP API
    • 安装与快速入门
    • 配置
    • 实战
  • 资料
  • 笔记
    • 1.x 升级 2.x
Powered by GitBook
On this page
  • Open / Close Index API /启动关闭索引
  • Shrink Index /缩小索引
  • Shrinking an index 缩小一个索引
  • Monitoring the shrink process /监测缩小进程
  • Wait For Active Shards /等待激活分片
  • Rollover Index/滚动索引
  • Naming the new index/新索引名称
  • Using date math with the rolllover API/使用滚动API的日期计算
  • Defining the new index/定义新索引
  • Dry run/干运行
  • Wait For Active Shards/等待激活分片

Was this helpful?

  1. 3.基础
  2. 3.4. APIs
  3. 3.4.1.索引 APIs (Indices APIs)

启动关闭/缩小/滚动->索引

Open / Close Index API /启动关闭索引

启动关闭索引允许关闭一个索引并在之后重新开启,一个关闭的索引几乎不占用集群资源(除了维持本身的元信息),并且关闭的索引对读写操作锁定。一个关闭的索引可以重新开启,将通过常规的恢复进程开启。

REST 接口为 /{index}/_close 和 /{index}/_open. 例如:

curl -XPOST 'localhost:9200/my_index/_close?pretty'
curl -XPOST 'localhost:9200/my_index/_open?pretty'

同时开启/关闭多个索引也是允许的。如果请求不存在的索引会抛出错误,可以通过修改配置 ignore_unavailable=true 阻止抛出错误。

所有的索引都可以被开启/关闭只用使用 _all 或者采用通配符(例如 * )作为索引名称。

通过 _all 或者通配符识别索引的方式可以通过修改配置 action.destructive_requires_name=true 来禁用,这项配置也可以通过集群API修改更新。

关闭索引需要消费大量的磁盘空间,可能会对现有环境造成问题。关闭索引的API可以通过集群设置API禁用:cluster.indices.close.enable= false(默认为true),关闭。

Shrink Index /缩小索引

缩小索引API可以允许你将存在的索引转变为一个只包含主要分片的新索引。目标索引中请求的主要分片数量必须要为原索引中的因子(即原分片数量是新分片倍数),例如8个分片可以缩小到4、2、1个分片。如果原分片数量为素数则只能缩小到一个分片。在缩小开始时,每个分片的复制都必须在同一节点(node)存在。

缩小工作如下:

  • 首先,以相同配置创建目标索引,但是主分片数量减少。

  • 然后硬链接( hard-linking ) 各部分自原索引到目标索引。(如果系统不支持硬链接,那么索引的所有部分都将复制迁移到新索引,将会花费大量时间)

  • 最终,将会恢复目标索引,因为目标索引刚被重新打开就会被关闭。

curl -XPUT 'localhost:9200/my_source_index/_settings?pretty' -d'
{
  "settings": {
    "index.routing.allocation.require._name": "shrink_node_name",   # 1
    "index.blocks.write": true    #2
  }
}'

避免索引的所有写操作,但是依然可以修改索引的基本信息,例如删除索引。

Shrinking an index 缩小一个索引

缩小索引 my_source_index 到 新的索引 my_target_index ,可以用下列请求:

curl -XPOST 'localhost:9200/my_source_index/_shrink/my_target_index?pretty'

上述请求将会在目标索引信息加入到集群时立即返回——他不会等到缩小操作开始。

重要:

  • 目标索引存在

  • 原索引主分片数量比目标索引多

  • 原索引主分片数量是目标索引倍数

  • 索引中的所有文档在目标索引将会被缩小到一个分片的数量不会超过 2,147,483,519,因为这是一个分片的承受的最大文档数量。

  • 执行缩小进程的节点必须要有足够的空闲磁盘空间满足原索引的分片能够全部复制迁徙到该节点。

curl -XPOST 'localhost:9200/my_source_index/_shrink/my_target_index?pretty' -d'
{
  "settings": {
    "index.number_of_replicas": 1,
    "index.number_of_shards": 1,      # 1
    "index.codec": "best_compression"   # 2
  },
  "aliases": {
    "my_search_indices": {}
  }
}'

}'

注意目标分片数量为原分片数量的因子

best_compression最佳压缩时只会影响新写到索引的数据,例如当 force-merging 分片。

映射(Mappings) 不应该在缩小API中制定,因为所有的 index.analysis.*和 index.similarity.* 设置都会被原索引配置覆盖。

Monitoring the shrink process /监测缩小进程

缩小API会在目标索引被创建时马上返回,这发生在所有分片被分配之前。这点说明这时所有分片都处于 unassigned 状态,如果出于任何原因目标索引分片无法分配到执行缩小的节点上,那么主分片状态会一直停留在 unassigned ,直到分片被分配。

一旦主节点分配成功,会转化状态会 initializing ,缩小进程就开始执行。当缩小操作结束的时候,这些分片会被激活。在这之后,Elasticsearch将试图分配副本,甚至可能决定主分片迁至另一个节点。

Wait For Active Shards /等待激活分片

Rollover Index/滚动索引

当现有索引被认为太大或太旧时,滚动索引API会将别名滚动到新的索引。

API接受单个别名和条件列表。 别名只能指向一个索引。 如果索引满足指定的条件,则创建一个新的索引,并将别名切换到指向新的索引。

curl -XPUT 'localhost:9200/logs-000001 ?pretty' -d'    # 1
{
  "aliases": {
    "logs_write": {}
  }
}'
# Add > 1000 documents to logs-000001
curl -XPOST 'localhost:9200/logs_write/_rollover ?pretty' -d'   # 2
{
  "conditions": {
    "max_age":   "7d",
    "max_docs":  1000
  }
}'

创建索引 logs-0000001 别名为 logs_write.

如果 ogs_write 指向的索引是在7天以前创建的,或者包含1000个以上的文档,则会创建 logs-000002索引,并更新logs_write别名以指向logs-000002.

上述可能会返回如下的响应:

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "old_index": "logs-000001",
  "new_index": "logs-000002",
  "rolled_over": true, # 1
  "dry_run": false,  # 2
  "conditions": {    # 3
    "[max_age: 7d]": false,
    "[max_docs: 1000]": true
  }
}

index 是否被滚动.

Whether the rollover was dry run.

条件的列表.

Naming the new index/新索引名称

如果现有索引的名称以 - 和数字结尾。 logs-000001 - 然后新索引的名称将遵循相同的模式,增加数字(logs-000002)。 无论旧索引名称如何,编号为零填充长度为6。

如果旧名称与此模式不匹配,则必须按照如下所示,指定新索引的名称:

curl -XPOST 'localhost:9200/my_alias/_rollover/my_new_index_name?pretty' -d'
{
  "conditions": {
    "max_age":   "7d",
    "max_docs":  1000
  }
}'

Using date math with the rolllover API/使用滚动API的日期计算

# PUT /<logs-{now/d}-1> with URI encoding:
curl -XPUT 'localhost:9200/%3Clogs-%7Bnow%2Fd%7D-1%3E ?pretty' -d' # 1
{
  "aliases": {
    "logs_write": {}
  }
}'
curl -XPUT 'localhost:9200/logs_write/log/1?pretty' -d'
{
  "message": "a dummy log"
}'
# Wait for a day to pass
curl -XPOST 'localhost:9200/logs_write/_rollover ?pretty' -d' # 2
{
  "conditions": {
    "max_docs":   "1"
  }
}'

创建当日的索引logs-2016.10.31-1

当日索引滚动, 立即生成如. logs-2016.10.31-000002 , 或者logs-2016.11.01-000002 24小时后

# GET /<logs-{now/d}-*>,<logs-{now/d-1d}-*>,<logs-{now/d-2d}-*>/_search
curl -XGET 'localhost:9200/%3Clogs-%7Bnow%2Fd%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-1d%7D-*%3E%2C%3Clogs-%7Bnow%2Fd-2d%7D-*%3E/_search?pretty'

Defining the new index/定义新索引

curl -XPUT 'localhost:9200/logs-000001?pretty' -d'
{
  "aliases": {
    "logs_write": {}
  }
}'
curl -XPOST 'localhost:9200/logs_write/_rollover?pretty' -d'
{
  "conditions" : {
    "max_age": "7d",
    "max_docs": 1000
  },
  "settings": {
    "index.number_of_shards": 2
  }
}'

Dry run/干运行

滚动API支持dry_run模式,可以在不执行实际滚动的情况下检查请求条件:

curl -XPUT 'localhost:9200/logs-000001?pretty' -d'
{
  "aliases": {
    "logs_write": {}
  }
}'
curl -XPOST 'localhost:9200/logs_write/_rollover?dry_run&pretty' -d'
{
  "conditions" : {
    "max_age": "7d",
    "max_docs": 1000
  }
}'

Wait For Active Shards/等待激活分片

因为滚动操作会创建一个新的索引,因此在创建索引时的wait_for_active_shards 设置也适用于滚动操作。

Previous创建/删除/获取->索引Next提交/获取/获取字段->映射

Last updated 5 years ago

Was this helpful?

强制所有分片都复制迁移到名字为shrink_node_name 的节点上,通过 获取更多设置信息。

从原索引迁移将会花费一定时间。进度信息可以在所有分片都迁移完成前通过 或者 使用 wait_for_no_relocating_shards 参数获得。

缩小索引API和创建索引 相似,并且对于目标索引接受 settings 和 aliases 参数

The shrink process can be monitored with the , or the can be used to wait until all primary shards have been allocated by setting the wait_for_statusparameter to yellow.

缩小进度可以被 或者 所监控。通过设置 wait_for_status 为 yellow ,可以等到所有的主分片都已经分配。

因为缩小操作会创建新的索引,所以 设置依然有效。

使用: 根据索引滚动的日期来命名滚动索引是有用的技术,例如logstash-2016.02.03.。 滚动API支持日期,但要求索引名称以一个破折号后跟一个数字,例如 logstash-2016.02.03-1,每次索引滚动时都会增加。 例如

然后可以按照 中的描述来引用这些索引。 例如,要搜索过去三天创建的索引,您可以执行以下操作:

新索引的设置,映射和别名取自任何匹配的。 此外,您可以在请求正文中指定设置,映射和别名,就像API一样。 请求中指定的值覆盖匹配索引模板中设置的任何值。 例如,以下滚动请求将覆盖index.number_of_shardssetting:

_cat recoveryAPI
cluster health API
(create index) API
_cat recovery API
cluster health API
_cat recovery API
cluster health API
wait for active shards
日期计算
日期数学文档
索引模板
create index
Shard Allocation Filtering