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
  • match_mapping_type(匹配映射类型)
  • match and unmatch(匹配和不匹配)
  • match_pattern(模式匹配)
  • path_match and path_unmatch(路径匹配和不匹配路径)
  • {name} and {dynamic_type}
  • Template examples(模板示例)

Was this helpful?

  1. 3.基础
  2. 3.2.Mapping
  3. 3.2.4.Dynamic Mapping(动态映射)

Dynamic templates(动态模板)

动态模板允许你定义可以用于动态添加的字段的自定义映射:

  • 由Elasticsearch中的match_mapping_type检测到数据类型。

  • 字段的名称可以是match(匹配)和unmatch(不匹配)或match_pattern(模式匹配)。

  • 全点路径的字段可以是path_match(路径匹配)和path_unmatch(不匹配路径)。

原始字段名称{name}和检测导的数据类型{dynamic_type}模板变量可以在映射规范中用作占位符。

Tips

仅当字段包含具体值(不为空或空数组)时才添加动态字段映射。这意味着如果在dynamic_template中使用null_value选项,只能在该字段具有的具体值已被索引第一个文档之后才会应用该值。

动态模板是指定为命名对象的一个数组:

"dynamic_templates": [
    {
      "my_template_name": {          # 1
        ...  match conditions ...    # 2
        "mapping": { ... }           # 3
      }
    },
    ...
  ]

1

模板名称可以是任意字符串。

2

匹配条件可以包括以下任何一种:match_mapping_type(匹配映射类型),match(匹配),match_pattern(模式匹配),unmatch(不匹配),path_match(路径匹配),path_unmatch(取消路径匹配)。

3

匹配字段应该使用的映射。

模板按顺序进行处理-第一个匹配模板达到要求。可以使用PUT mapping API将新的模板附加到列表的尾部。如果新的模板与现有的模板同名,它将会替换旧的版本。

match_mapping_type(匹配映射类型)

match_mapping_type通过动态字段映射检测数据类型的匹配,换句话说,就是Elasticsearch认为该字段应该具有的数据类型。只能自动检测以下数据类型:boolean(布尔类型),date(日期),double(浮点型),long(长整型),object(对象类型),string(字符类型)。同时,它也接受*来匹配所有数据类型。

例如,如果我们要将所有整数字段映射为integer(整型)而不是long(长整型),并且所有string(字符串类型)字段都是text(文本)和keyword(关键词),我们可以使用以下模板:

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "integers": {
            "match_mapping_type": "long",
            "mapping": {
              "type": "integer"
            }
          }
        },
        {
          "strings": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "text",
              "fields": {
                "raw": {
                  "type":  "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      ]
    }
  }}


PUT my_index/my_type/1{
  "my_integer": 5,                 #1
  "my_string": "Some string"       #2
}

1

my_integer字段被映射为integer(整数类型)。

2

my_string字段被映射为text(文本类型),并且是keword(关键字)多字段。

match and unmatch(匹配和不匹配)

match(匹配)参数使用模式匹配字段名称,而unmatch(不匹配)使用模式排除match(匹配)的字段。

以下示例匹配名称为long_开头(除了以_text结尾的字段字符串除外)的所有string(字符串类型)字段,并将其映射为long(长整型)字段:

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "longs_as_strings": {
            "match_mapping_type": "string",
            "match":   "long_*",
            "unmatch": "*_text",
            "mapping": {
              "type": "long"
            }
          }
        }
      ]
    }
  }}

PUT my_index/my_type/1{
  "long_num": "5",              #1
  "long_text": "foo"           #2
}

1

long_num字段被映射为long(长整型)。

2

long_text字段使用默认的string(字符串类型)映射。

match_pattern(模式匹配)

match_pattern参数调整match参数的行为,使其在字段名称上支持匹配完整的Java正则表达式,而不是简单的通配符,例如:

"match_pattern": "regex",
"match": "^profit_\d+$"

