_id 字段

编辑

每个文档都有一个唯一的 _id 来标识它,该 _id 会被索引,以便可以使用 GET APIids 查询来查找文档。 _id 可以在索引时分配,也可以由 Elasticsearch 生成一个唯一的 _id。此字段在映射中是不可配置的。

termtermsmatchquery_string 等查询中,可以访问 _id 字段的值。

resp = client.index(
    index="my-index-000001",
    id="1",
    document={
        "text": "Document with ID 1"
    },
)
print(resp)

resp1 = client.index(
    index="my-index-000001",
    id="2",
    refresh=True,
    document={
        "text": "Document with ID 2"
    },
)
print(resp1)

resp2 = client.search(
    index="my-index-000001",
    query={
        "terms": {
            "_id": [
                "1",
                "2"
            ]
        }
    },
)
print(resp2)
response = client.index(
  index: 'my-index-000001',
  id: 1,
  body: {
    text: 'Document with ID 1'
  }
)
puts response

response = client.index(
  index: 'my-index-000001',
  id: 2,
  refresh: true,
  body: {
    text: 'Document with ID 2'
  }
)
puts response

response = client.search(
  index: 'my-index-000001',
  body: {
    query: {
      terms: {
        _id: [
          '1',
          '2'
        ]
      }
    }
  }
)
puts response
{
	res, err := es.Index(
		"my-index-000001",
		strings.NewReader(`{
	  "text": "Document with ID 1"
	}`),
		es.Index.WithDocumentID("1"),
		es.Index.WithPretty(),
	)
	fmt.Println(res, err)
}

{
	res, err := es.Index(
		"my-index-000001",
		strings.NewReader(`{
	  "text": "Document with ID 2"
	}`),
		es.Index.WithDocumentID("2"),
		es.Index.WithRefresh("true"),
		es.Index.WithPretty(),
	)
	fmt.Println(res, err)
}

{
	res, err := es.Search(
		es.Search.WithIndex("my-index-000001"),
		es.Search.WithBody(strings.NewReader(`{
	  "query": {
	    "terms": {
	      "_id": [
	        "1",
	        "2"
	      ]
	    }
	  }
	}`)),
		es.Search.WithPretty(),
	)
	fmt.Println(res, err)
}
const response = await client.index({
  index: "my-index-000001",
  id: 1,
  document: {
    text: "Document with ID 1",
  },
});
console.log(response);

const response1 = await client.index({
  index: "my-index-000001",
  id: 2,
  refresh: "true",
  document: {
    text: "Document with ID 2",
  },
});
console.log(response1);

const response2 = await client.search({
  index: "my-index-000001",
  query: {
    terms: {
      _id: ["1", "2"],
    },
  },
});
console.log(response2);
# Example documents
PUT my-index-000001/_doc/1
{
  "text": "Document with ID 1"
}

PUT my-index-000001/_doc/2?refresh=true
{
  "text": "Document with ID 2"
}

GET my-index-000001/_search
{
  "query": {
    "terms": {
      "_id": [ "1", "2" ] 
    }
  }
}

_id 字段上进行查询(另请参阅 ids 查询

_id 字段被限制用于聚合、排序和脚本。如果需要在 _id 字段上进行排序或聚合,建议将 _id 字段的内容复制到另一个启用了 doc_values 的字段中。

_id 的大小限制为 512 字节,更大的值将被拒绝。