多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# _index field 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-index-field.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/mapping-index-field.html) 译文链接 : [http://www.apache.wiki/display/Elasticsearch/_id+field](http://www.apache.wiki/display/Elasticsearch/_id+field) 贡献者 : [朱彦安](/display/~zhuyanan),[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) 当跨多个索引执行查询时,有时需要添加仅与某些索引相关联的查询子句。该 **_index**字段允许在索引文档上进行索引的匹配。它可以在 **term**,**terms** 查询,**aggregations**(聚合) , **scripts**(脚本) , **sorting**(排序)中使用 :  注意 : 该 **_index** 被表现为虚拟字段 - 它不会作为实际字段添加到 **Lucene** 索引。这意味着您可以使用 **term** 或 **terms** 查询(或任何重写到 **term** 查询的查询,如 **match** , **query_string** 或 **simple_query_string** 查询) **_index** 字段,但不支持 **prefix(前缀)** , **wildcard(通配符)**, **regexp(正则表达式)**或 **fuzzy(模糊查询)**。 ``` # Example documents curl -XPUT 'localhost:9200/index_1/my_type/1?pretty' -H 'Content-Type: application/json' -d' { "text": "Document in index 1" } ' curl -XPUT 'localhost:9200/index_2/my_type/2?refresh=true&pretty' -H 'Content-Type: application/json' -d' { "text": "Document in index 2" } ' curl -XGET 'localhost:9200/index_1,index_2/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "terms": { "_index": ["index_1", "index_2"] # 1 } }, "aggs": { "indices": { "terms": { "field": "_index", # 2 "size": 10 } } }, "sort": [ { "_index": { # 3 "order": "asc" } } ], "script_fields": { "index_name": { "script": { "lang": "painless", "inline": "doc[\u0027_index\u0027]" # 4 } } } } ' ``` | 1 | 查询 **_index** 字段 | | 2 | 聚合 **_index** 字段 | | 3 | 在 **_index** 字段上排序 | | 4 | 在脚本中访问 **_index** 字段 |