Elasticsearch Reference
  • Introduction
  • 入门
    • 基本概念
    • 安装
    • 探索集群
      • 集群健康
      • 获取所有索引库
      • 创建一个索引库
      • 索引文档创建与查询
      • 删除索引
    • 修改数据
      • 更新文档
      • 删除文档
      • 批量处理
    • 探索您的数据
      • 搜索API
      • 查询语言介绍
      • 执行搜索
      • 执行过滤
      • 执行聚合
    • 总结
  • Elasticsearch设置
    • 安装Elasticsearch
      • .zip或.tar.gz文件的安装方式
      • Debian软件包安装方式
      • RPM安装方式
      • Windows安装方式
      • Docker安装方式
    • 配置Elasticsearch
    • 重要的Elasticsearch配置
    • 安全配置
    • 启动前检查
      • 堆大小检查
      • 文件描述符检查
      • 内存锁定检查
      • 最大线程数检查
      • 最大虚拟内存检查
      • 最大map数检查
      • JVM检查
      • 串行收集使用检查
      • 系统调用过滤检查
      • OnError与OnOutOfMemroyError检查
      • G1GC检查
    • 重要的系统配置
      • 系统设置
      • 在jvm.options中设置JVM堆大小
      • 禁用swapping
      • 文件描述符
      • 虚拟内存
      • 线程数
    • Elasticsearch升级
      • 滚动升级
      • 全集群重启升级
      • 索引重建升级
    • Elasticsearch停机
  • 重大变更
    • 5.3重大变更
    • 5.2重大变更
      • 删除Shadow副本
    • 5.1重大变更
    • 5.0重大变更
      • 搜索与查询DSL变化
      • Mapping变化
      • Percolator变化
      • Suggester变化
      • Index API变化
      • Document API变化
      • Settings变化
      • Allocation变化
      • HTTP变化
      • REST API变化
      • CAT API变化
      • JAVA API变化
      • 安装包
      • 插件变化
      • 文件系统相关变化
      • 磁盘数据文件路径
      • Aggregation变化
      • 脚本相关变化
  • API约定
    • 多索引语法
    • 索引库名称的日期运算
    • 常用选项
    • URL-based访问控制
  • 文档API
    • 读写文档
    • Index API
    • Get API
    • Delete API
    • Delete By Query API
    • Update API
    • Update By Query API
    • Multi Get API
    • Bulk API
    • Reindex API
    • Term Vectors
    • Multi termvectors API
    • refresh
  • 搜索API
    • 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
      • Field Collapsing
      • Search After
    • Search Template
    • Multi Search Template
    • Search Shards API
    • Suggesters
      • Term suggester
      • Phrase Suggester
      • Completion Suggester
      • Context Suggester
    • Multi Search API
    • Count API
    • Validate API
    • Explain API
    • Profile API
      • Profiling Queries
      • Profiling Aggregations
      • Profiling Considerations
    • Percolator
    • Field stats API
  • 聚合
    • Metrics 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
    • Bucket Aggregations
      • Adjacency Matrix Aggregation
      • Children Aggregation
      • Date Histogram Aggregation
      • Date Range Aggregation
      • Diversified Sampler Aggregation
      • Filter Aggregation
      • Filters Aggregation
      • Geo Distance Aggregation
      • GeoHash grid Aggregation
      • Global Aggregation
      • Histogram Aggregation
      • IP Range Aggregation
      • Missing Aggregation
      • Nested Aggregation
      • Range Aggregation
      • Reverse nested Aggregation
      • Sampler Aggregation
      • Significant Terms Aggregation
      • Terms Aggregation
    • 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
    • Matrix Aggregations
      • Matrix Stats
    • Caching heavy aggregations
    • Returning only aggregation results
    • Aggregation Metadata
  • 索引库API
    • Create Index
    • Delete Index
    • Get Index
    • Indices Exists
    • Open / Close Index API
    • Shrink Index
    • Rollover Index
    • Put Mapping
    • Get Mapping
    • Get Field Mapping
    • Types Exists
    • Index Aliases
    • Update Indices Settings
    • Get Settings
    • Analyze
      • Explain Analyze
    • Index Templates
    • Shadow replica indices
      • Node level settings related to shadow replicas
    • Indices Stats
    • Indices Segments
    • Indices Recovery
    • Indices Shard Stores
    • Clear Cache
    • Flush
      • Synced Flush
    • Refresh
    • Force Merge
  • cat API
    • cat aliases
    • cat allocation
    • cat count
    • cat fielddata
    • cat health
    • cat indices
    • cat master
    • cat nodeattrs
    • cat nodes
    • cat pending tasks
    • cat plugins
    • cat recovery
    • cat repositories
    • cat thread pool
    • cat shards
    • cat segments
    • cat snapshots
    • cat templates
  • 集群API
    • Cluster Health
    • Cluster State
    • Cluster Stats
    • Pending cluster tasks
    • Cluster Reroute
    • Cluster Update Settings
    • Nodes Stats
    • Nodes Info
    • Task Management API
    • Nodes hot_threads
    • Cluster Allocation Explain API
  • DSL查询
    • Query and filter context
    • Match All Query
    • Full text queries
      • Match Query
      • Match Phrase Query
      • Match Phrase Prefix Query
      • Multi Match Query
      • Common Terms Query
      • Query String Query
      • Simple Query String Query
    • Term level queries
      • Term Query
      • Terms Query
      • Range Query
      • Exists Query
      • Prefix Query
      • Wildcard Query
      • Regexp Query
      • Fuzzy Query
      • Type Query
      • Ids Query
    • Compound queries
      • Constant Score Query
      • Bool Query
      • Dis Max Query
      • Function Score Query
      • Boosting Query
      • Indices Query
    • Joining queries
      • Nested Query
      • Has Child Query
      • Has Parent Query
      • Parent Id Query
    • Geo queries
      • GeoShape Query
      • Geo Bounding Box Query
      • Geo Distance Query
      • Geo Distance Range Query
      • Geo Polygon Query
    • Specialized queries
      • More Like This Query
      • Template Query
      • Script Query
      • Percolate Query
    • Span queries
      • Span Term Query
      • Span Multi Term Query
      • Span First Query
      • Span Near Query
      • Span Or Query
      • Span Not Query
      • Span Containing Query
      • Span Within Query
      • Span Field Masking Query
    • Minimum Should Match
    • Multi Term Query Rewrite
  • Mapping
    • Field datatypes
      • Array datatype
      • Binary datatype
      • Range datatypes
      • Boolean datatype
      • Date datatype
      • Geo-point datatype
      • Geo-Shape datatype
      • IP datatype
      • Keyword datatype
      • Nested datatype
      • Numeric datatypes
      • Object datatype
      • String datatype
      • Text datatype
      • Token count datatype
      • Percolator type
    • Meta-Fields
      • _all field
      • _field_names field
      • _id field
      • _index field
      • _meta field
      • _parent field
      • _routing field
      • _source field
      • _type field
      • _uid field
    • Mapping parameters
      • analyzer
      • normalizer
      • boost
      • coerce
      • copy_to
      • doc_values
      • dynamic
      • enabled
      • fielddata
      • format
      • ignore_above
      • ignore_malformed
      • include_in_all
      • index
      • index_options
      • fields
      • norms
      • null_value
      • position_increment_gap
      • properties
      • search_analyzer
      • similarity
      • store
      • term_vector
    • Dynamic Mapping
      • default mapping
      • Dynamic field mapping
      • Dynamic templates
      • Override default template
  • Analysis
    • Anatomy of an analyzer
    • Testing analyzers
    • Analyzers
      • Configuring built-in analyzers
      • Standard Analyzer
      • Simple Analyzer
      • Whitespace Analyzer
      • Stop Analyzer
      • Keyword Analyzer
      • Pattern Analyzer
      • Language Analyzers
      • Fingerprint Analyzer
      • Custom Analyzer
      • Normalizers
    • Tokenizers
      • Standard Tokenizer
      • Letter Tokenizer
      • Lowercase Tokenizer
      • Whitespace Tokenizer
      • UAX URL Email Tokenizer
      • Classic Tokenizer
      • Thai Tokenizer
      • NGram Tokenizer
      • Edge NGram Tokenizer
      • Keyword Tokenizer
      • Pattern Tokenizer
      • Path Hierarchy Tokenizer
    • Token Filters
      • Standard Token Filter
      • ASCII Folding Token Filter
      • Flatten Graph Token Filter
      • Length Token Filter
      • Lowercase Token Filter
      • Uppercase Token Filter
      • NGram Token Filter
      • Edge NGram Token Filter
      • Porter Stem Token Filter
      • Shingle Token Filter
      • Stop Token Filter
      • Word Delimiter Token Filter
      • Stemmer Token Filter
      • Stemmer Override Token Filter
      • Keyword Marker Token Filter
      • Keyword Repeat Token Filter
      • KStem Token Filter
      • Snowball Token Filter
      • Phonetic Token Filter
      • Synonym Token Filter
      • Synonym Graph Token Filter
      • Compound Word Token Filter
      • Reverse Token Filter
      • Elision Token Filter
      • Truncate Token Filter
      • Unique Token Filter
      • Pattern Capture Token Filter
      • Pattern Replace Token Filter
      • Trim Token Filter
      • Limit Token Count Token Filter
      • Hunspell Token Filter
      • Common Grams Token Filter
      • Normalization Token Filter
      • CJK Width Token Filter
      • CJK Bigram Token Filter
      • Delimited Payload Token Filter
      • Keep Words Token Filter
      • Keep Types Token Filter
      • Classic Token Filter
      • Apostrophe Token Filter
      • Decimal Digit Token Filter
      • Fingerprint Token Filter
      • Minhash Token Filter
    • Character Filters
      • HTML Strip Char Filter
      • Mapping Char Filter
      • Pattern Replace Char Filter
  • 模块
    • 集群
      • 集群的分片分配
      • 基于磁盘的分片分配
      • 了解分片分配
      • 分片分配过滤
      • 其他群集设置
    • Discovery
      • Azure Classic Discovery
      • EC2 Discovery
      • Google Compute Engine Discovery
      • Zen Discovery
    • 本地网关
    • HTTP
    • 索引库
      • 熔断
      • Fielddata
      • 节点查询缓存
      • 索引缓冲
      • 分片请求缓存
      • 索引库恢复
    • 网络设置
    • 节点
    • 插件
    • 脚本
      • 如何使用脚本
      • 文档中字段的访问以及一些特殊变量
      • 脚本与安全
      • Groovy脚本语言
      • PainLess脚本语言
      • PainLess语法
      • PainLess调试
      • Lucene表达式语言
      • 原生(java)脚本
      • 高级文本评分脚本
    • 快照与还原
    • 线程池
    • 传输
    • Tribe节点
    • 跨集群搜索
  • Index模块
    • Analysis
    • Index Shard Allocation
      • Shard Allocation Filtering
      • Delaying allocation when a node leaves
      • Index recovery prioritization
      • Total Shards Per Node
    • Mapper
    • Merge
    • Similarity module
    • Slow Log
    • Store
      • Pre-loading data into the file system cache
    • Translog
  • Ingest节点
    • Pipeline Definition
    • Ingest APIs
      • Put Pipeline API
      • Get Pipeline API
      • Delete Pipeline API
      • Simulate Pipeline API
    • Accessing Data in Pipelines
    • Handling Failures in Pipelines
    • Processors
      • Append Processor
      • Convert Processor
      • Date Processor
      • Date Index Name Processor
      • Fail Processor
      • Foreach Processor
      • Grok Processor
      • Gsub Processor
      • Join Processor
      • JSON Processor
      • KV Processor
      • Lowercase Processor
      • Remove Processor
      • Rename Processor
      • Script Processor
      • Set Processor
      • Split Processor
      • Sort Processor
      • Trim Processor
      • Uppercase Processor
      • Dot Expander Processor
  • How To
    • General recommendations
    • Recipes
    • Tune for indexing speed
    • Tune for search speed
    • Tune for disk usage
  • Testing
    • Java Testing Framework
      • why randomized testing?
      • Using the elasticsearch test classes
      • unit tests
      • integration tests
      • Randomized testing
      • Assertions
  • 术语表
  • 发布说明
    • 5.3.0 Release Notes
    • 5.2.2 Release Notes
    • 5.2.1 Release Notes
    • 5.2.0 Release Notes
    • 5.1.2 Release Notes
    • 5.1.1 Release Notes
    • 5.1.0 Release Notes
    • 5.0.2 Release Notes
    • 5.0.1 Release Notes
    • 5.0.0 Combined Release Notes
    • 5.0.0 GA Release Notes
    • 5.0.0-rc1 Release Notes
    • 5.0.0-beta1 Release Notes
    • 5.0.0-alpha5 Release Notes
    • 5.0.0-alpha4 Release Notes
    • 5.0.0-alpha3 Release Notes
    • 5.0.0-alpha2 Release Notes
    • 5.0.0-alpha1 Release Notes
    • 5.0.0-alpha1 Release Notes (Changes previously released in 2.x)
  • Painless API参考文档