path_match and path_unmatch(路径匹配和不匹配路径)

path_match和path_unmatch参数的工作方式与match和unmathc相同,但是字段运行在完整的路径上,而不仅仅是最终的名称,例如:some_object.*.some_field。

此示例将name对象中的任何字段的值复制到顶级full_name字段,但middle字段除外:

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "full_name": {
            "path_match":   "name.*",
            "path_unmatch": "*.middle",
            "mapping": {
              "type":       "text",
              "copy_to":    "full_name"
            }
          }
        }
      ]
    }
  }
}

PUT my_index/my_type/1
{
  "name": {
    "first":  "Alice",
    "middle": "Mary",
    "last":   "White"
  }
}

{name} and {dynamic_type}

在映射中{name}和{dynamic_type}占位符会被替换为字段名称和检测到的动态类型。以下示例将所有的字符串类型设置为使用与该字段名称相同的analyzer(分析器),并禁用所有非字符串字段的doc_values:

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "named_analyzers": {
            "match_mapping_type": "string",
            "match": "*",
            "mapping": {
              "type": "text",
              "analyzer": "{name}"
            }
          }
        },
        {
          "no_doc_values": {
            "match_mapping_type":"*",
            "mapping": {
              "type": "{dynamic_type}",
              "doc_values": false
            }
          }
        }
      ]
    }
  }
}

PUT my_index/my_type/1
{
  "english": "Some English text",  # 1
  "count":   5                     # 2
}

1

english字段通过english分析器映射为string(字符串)字段。

2

count字段通过禁用的doc_values映射为long(长整型)字段。

Template examples(模板示例)

以下是一些可能有用的动态模板示例:

Structured search(结构化搜索)

默认情况下,elasticsearch将字符串字段映射为具有子keyword(关键字)字段的text(文本)字段。但是,如果你只想索引结构化内容,对全文检索不感兴趣,你可以使用elasticsearch将你的字段映射为“关键字”。请注意,这意味着为了搜索这些字段,你必须搜索与索引完全相同的值。

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "strings_as_keywords": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ]
    }
  }
}

text-only mappings for strings(字符串的纯文字映射)

与上一个例子相反,如果你唯一关心的字符串字段是全文检索,并且如果你不打算在该字符串字段上运行聚合,排序或精确搜索,你可以告诉elasticsearch将它仅映射为文本字段(这是5.0之前的默认行为):

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "strings_as_text": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "text"
            }
          }
        }
      ]
    }
  }}

Disabled norms(禁用规范)

Norms(规范)是索引时评分的因子。如果你不关心评分,不按分数排序文档,则可能会出现这种情况,你可以在索引中禁用这些评分因子的存储,这样能够节省一些空间。

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "strings_as_keywords": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "text",
              "norms": false,
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      ]
    }
  }
}

子keyword字段出现在此模板中,用以同动态映射的默认规则保持一致。当然,如果你不需要它们,你可以按上一节所述删除它,因为你不需要在此字段上执行精确搜索或聚合。

Time-series(时间序列)

当使用elasticsearch进行时间序列分析,通常你会经常聚合许多数字字段,但不会过滤。在这种情况下,你可以禁用这些字段上的索引来节省磁盘空间,同时可能获得一些索引速度的提升:

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "unindexed_longs": {
            "match_mapping_type": "long",
            "mapping": {
              "type": "long",
              "index": false
            }
          }
        },
        {
          "unindexed_doubles": {
            "match_mapping_type": "double",
            "mapping": {
              "type": "float",     # 1
              "index": false
            }
          }
        }
      ]
    }
  }
}

1

像默认的动态映射规则一样,doubles(双精度)映射为floats(浮点),通常情况下是足够准确的,但只需要一半的磁盘空间。

PreviousDynamic field mapping(动态字段映射)NextOverride default template(覆盖默认模板)

Last updated 5 years ago

Was this helpful?