Skip to content

Latest commit

 

History

History
60 lines (47 loc) · 1.2 KB

File metadata and controls

60 lines (47 loc) · 1.2 KB
endpoint get
lang javascript
es_version 9.3
client @elastic/elasticsearch@9.3.0

Elasticsearch 9.3 get endpoint (JavaScript example)

Use client.get() to retrieve a document by its ID.

const response = await client.get({ index: "products", id: "prod-1" });

const doc = response._source;
console.log(`${doc.name} — $${doc.price}`);

The response includes metadata (_index, _id, _version, _seq_no, _primary_term) alongside the _source document body.

Selecting fields

Use _source_includes or _source_excludes to return only the fields you need:

const response = await client.get({
  index: "products",
  id: "prod-1",
  _source_includes: ["name", "price"],
});

To fetch metadata without the source body, disable it entirely:

const response = await client.get({
  index: "products",
  id: "prod-1",
  _source: false,
});

Handling missing documents

A ResponseError with status 404 is thrown when the document does not exist:

try {
  await client.get({ index: "products", id: "prod-999" });
} catch (err) {
  if (err.statusCode === 404) {
    console.log("Document not found");
  } else {
    throw err;
  }
}