Powered by GitBook
On this page
  • 自动创建索引库
  • 版本控制
  • 版本控制类型
  • 操作类型
  • 自动生成ID
  • 路由
  • 父文档&子文档
  • 分布式
  • 等待活动分片
  • 冲刷
  • Noop Updates
  • 超时

Was this helpful?

  1. 文档API

Index API

Index API通过往指定的索引库输入一个JSON文档来添加或更新索引,让文档变得可搜索。以下示例将JSON文档插入到叫 “twitter” 的索引库中,ID 为1:

PUT twitter/tweet/1
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

上面的索引操作结果如下:

{
    "_shards" : {
        "total" : 2,
        "failed" : 0,
        "successful" : 2
    },
    "_index" : "twitter",
    "_type" : "tweet",
    "_id" : "1",
    "_version" : 1,
    "created" : true,
    "result" : created
}

_shards 头提供了有关索引操作的复制过程的信息。

  • total —— 标识有多少个分片拷贝被执行创建索引操作(包括主分片与副本分片)。

  • successful —— 标识有多少个分片执行创建索引操作成功。

  • failed —— 在存在失败的情况下会返回一个操作失败的分片数组列表。

在successful至少为1的情况下创建索引文档操作才会成功。

注意

自动创建索引库

可以通过在所有节点的配置文件中将action.auto_create_index设置为false来禁用自动创建索引库。可以通过将index.mapper.dynamic设置为false作为索引设置来禁用自动映射创建。

