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
  • _parent field
  • Parent-child restrictionsedit (父子限制)
  • Global ordinals (全局序列)
  • _routing field
  • Searching with custom routing(使用自定义路由搜索)
  • Making a routing value required(路由值的要求)
  • Unique IDs with custom routing(具有自定义路由的唯一ID)
  • Routing to an index partition(路由到索引分区)

Was this helpful?

  1. 3.基础
  2. 3.2.Mapping
  3. 3.2.2.Meta-Fields(元字段)

_parent,_routing 元字段

_parent

用于创建两个映射类型之间的父子关系。

_routing

将文档路由到特定分片的自定义路由值。

_parent field

通过使一个映射类型成为另一个映射类型的父类,可以在同一索引中的文档之间建立父子关系 :

curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_parent": {},
    "my_child": {
      "_parent": {
        "type": "my_parent" # 1
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index/1/1?pretty' -H 'Content-Type: application/json' -d' # 2
{
  "text": "This is a parent document"
}

'
curl -XPUT 'localhost:9200/my_index/my_child/2?parent=1&pretty' -H 'Content-Type: application/json' -d' # 2
{
  "text": "This is a child document"
}
'
curl -XPUT 'localhost:9200/my_index/my_child/3?parent=1&refresh=true&pretty' -H 'Content-Type: application/json' -d' # 4
{
  "text": "This is another child document"
}
'
curl -XGET 'localhost:9200/my_index/my_parent/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "has_child": { # 5
      "type": "my_child",
      "query": {
        "match": {
          "text": "child document"
        }
      }
    }
  }
}
'

1

该my_parent类型是my_child类型的parent(父节)。

2

索引parent文档。

3 , 4

索引两个子文档,指定父文档的ID。

5

查找具有与查询匹配的子项的所有父文档。

通过使一个映射类型成为另一个映射类型的父类,可以在同一索引中的文档之间建立父子关系 :

curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "parent_id": {
      "type": "my_child",
      "id": "1"
    }
  },
  "aggs": {
    "parents": {
      "terms": {
        "field": "_parent",
        "size": 10
      }
    }
  },
  "script_fields": {
    "parent": {
      "script": {
         "inline": "doc[\u0027_parent\u0027]"
      }
    }
  }
}
'

1

2

3

在脚本中访问_parent字段

Parent-child restrictionsedit (父子限制)

  • 父类和子类型必须不同 - 不能在相同类型的文档之间建立父子关系。

  • 该_parent.type设置只能指向不存在的类型。 这意味着类型在创建后不能成为父类型。

  • 父文档和子文档必须在相同的分片上索引。 父ID用作子节点的路由值,以确保子节点与父节点在同一分片上进行索引。 这意味着在获取,删除或更新子文档时需要提供相同的parent值。

Global ordinals (全局序列)

默认情况下,全局序列是高效的:如果索引已更改,则 _parent 字段的全局序列将作为刷新的一部分进行重新构建。 这会增加刷新时间。 然而,大多数时候,这是正确的选择,否则,当使用第一个 parent-child query(父子查询)或aggregation(聚合)时,全局序列将被重建。 这可能会为您的用户带来较大的延迟,并且通常会更糟,因为在发生许多写入时,可能会在单个刷新间隔内重新创建 _parent 字段的多个全局序列。

当 parent/child不频繁使用时,写入频繁发生,最好禁用预加载 :

curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_parent": {},
    "my_child": {
      "_parent": {
        "type": "my_parent",
        "eager_global_ordinals": false
      }
    }
  }
}
'

可以检查全局序列使用的堆数量,如下所示 :

# Per-index
curl -XGET 'localhost:9200/_stats/fielddata?human&fields=_parent&pretty'
# Per-node per-index
curl -XGET 'localhost:9200/_nodes/stats/indices/fielddata?human&fields=_parent&pretty'

_routing field

使用以下公式将文档路由到索引中的特定分片:

shard_num = hash(_routing) % num_primary_shards

