_id field

_id field

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.

  1. resp = client.index(
  2. index="my-index-000001",
  3. id="1",
  4. document={
  5. "text": "Document with ID 1"
  6. },
  7. )
  8. print(resp)
  9. resp1 = client.index(
  10. index="my-index-000001",
  11. id="2",
  12. refresh=True,
  13. document={
  14. "text": "Document with ID 2"
  15. },
  16. )
  17. print(resp1)
  18. resp2 = client.search(
  19. index="my-index-000001",
  20. query={
  21. "terms": {
  22. "_id": [
  23. "1",
  24. "2"
  25. ]
  26. }
  27. },
  28. )
  29. print(resp2)
  1. response = client.index(
  2. index: 'my-index-000001',
  3. id: 1,
  4. body: {
  5. text: 'Document with ID 1'
  6. }
  7. )
  8. puts response
  9. response = client.index(
  10. index: 'my-index-000001',
  11. id: 2,
  12. refresh: true,
  13. body: {
  14. text: 'Document with ID 2'
  15. }
  16. )
  17. puts response
  18. response = client.search(
  19. index: 'my-index-000001',
  20. body: {
  21. query: {
  22. terms: {
  23. _id: [
  24. '1',
  25. '2'
  26. ]
  27. }
  28. }
  29. }
  30. )
  31. puts response
  1. {
  2. res, err := es.Index(
  3. "my-index-000001",
  4. strings.NewReader(`{
  5. "text": "Document with ID 1"
  6. }`),
  7. es.Index.WithDocumentID("1"),
  8. es.Index.WithPretty(),
  9. )
  10. fmt.Println(res, err)
  11. }
  12. {
  13. res, err := es.Index(
  14. "my-index-000001",
  15. strings.NewReader(`{
  16. "text": "Document with ID 2"
  17. }`),
  18. es.Index.WithDocumentID("2"),
  19. es.Index.WithRefresh("true"),
  20. es.Index.WithPretty(),
  21. )
  22. fmt.Println(res, err)
  23. }
  24. {
  25. res, err := es.Search(
  26. es.Search.WithIndex("my-index-000001"),
  27. es.Search.WithBody(strings.NewReader(`{
  28. "query": {
  29. "terms": {
  30. "_id": [
  31. "1",
  32. "2"
  33. ]
  34. }
  35. }
  36. }`)),
  37. es.Search.WithPretty(),
  38. )
  39. fmt.Println(res, err)
  40. }
  1. const response = await client.index({
  2. index: "my-index-000001",
  3. id: 1,
  4. document: {
  5. text: "Document with ID 1",
  6. },
  7. });
  8. console.log(response);
  9. const response1 = await client.index({
  10. index: "my-index-000001",
  11. id: 2,
  12. refresh: "true",
  13. document: {
  14. text: "Document with ID 2",
  15. },
  16. });
  17. console.log(response1);
  18. const response2 = await client.search({
  19. index: "my-index-000001",
  20. query: {
  21. terms: {
  22. _id: ["1", "2"],
  23. },
  24. },
  25. });
  26. console.log(response2);
  1. # Example documents
  2. PUT my-index-000001/_doc/1
  3. {
  4. "text": "Document with ID 1"
  5. }
  6. PUT my-index-000001/_doc/2?refresh=true
  7. {
  8. "text": "Document with ID 2"
  9. }
  10. GET my-index-000001/_search
  11. {
  12. "query": {
  13. "terms": {
  14. "_id": [ "1", "2" ]
  15. }
  16. }
  17. }

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.