自动创建所以库可以基于一定规则的黑白名单列表,例如,设置action.auto_create_index 为 +aaa*,-bbb*,+ccc*,-* ( +表示允许,-表示不允许)。

版本控制

PUT twitter/tweet/1?version=2
{
    "message" : "elasticsearch now has versioning support, double cool!"
}

注意:版本控制是实时的,不受近实时查询操作的影响。如果没提供版本信息,这个操作的执行将不受到任何版本的检查。

默认情况下,内部版本号从1开始,并且根据每一次的更新、删除操作来递增。可选的是,版本号支持使用外部值(譬如在数据库维护)。要开启此功能,version_type需要被设置为external。提供的值必须是数字、长整型类型,且大于等于0小于9.2e+18。在使用外部版本控制时,系统检查请求中传递文档的版本号是否大于当前文档的版本号,如果小于等于当前存储的文档版本号会有版本冲突,则会在创建索引文档时返回失败。

警告

一个好的作用是,只要使用源数据库生成的版本号,在执行异步创建文档索引时不需要维护严格顺序。一个简单的场景是,使用数据库数据更新elasticsearch的索引,因为只有最新版本的数据将被使用,不管index操作是否出于什么原因执行乱序了。

版本控制类型

上面介绍过了internal与external,Elasticsearch在特殊的场景中还支持其它的类型。这是不同版本控制类型的概述.

