每个文档都有与之关联的元数据,例如 _index 和 _id 元数据字段。 创建映射时,可以自定义其中一些元数据字段的行为。比如我们创建如下的一个文档:

PUT test
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "message": {
        "type": "text"
      }
    }
  }
}

PUT test/_doc/1
{
  "id": "1234",
  "message": "This is so lovely"
}

上面的最后一个命令的返回信息如下:

{
  "_index" : "test",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

从上面的响应中,我们可以看出来 _index 字段为 test,而 _id 为 1。这个是上述文档的身份元数据字段:

身份元数据字段

身份元数据字段
_index文档所属的索引。
_id文档的 ID。

文档源元数据字段

文档源元数据字段
_source表示文档正文的原始 JSON。
_size_source 字段的大小(以字节为单位),由 mapper-size 插件提供。

 要获得 _size 元数据,你必须按照上面表格中的链接按照 mapper-size 插件,并重新启动 Elasticsearch。我们做如下的练习:

DELETE test

PUT test
{
  "mappings": {
    "_size": {
      "enabled": true
    },
    "properties": {
      "id": {
        "type": "keyword"
      },
      "message": {
        "type": "text"
      }
    }
  }
}

PUT test/_doc/1
{
  "id": "1234",
  "message": "This is so lovely"
}

在上面,我们在 mappings 里启动 _size。我们写入一个文档后,我们可以做如下的搜索:

GET test/_search?filter_path=**.hits
{
  "query": {
    "range": {
      "_size": {
        "gte": 10
      }
    }
  },
  "fields": [
    "_size"
  ]
}

上面的命令返回的结果为:

{
  "hits" : {
    "hits" : [
      {
        "_index" : "test",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : "1234",
          "message" : "This is so lovely"
        },
        "fields" : {
          "_size" : [
            53
          ]
        }
      }
    ]
  }
}

从上面,我们可以看出来 _size 这个元数据以及 _source 所表达的源文件数据。

文档计数元数据字段

文档计数元数据字段
_doc_count当文档表示预聚合(pre-aggregation)数据时,用于存储文档计数的自定义字段。

桶聚合总是返回一个名为 doc_count 的字段,显示每个桶中聚合和分区的文档数。 doc_count 值的计算非常简单。 对于每个存储桶中收集的每个文档,doc_count 都会增加 1。

虽然这种简单的方法在计算单个文档的聚合时很有效,但它不能准确地表示存储预聚合数据的文档(例如 histogram 或 aggregate_metric_double 字段),因为一个汇总字段可能代表多个文档。

为了在处理预聚合数据时正确计算文档数量,我们引入了一个名为 _doc_count 的元数据字段类型。 _doc_count 必须始终是一个正整数,表示在单个汇总字段中聚合的文档数。

当字段 _doc_count 添加到文档中时,所有存储桶聚合都将尊重其值并将存储桶 doc_count 增加该字段的值。 如果文档不包含任何 _doc_count 字段,则默认隐含 _doc_count = 1。

重要

  • 每个文档的 _doc_count 字段只能存储一个正整数。 不允许嵌套数组。
  • 如果文档不包含 _doc_count 字段,聚合器将递增 1,这是默认行为。

例子

以下创建索引 API 请求创建具有以下字段映射的新索引:

PUT my_index
{
  "mappings" : {
    "properties" : {
      "my_histogram" : {
        "type" : "histogram"
      },
      "my_text" : {
        "type" : "keyword"
      }
    }
  }
}

以下 index API请求存储两个直方图的预聚合数据:histogram_1 和 histogram_2。

PUT my_index/_doc/1
{
  "my_text" : "histogram_1",
  "my_histogram" : {
      "values" : [0.1, 0.2, 0.3, 0.4, 0.5],
      "counts" : [3, 7, 23, 12, 6]
   },
  "_doc_count": 45 
}

PUT my_index/_doc/2
{
  "my_text" : "histogram_2",
  "my_histogram" : {
      "values" : [0.1, 0.25, 0.35, 0.4, 0.45, 0.5],
      "counts" : [8, 17, 8, 7, 6, 2]
   },
  "_doc_count": 62 
}

请注意上面的 _doc_count 的定义。它必须是一个正整数,存储为生成每个直方图而聚合的文档数。

如果我们在 my_index 上运行以下术语聚合

GET my_index/_search?filter_path=aggregations
{
  "size": 0,
  "aggs": {
    "histogram_titles": {
      "terms": {
        "field": "my_text"
      }
    }
  }
}

我们将得到以下响应:

{
  "aggregations" : {
    "histogram_titles" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "histogram_2",
          "doc_count" : 62
        },
        {
          "key" : "histogram_1",
          "doc_count" : 45
        }
      ]
    }
  }
}

索引元数据字段

索引元数据字段
_field_names档中包含非空值的所有字段。
_ignored由于 ignore_malformed 而在索引时被忽略的文档中的所有字段。

_field_names 字段用于索引文档中包含除 null 之外的任何值的每个字段的名称。 exists 查询使用此字段来查找具有或不具有特定字段的任何非空值的文档。

