_index,_uid,_type,_id 元字段

Identity meta-fields(身份的元字段)

文档所属的索引。

复合字段包含了_type _id

文档 ID。

_index field

当跨多个索引执行查询时,有时需要添加仅与某些索引相关联的查询子句。该 _index 字段允许在索引文档上进行索引的匹配。它可以在 term,terms 查询,aggregations(聚合) , scripts(脚本) , sorting(排序)中使用 :

注意 :

_index被表现为虚拟字段 - 它不会作为实际字段添加到Lucene索引。这意味着您可以使用termterms 查询(或任何重写到term查询的查询,如matchquery_stringsimple_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字段

_type field

每个索引的文档都与一个 _type 以及一个 _id相关联。 为了使类型名称快速搜索,_type字段被索引。

_type 字段的值可以在查询,聚合,脚本以及排序时访问:

也就是表的概念

# Example documents
curl -XPUT 'localhost:9200/my_index/type_1/1?pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document with type 1"
}
'
curl -XPUT 'localhost:9200/my_index/type_2/2?refresh=true&pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document with type 2"
2
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms": {
      "_type": [ "type_1", "type_2" ] # 1
    }
  },
  "aggs": {
    "types": {
      "terms": {
        "field": "_type", # 2
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_type": { # 3
        "order": "desc"
      }
    }
  ],
  "script_fields": {
    "type": {
      "script": {
        "lang": "painless",
        "inline": "doc['_type']" # 4
      }
    }
  }
}
'

1

_type字段上查询

2

_type字段上聚合

3

_type字段上排序

4

在脚本中访问_type字段

返回结果:

{
  "took": 42,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_index": "my_index",
        "_type": "type_2",
        "_id": "2",
        "_score": null,
        "fields": {
          "type": [
            "type_2"
          ]
        },
        "sort": [
          "type_2"
        ]
      },
      {
        "_index": "my_index",
        "_type": "type_1",
        "_id": "1",
        "_score": null,
        "fields": {
          "type": [
            "type_1"
          ]
        },
        "sort": [
          "type_1"
        ]
      }
    ]
  },
  "aggregations": {
    "types": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "type_1",
          "doc_count": 1
        },
        {
          "key": "type_2",
          "doc_count": 1
        }
      ]
    }
  }
}

_uid field

每个索引的文档都与一个 _type和一个 _id相关联。

这些值组合为 {type}#{id} 并作为 _uid 字段编入索引。

_uid 字段的值可以在queries(查询),aggregations(聚合),scripts(脚本)以及sorting(排序)时访问 :

# Example documents
curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document with ID 1"
}
'
curl -XPUT 'localhost:9200/my_index/my_type/2?refresh=true&pretty' -H 'Content-Type: application/json' -d'
{
  "text": "Document with ID 2"
}
'
curl -XGET 'localhost:9200/my_index/_search?pretty' -H 'Content-Type: application/json' -d'
{
  "query": {
    "terms": {
      "_uid": [ "my_type#1", "my_type#2" ] # 1
    }
  },
  "aggs": {
    "UIDs": {
      "terms": {
        "field": "_uid", # 2
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_uid": { # 3
        "order": "desc"
      }
    }
  ],
  "script_fields": {
    "UID": {
      "script": {
         "lang": "painless",
         "inline": "doc['_uid']" # 4
      }
    }
  }
}
'

1

_uid字段上查询 (也可以参考 ids query)

2

_uid字段上聚合

3

_uid字段上排序

4

在脚本中访问_uid字段

_id field

每个索引的文档都与一个 _type 和一个 _id相关联。

_id 字段不被索引,因为它的值可以从 _uid 字段自动导出。

_id 字段的值可以在某些查询( term , terms , match , query_string , simple_query_string )中访问,但不能在aggregations(聚合),scripts(脚本)或sorting(排序)中使用,而应使用 _uid 字段代替 :

# Example documents
PUT my_index/my_type/1
{
  "text": "Document with ID 1"
}

PUT my_index/my_type/2&refresh=true
{
  "text": "Document with ID 2"
}

GET my_index/_search
{
  "query": {
    "terms": {
      "_id": [ "1", "2" ] # 1
    }
  }
}

1

_id字段上查询(参见ids查询

Last updated