internal

  只能够在指定的版本与存储文档的版本完全一致时创建文档索引。

external或external_gt

  如果给定版本严格高于存储的文档的版本或如果没有现有文档,则仅索引文档。给定版本将用作新版本,并与新文档一起存储。提供的版本必须是非负长数字。

external_gte

  如果给定版本高于或等于存储的文档的版本或如果没有现有文档,则仅索引文档。给定版本将用作新版本,并与新文档一起存储。提供的版本必须是非负长数字。

注意:external_gte版本类型适用于特殊场景,应谨慎使用。如果使用不正确,可能会导致数据丢失。还有一个选项,force已被被弃用,因为它可能导致主分片和副本分片出现不一致。

操作类型

index操作还可以接受一个op_type参数来强制执行create操作,允许put-if-absent行为。在使用create创建文档索引时,如果文档已存在则会失败。

这是使用op_type参数的示例:

PUT twitter/tweet/1?op_type=create
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

还有种通过uri指定create的方式

PUT twitter/tweet/1/_create
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

自动生成ID

可以在不指定 ID 的情况下执行索引操作。在这种情况下,将自动生成id。此外,op_type将自动设置为create。这里是一个例子(注意使用的是POST,而不是PUT):

POST twitter/tweet/
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

操作将返回:

{
    "_shards" : {
        "total" : 2,
        "failed" : 0,
        "successful" : 2
    },
    "_index" : "twitter",
    "_type" : "tweet",
    "_id" : "6a8ca01c-7896-48e9-81cc-9f70661fcb32",
    "_version" : 1,
    "created" : true,
    "result": "created"
}

