加载中

_index 字段

在跨多个索引执行查询时,有时希望添加仅与某些索引的文档相关联的查询子句。_index 字段允许匹配文档所在的索引。它的值可以在某些查询和聚合中访问,也可以在排序或脚本中访问。

				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": {
      "_index": ["index_1", "index_2"]
    }
  },
  "aggs": {
    "indices": {
      "terms": {
        "field": "_index",
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_index": {
        "order": "asc"
      }
    }
  ],
  "script_fields": {
    "index_name": {
      "script": {
        "lang": "painless",
        "source": "doc['_index']"
      }
    }
  }
}
		
  1. _index 字段上进行查询
  2. _index 字段上进行聚合
  3. _index 字段上进行排序
  4. 在脚本中访问 _index 字段

_index 字段是以虚拟方式暴露的——它并没有作为真实字段添加到 Lucene 索引中。这意味着您可以在 termterms 查询(或任何重写为 term 查询的查询,例如 matchquery_stringsimple_query_string 查询),以及 prefixwildcard 查询中使用 _index 字段。但是,它不支持 regexpfuzzy 查询。

_index 字段的查询除了接受具体索引名称外,还接受索引别名。

注意

当指定远程索引名称(例如 cluster_1:index_3)时,查询必须包含分隔符 :。例如,在 cluster_*:index_3 上进行的 wildcard 查询将匹配来自远程索引的文档。但是,在 cluster*index_1 上的查询仅匹配本地索引,因为不存在分隔符。此行为与远程索引名称的常用解析规则一致。

© . This website operates independently and is not affiliated with or endorsed by Elasticsearch B.V. All brand names, logos, and trademarks are the property of their respective owners.