Link Search Menu Expand Document Documentation Menu

ID

OpenSearch 中的每个文档都有一个唯一的 _id 字段。此字段已编入索引,允许您使用 GET API 或 ids 查询来检索文档。

如果您不提供 _id 值,OpenSearch 会自动为文档生成一个。

以下示例请求创建了一个名为 test-index1 的索引,并添加了两个具有不同 _id 值的文档

PUT test-index1/_doc/1
{
  "text": "Document with ID 1"
}

PUT test-index1/_doc/2?refresh=true
{
  "text": "Document with ID 2"
}

然后,您可以使用 _id 字段查询文档,如以下示例请求所示

GET test-index1/_search
{
  "query": {
    "terms": {
      "_id": ["1", "2"]
    }
  }
}

响应返回两个文档,其 _id 值分别为 12

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "test-index1",
        "_id": "1",
        "_score": 1,
        "_source": {
          "text": "Document with ID 1"
        }
      },
      {
        "_index": "test-index1",
        "_id": "2",
        "_score": 1,
        "_source": {
          "text": "Document with ID 2"
        }
      }
    ]
  }

_id 字段的限制

虽然 _id 字段可用于各种查询,但它限制用于聚合、排序和脚本。如果您需要根据 _id 字段进行排序或聚合,建议将 _id 内容复制到另一个启用 doc_values 的字段中。有关示例,请参阅 IDs 查询

剩余 350 字符

有问题?

想要贡献?