路由

默认情况下,分片的位置——或路由(routing)——通过使用文档的id值的哈希值来控制。对于更明确的控制,基于每个操作直接使用路由参数指定反馈给路由使用的散列函数的值。例如:

POST twitter/tweet?routing=kimchy
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}

在上面的示例中,"tweet" 文档根据提供的路由参数发送到分片:"kimchy" 。

设置显式映射时,可以选择从文档本身提取路由字段值_routing字段来指示索引操作。这在文档解析花费额外的(非常小)成本。如果定义_routing映射并将其设置为必需,则如果未提供或提取不到路由值,则索引操作将失败。

父文档&子文档

通过在索引时指定其父级,可以索引子文档。例如:

PUT blogs
{
  "mappings": {
    "tag_parent": {},
    "blog_tag": {
      "_parent": {
        "type": "tag_parent"
      }
    }
  }
}

PUT blogs/blog_tag/1122?parent=1111
{
    "tag" : "something"
}

对子文档建立索引时,路由值会自动设置为与其父文档相同,除非使用路由参数显式指定路由值。

分布式

index操作会根据路由信息(参见上面的路由章节)定向到主分片,并在主分片所在的实际节点上执行。在主分片操作完成后,如果有需要,会分布更新到可用的副本上。

等待活动分片

为了提高写入系统的弹性,index操作可以配置为在继续操作之前等待一定数量的活动分片副本。如果所需的活动分片副本数不可用,则写操作必须等待并重试,直到必需的分片副本已启动或发生超时为止。默认情况下,写操作只等待主分片处于活动状态,然后再继续(wait_for_active_shards=1)。可以通过设置 index.write.wait_for_active_shards 来动态地在索引设置中覆盖此默认值。要更改每个操作的此行为,可以使用 wait_for_active_shards 请求参数。

