update

edit
client.update([params, [callback]])

Update parts of a document. The required body parameter can contain one of two things:

  • a partial document, which will be merged with the existing one.
  • a script which will update the document content

Check the API Conventions and the elasticsearch docs for more information pertaining to this method.

Update document title using partial document.

const response = await client.update({
  index: 'myindex',
  type: 'mytype',
  id: '1',
  body: {
    // put the partial document under the `doc` key
    doc: {
      title: 'Updated'
    }
  }
})

Add a tag to document tags property using a script.

const response = await client.update({
  index: 'myindex',
  type: 'mytype',
  id: '1',
  body: {
    script: 'ctx._source.tags += tag',
    params: { tag: 'some new tag' }
  }
});

Increment a document counter by 1 or initialize it, when the document does not exist.

const response = await client.update({
  index: 'myindex',
  type: 'mytype',
  id: '777',
  body: {
    script: 'ctx._source.counter += 1',
    upsert: {
      counter: 1
    }
  }
})

Delete a document if it’s tagged “to-delete”.

const response = await client.update({
  index: 'myindex',
  type: 'mytype',
  id: '1',
  body: {
    script: 'ctx._source.tags.contains(tag) ? ctx.op = "delete" : ctx.op = "none"',
    params: {
      tag: 'to-delete'
    }
  }
});

Params

waitForActiveShards

String — Sets the number of shard copies that must be active before proceeding with the update operation. Defaults to 1, meaning the primary shard only. Set to all for all shard copies, otherwise set to any non-negative value less than or equal to the total number of copies for the shard (number of replicas + 1)

fields

String, String[], Boolean — A comma-separated list of fields to return in the response

_source

String, String[], Boolean — True or false to return the _source field or not, or a list of fields to return

_sourceExclude

String, String[], Boolean — A list of fields to exclude from the returned _source field

_sourceInclude

String, String[], Boolean — A list of fields to extract and return from the _source field

lang

String — The script language (default: painless)

parent

String — ID of the parent document. Is is only used for routing and when for the upsert request

refresh

String — If true then refresh the effected shards to make this operation visible to search, if wait_for then wait for a refresh to make this operation visible to search, if false (the default) then do nothing with refreshes.

Options
  • "true"
  • "false"
  • "wait_for"
  • ""

retryOnConflict

Number — Specify how many times should the operation be retried when a conflict occurs (default: 0)

routing

String — Specific routing value

timeout

DurationString — Explicit operation timeout

timestamp

Timestamp — Explicit timestamp for the document

ttl

DurationString — Expiration time for the document

version

Number — Explicit version number for concurrency control

versionType

String — Specific version type

Options
  • "internal"
  • "force"

id

String — Document ID

index

String — The name of the index

type

String — The type of the document

body

Object, JSON — An optional request body, as either JSON or a JSON serializable object. See the elasticsearch docs for details about what can be specified here.

back to top