_id field

edit

Each document has an _id that uniquely identifies it, which is indexed so that documents can be looked up either with the GET API or the ids query. The _id can either be assigned at indexing time, or a unique _id can be generated by Elasticsearch. This field is not configurable in the mappings.

The value of the _id field is accessible in queries such as term, terms, match, and query_string.

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" ] 
    }
  }
}

Querying on the _id field (also see the ids query)

The _id field is restricted from use in aggregations, sorting, and scripting. In case sorting or aggregating on the _id field is required, it is advised to duplicate the content of the _id field into another field that has doc_values enabled.

_id is limited to 512 bytes in size and larger values will be rejected.