有效值是不大于索引库的所有分片总数的正整数(即number_of_replicas+1)。指定负值或者大于分片总数的数字将抛出错误。

例如,假设我们有一个三个节点A、B和C的集群,我们创建一个索引,索引副本数设置为 3 (会导致有4个分片,主分片加3个副本分片)。如果我们尝试索引操作,默认情况下,操作将仅确保每个主分片在继续之前可用。这意味着,在A托管主分片时,即使B和C停机,索引操作仍然将继续,只会有一份数据保存。如果集群有3个活动节点启动,这时每个节点都有一个分片的复制,这时对请求设置 wait_for_active_shards 为 3 (并且所有3个节点都已启动),则索引操作将在继续之前需要 3 个活动分片,这是可以执行的。 但如果我们将 wait_for_active_shards 设置为 all (或者 4, 这是相同的),索引操作将不会继续,因为索引中的没有所有的4个分片处于活动状态。该操作将超时,除非在集群中启动新节点以托管分片的第四个副本。

重要的是要注意,这个设置极大地减少了写操作不写入所需数量的分片副本的机会,但是它不能完全消除可能性,因为这种检查在写操作开始之前发生。一旦写操作正在进行,复制仍然可能在任意数量的分片副本上失败,但在主分片上仍然成功。写操作响应的 _shard 部分显示复制成功/失败的分片副本的数量。

{
    "_shards" : {
        "total" : 2,
        "failed" : 0,
        "successful" : 2
    }
}

冲刷

Noop Updates

当使用index API更新文档时,即使文档没有更改,也始终创建新版本的文档。如果这不可接受,请使用将 detect_noop 设置为 true 的_update API 。此选项在index API上不可用,因为index api不提取旧源,并且无法将其与新源进行比较。

没有一个强制和快速的规则,当等待更新是不能接受的。它是许多因素的组合,例如数据源发送实际上是 noops 的更新的频率,以及每秒多少查询,elasticsearch 在接收更新时在分片上运行。

超时

执行索引操作时分配的主分片可能不可用。这种情况的一些原因可能是主分片当前正在从网关恢复或正在进行重分配。默认情况下,索引操作将在主分片上等待最多1分钟,然后失败并响应错误。 timeout参数可以用于显式指定等待时间。以下是将其设置为5分钟的示例:

PUT twitter/tweet/1?timeout=5m
{
    "user" : "kimchy",
    "post_date" : "2009-11-15T14:12:12",
    "message" : "trying out Elasticsearch"
}
Previous读写文档NextGet API

Last updated 4 years ago

Was this helpful?

当创建索引操作成功时,副本的分片有可能还没开始做(默认情况下,只有主分片才是必须的,但是这个行为是可以)。在这个场景中,total将等于基于number_of_replicas设置的所有分片数,successful将会等于已成功的分片数(主分片+副本分片)。如果没有失败的分片,failed将会为0。

如果创建索引文档时索引库不存在,这会自动创建索引库(参见手动API),并且会根据动态类型mapping来创建索引库的type(参见手动API)。

mapping本身是非常灵活与格式自由的。新的字段或者是对象将会被自动添加到指定类型的mapping定义中。查看部分以获取有关映射定义的更多信息。

每一个被索引的文档都会给他一个版本号。分配的version编号会作为创建索引文档返回结果的一部分。当版本号指定的时候,index API允许。这将故意控制文档的版本。一个好的版本控制用户案例是执行事务读然后修改。在指定version开始读取文档时,同时确保文档不发生变化(读操作在修改操作之后时,推荐设置perference为_primary)。例如:

外部版本控制支持0是一个有效的版本号。它允许文档的版本在与外部版本控制系统同步时从0开始,而不是1。它的副作用是在版本号为0的文档不能使用更新以及不能使用删除。

用来控制本次的修改能够被搜索可见。参见:。

创建索引库
添加映射
映射
乐观并发控制
Update-By-Query API
Delete-By-Query API
refresh
修改