现在 _field_names 字段仅索引禁用了 doc_values 和  norms 的字段的名称。 对于启用了 doc_values 或 norm 的字段,exists 查询仍然可用,但不会使用 _field_names 字段。

我们还是拿先前的例子来做展示:

DELETE test

PUT test
{
  "mappings": {
    "_size": {
      "enabled": true
    },
    "properties": {
      "id": {
        "type": "keyword",
        "doc_values": true
      },
      "message": {
        "type": "text",
        "norms": false
      }
    }
  }
}

PUT test/_doc/1
{
  "id": "1234",
  "message": "This is so lovely"
}

我们对上面的文档进行搜索:

GET test/_search?filter_path=**.hits
{
  "query": {
    "match": {
      "_field_names": "id"
    }
  }
}

由于 id 字段的 doc_values 是启动的,那么如下的查询:

GET test/_search?filter_path=**.hits
{
  "query": {
    "match": {
      "_field_names": "id"
    }
  }
}

返回:

{
  "hits" : {
    "hits" : [ ]
  }
}

我们再查询:

GET test/_search?filter_path=**.hits
{
  "query": {
    "match": {
      "_field_names": "message"
    }
  }
}

返回:

{
  "hits" : {
    "hits" : [
      {
        "_index" : "test",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "id" : "1234",
          "message" : "This is so lovely"
        }
      }
    ]
  }
}

我们可以使用如下的查询:

GET test/_search?filter_path=**.hits
{
  "query": {
    "exists": {
      "field": "message"
    }
  }
}

GET test/_search?filter_path=**.hits
{
  "query": {
    "exists": {
      "field": "id"
    }
  }
}

上面的两个凌乱都将返回:

{
  "hits" : {
    "hits" : [
      {
        "_index" : "test",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : "1234",
          "message" : "This is so lovely"
        }
      }
    ]
  }
}

路由元数据字段

路由元数据字段
_routing将文档路由到特定分片的自定义路由值。

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

routing_factor = num_routing_shards / num_primary_shards
shard_num = (hash(_routing) % num_routing_shards) / routing_factor

num_routing_shards 是 index.number_of_routing_shards 索引设置的值。 num_primary_shards 是index.number_of_shards 索引设置的值。

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

PUT my-index-000001/_doc/1?routing=user1&refresh=true 
{
  "title": "This is a document"
}

GET my-index-000001/_doc/1?routing=user1

在上面,我们使用 user1 作为路由值而不是文档的 _id 值。当我们想更新获取或者删除该文档时,我们需要使用同样的路由值。

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

GET my-index-000001/_search?filter_path=**.hits
{
  "query": {
    "terms": {
      "_routing": [ "user1" ] 
    }
  }
}

上面的命令的结果为:

{
  "hits" : {
    "hits" : [
      {
        "_index" : "my-index-000001",
        "_id" : "1",
        "_score" : 1.0,
        "_routing" : "user1",
        "_source" : {
          "title" : "This is a document"
        }
      }
    ]
  }
}

使用自定义路由搜索

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

GET my-index-000001/_search?routing=user1,user2 
{
  "query": {
    "match": {
      "title": "document"
    }
  }
}

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

使路由值成为必需

使用自定义路由时,在索引、获取、删除或更新文档时提供路由值非常重要。

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

PUT my-index-000002
{
  "mappings": {
    "_routing": {
      "required": true 
    }
  }
}

PUT my-index-000002/_doc/1 
{
  "text": "No routing value provided"
}

在上面索引中,我们定义 _routing 是必须的。在下面的命令中,由于我们在请求中没有指定 _routing 参数,那么它将抛出 routing_missing_exception 错误。

其它元数据字段

其它元数据字段
_meta应用程序特定的元数据。
_tier文档所属索引的当前数据层首选项。

关于 _meta 元数据,请参考我的另外一篇文章 “Elasticsearch:添加 metadata 到 mapping 中”。

_tier 字段

在跨多个索引执行查询时,有时需要针对给定数据层(data_hot、data_warm、data_cold 或 data_frozen)的节点上保存的索引。 _tier 字段允许匹配文档被索引到的索引的 tier_preference 设置。 在某些查询中可以访问首选值:

PUT index_1/_doc/1
{
  "text": "Document in index 1"
}

PUT index_2/_doc/2?refresh=true
{
  "text": "Document in index 2"
}

GET index_1,index_2/_search
{
  "query": {
    "terms": {
      "_tier": ["data_hot", "data_warm"] 
    }
  }
}

在上面的查询中,我们对 data_hot 及 data_warm 数据层中的数据进行统计。

通常,查询将使用术语查询来列出感兴趣的层(tier),但你可以在任何重写为术语查询的查询中使用 _tier 字段,例如 match、query_string、term、terms 或 simple_query_string 查询,以及 前缀和通配符查询。 但是,它不支持正则表达式和模糊查询。

索引的 tier_preference 设置是按优先顺序排列的层名称的逗号分隔列表,即首先列出托管索引的首选层,然后是可能的许多后备选项。 查询匹配只考虑第一个偏好(列表的第一个值)。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