用于 _routing 的默认值是文档的_id或文档的_parent ID(如果存在)。

可以通过为每个文档指定自定义路由值来实现自定义路由模式。 例如:

curl -XPUT 'localhost:9200/my_index/my_type/1?routing=user1&refresh=true&pretty' -H 'Content-Type: application/json' -d' # 1
{
  "title": "This is a document"
}
'
curl -XGET 'localhost:9200/my_index/my_type/1?routing=user1&pretty' # 2

1

此文档使用user1作为其routingvalue(路由值),而不是其ID。

2

该

_routing 字段的值可以在查询中访问 :

curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d' # 1
{
  "query": {
    "terms": {
      "_routing": [ "user1" ]
    }
  }
}
'

1

Searching with custom routing(使用自定义路由搜索)

自定义路由可以减少搜索的影响。 搜索请求不必分散到索引中的所有分片,而是将该请求发送到与特定路由值(或值)匹配的分片 :

curl -XGET 'localhost:9200/my_index/_search?routing=user1,user2&pretty' -H 'Content-Type: application/json' -d' # 1
{
  "query": {
    "match": {
      "title": "document"
    }
  }
}
'

1

该搜索请求仅会在与user1和user2路由值相关联的分片上执行。

Making a routing value required(路由值的要求)

忘记路由值可能导致文档被索引在多个分片上。 作为保护措施,可以将 _routing 字段配置为使所有CRUD操作都需要指定路由值 :

curl -XPUT 'localhost:9200/my_index2?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "_routing": {
        "required": true # 1
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/my_index2/my_type/1?pretty' -H 'Content-Type: application/json' -d' # 2
{
  "text": "No routing value provided"
}
'

1

my_type文档需要路由。

2

此索引请求会引发一个routing_missing_exception。

Unique IDs with custom routing(具有自定义路由的唯一ID)

在索引指定自定义 _routing 的文档时,_id 的唯一性不能保证在索引中的所有分片。

事实上,如果使用不同的 _routing 值进行索引,那么具有相同_id 的文档可能会在不同的分片上出现。

由用户确定ID 在索引中是唯一的。

Routing to an index partition(路由到索引分区)

可以配置索引,使得自定义路由值将转到分片的子集,而不是单个分片。这有助于减轻最终产生不平衡群集的风险,同时还可以减少搜索的影响。

这是通过在索引创建时提供索引级别设置 index.routing_partition_size 来完成的。随着分区大小的增加,数据的分布越均匀,必须以每个请求搜索更多的分片为代价。

当存在此设置时,计算分片的公式为:

shard_num = (hash(_routing) + hash(_id) % routing_partition_size) % num_primary_shards

也就是说,_routing 字段用于计算索引中的一组分片,然后使用 _id 来选择该组内的分片。

要启用此功能,index.routing_partition_size 应具有大于1且小于 index.number_of_shards 的值。

一旦启用,分区索引将具有以下限制 :

  • 具有父子关系的映射无法在其中创建。

  • 索引中的所有映射必须设置

    _routing 字段。

Previous_all, _field_names元字段Next_meta 元字段

Last updated 5 years ago

Was this helpful?

有关更多信息,请参阅 和查询,和。

该_parent 字段的值可以在aggregations(聚合)和scripts(脚本)中访问,并且可以使用 查询进行查询

查询_parent字段的id(参考查询和查询)

在_parent字段上的聚合(参考)

父子使用 来加快连接。 在对分片进行任何更改之后,需要重建全局序列。 分片中存储的父ID值越多,重建 _parent 字段的全局序列所需的时间越长。

在(获取),(删除)或(更新)文档时需要提供相同的路由值。

在_routing字段上查询(也可以参见)

当使用自定义路由时,重要的是在(索引), (获取), (删除)或 (更新)文档时提供路由值。

has_child
has_parent
children aggregation
inner hits
parent_id
全局序列
indexing
getting
deleting
updating
has_parent
has_child
children aggregation
getting
deleting
updating
ids query