Skip to content

Latest commit

 

History

History
47 lines (38 loc) · 1.01 KB

File metadata and controls

47 lines (38 loc) · 1.01 KB
endpoint get_source
lang javascript
es_version 9.3
client @elastic/elasticsearch@9.3.0

Elasticsearch 9.3 get_source endpoint (JavaScript example)

Use client.getSource() to retrieve only the document body, without metadata. This is a convenience over get() when you don't need _version, _seq_no, or other metadata fields.

const doc = await client.getSource({ index: "products", id: "prod-1" });
console.log(`${doc.name} — $${doc.price}`);

Selecting fields

Use _source_includes or _source_excludes to return a subset of fields:

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

Handling missing documents

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

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