Skip to content

Latest commit

 

History

History
61 lines (50 loc) · 1.31 KB

File metadata and controls

61 lines (50 loc) · 1.31 KB
endpoint update
lang javascript
es_version 9.3
client @elastic/elasticsearch@9.3.0

Elasticsearch 9.3 update endpoint (JavaScript example)

Use client.update() to modify specific fields of an existing document without replacing the entire document.

const response = await client.update({
  index: "products",
  id: "prod-1",
  doc: { price: 799.99, in_stock: false },
});
console.log(`${response.result} document ${response._id}`);

Only the fields in doc are merged into the existing document. All other fields remain unchanged.

Script updates

Use a script for updates that depend on the current document state:

const response = await client.update({
  index: "products",
  id: "prod-1",
  script: {
    source: "ctx._source.price *= params.discount",
    params: { discount: 0.9 },
  },
});

Upserts

Supply upsert to create the document if it does not already exist:

const response = await client.update({
  index: "products",
  id: "prod-3",
  doc: { price: 599.00 },
  upsert: {
    name: "Ergonomic Standing Desk",
    brand: "DeskCraft",
    price: 599.00,
    category: "furniture",
    in_stock: true,
    rating: 4.8,
  },
});

If prod-3 exists, only price is updated. If it does not exist, the full upsert body is indexed instead.