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
  • Indexing meta-fields(索引的元字段)
  • _all field
  • _field_names field

Was this helpful?

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

_all, _field_names元字段

Indexing meta-fields(索引的元字段)

_all

一个索引其他全部字段值的全能字段。

_field_names

文档中包含非空值的所有字段。

_all field

该_all 字段是一个特殊的全能字段,它将所有其他字段的值连接成一个大字符串,使用空格作为分隔符,然后对其进行分词和索引,但不存储。 这意味着它可以被检索,但不能取回。

该_all 字段允许您在文档中搜索,并不需要知道哪个字段包含该值。 这使得它成为开始使用新数据集时的有用选项。 例如 :

curl -XPUT 'localhost:9200/my_index/user/1?pretty' -H 'Content-Type: application/json' -d' # 1
{
  "first_name":    "John",
  "last_name":     "Smith",
  "date_of_birth": "1970-10-24"
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "_all": "john smith 1970"
    }
  }
}
'

1

该 _all 字段包含项: [ "john", "smith", "1970", "10", "24" ]

注意 :

所有值被视为字符串

上述示例中的date_of_birth字段被视为date字段,因此将索引表示1970-10-24 00:00:00 UTC的单个项。 然而,_all字段将所有值视为字符串,因此日期值被索引成三个字符串项 : “1970”,“24”,“10”。 注意,_all字段将每个字段的原始值作为字符串组合。 它不会组合来自每个字段的项。

该_all字段是一个文本字段,并接受与其他字符串字段相同的参数,包括analyzer,term_vectors,index_options 和store。

该_all字段是有用的,特别是当使用简单的过滤来探索新的数据时。 但是,通过将字段值连接成一个大字符串,_all字段将丢失短字段(更相关)和长字段(较不相关)之间的区别。 对于搜索相关性很重要的用例,最好专门查询各个字段。

该_all字段并不轻量 : 它需要额外的CPU 并使用更多的磁盘空间。 如果不需要,它可以完全禁用或个别字段定制。

在查询中使用 _all 字段

query_string和simple_query_string查询默认查询_all字段,除非指定了另一个字段 :

 curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "query_string": {
      "query": "john smith 1970"
    }
  }
}
'
GET _search?q=john+smith+1970

禁用 _all 字段

通过将enabled 设置为false,可以对_all 字段完全禁用:

 curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "type_1": { # 1
      "properties": {...}
    },
    "type_2": { # 2
      "_all": {
        "enabled": false
      },
      "properties": {...}
    }
  }
}
'

1

type_1 中的 _all 字段已启用。

2

type_2 中的 _all 字段已完全禁用.

如果_all字段被禁用,则URI 搜索请求和query_string 和simple_query_string 查询将无法将其用于查询(请参阅在查询中使用_all 字段)。 您可以将它们配置为使用与 index.query.default_field 设置不同的字段 :

curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "my_type": {
      "_all": {
        "enabled": false # 1
      },
      "properties": {
        "content": {
          "type": "text"
        }
      }
    }
  },
  "settings": {
    "index.query.default_field": "content" # 2
  }
}
'

1

_all字段对于my_type类型是禁用的。

2

query_string查询将默认查询此索引中的content字段。

排除 _all 的字段

索引提升和 _all 字段

curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "properties": {
        "title": { # 1
          "type": "text",
          "boost": 2
        },
        "content": { # 2
          "type": "text"
        }
      }
    }
  }
}
'

1 , 2

当查询_all字段时,源于title字段的字符是源于content字段的字符的两倍相关。

警告 :

使用_all字段的在索引时boost对查询性能有显着的影响。 通常更好的解决方案是单独查询字段,可在查询时boost。

自定义 _all 字段

虽然每个索引只有一个_all字段,但copy_to 参数允许创建多个自定义_all 字段。 例如,first_name 和last_name 字段可以组合到full_name 字段中 :

curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "properties": {
        "first_name": {
          "type":    "text",
          "copy_to": "full_name" # 1
        },
        "last_name": {
          "type":    "text",
          "copy_to": "full_name" # 2
        },
        "full_name": {
          "type":    "text"
        }
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/myindex/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "full_name": "John Smith"
    }
  }
}
'

1 , 2

first_name和last_name值将复制到full_name字段。

高亮显示和 _all 字段

存储 _all 字段

如果存储设置为true,则可以检索原始字段值,并且可以高亮显示 :

curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "_all": {
        "store": true
      }
    }
  }
}
'
curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "_all": "John Smith"
    }
  },
  "highlight": {
    "fields": {
      "_all": {}
    }
  }
}
'

当然,存储_all 字段将使用更多的磁盘空间,因为由于它是其他字段的组合,所以可能会导致奇怪的高亮显示结果。

_all字段也接受term_vector 和index_options 参数,允许使用快速的矢量荧光笔和贴片荧光笔。

高亮显示原始字段

您可以查询_all字段,但使用原始字段进行高亮显示,如下所示 :

curl -XPUT 'localhost:9200/myindex?pretty' -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "mytype": {
      "_all": {}
    }
  }
}
'
curl -XPUT 'localhost:9200/myindex/mytype/1?pretty' -H 'Content-Type: application/json' -d'
{
  "first_name": "John",
  "last_name": "Smith"
}
'
curl -XGET 'localhost:9200/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "_all": "John Smith" # 1
    }
  },
  "highlight": {
    "fields": {
      "*_name": { # 2
        "require_field_match": false # 3
      }
    }
  }
}
'

1

查询检查_all字段以查找匹配的文档。

2

在_source可以使用的两个名称字段上执行高亮显示。

3

该查询未针对name字段运行,因此将require_field_match设置为false。

_field_names field

该_field_names 字段的值可以在查询中被访问:

# Example documents
PUT my_index/my_type/1
{
  "title": "This is a document"
}

PUT my_index/my_type/2
{
  "title": "This is another document",
  "body": "This document has a body"
}
PUT my_index/my_type/3
{
"title": ""
}

PUT my_index/my_type/4?refresh=true
{
"title": null
}

GET my_index/_search
{
  "query": {
    "terms": {
      "_field_names": [ "title" ] # 1
    }
  }
}

1

通过以下返回结果,简单理解就是指定的字段,值不为null的所有文档.

返回结果:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.6931472,
    "hits": [
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "2",
        "_score": 0.6931472,
        "_source": {
          "title": "This is another document",
          "body": "This document has a body"
        }
      },
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "This is a document"
        }
      },
      {
        "_index": "my_index",
        "_type": "my_type",
        "_id": "3",
        "_score": 0.2876821,
        "_source": {
          "title": ""
        }
      }
    ]
  }
}
Previous_source,_size 元字段Next_parent,_routing 元字段

Last updated 5 years ago

Was this helpful?

同样的在中 ?q= parameter(内部改写为query_string):

其他查询(如 和 查询)要求您按照 显式指定_all 字段。

可以使用 设置从_all字段中包含或排除各个字段。

使用 参数,索引时可以提升个别字段。_all字段考虑到这些提升 :

如果原始字符串值可用,则字段只能用于 显示,从 字段或作为存储字段。

_all字段不在_source 字段中,默认情况下不存储,因此无法高亮显示。 有两个选项。存储字段或高亮显示 。

该_field_names 字段索引包含除 null 之外的任何值的文档中每个字段的名称。 该字段通过 查询以查找特定字段具有或不具有任何非空值的文档。

在_field_names字段上查询(参考查询)

URI搜索请求
match
term
第一个示例
include_in_all
boost
高亮
_source
_all
原始字段
exists
exists