Method: Elasticsearch::API::Actions#update

Defined in:
lib/elasticsearch/api/actions/update.rb

#update(arguments = {}) ⇒ Object

Update a document without sending the whole document in the request (“partial update”).

Send either a partial document (doc ) which will be deeply merged into an existing document, or a script, which will update the document content, in the :body argument.

The partial update operation allows you to limit the amount of data you send over the wire and reduces the chance of failed updates due to conflict.

Specify the :version and :retry_on_conflict arguments to balance convenience and consistency.

Examples:

Update document title using partial doc-ument


client.update index: 'myindex', type: 'mytype', id: '1',
              body: { doc: { title: 'Updated' } }

Add a tag to document tags property using a script


client.update index: 'myindex', type: 'mytype', id: '1',
              body: { script: 'ctx._source.tags += tag', params: { tag: 'x' } }

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


client.update index: 'myindex', type: 'mytype', id: '666',
              body: { script: 'ctx._source.counter += 1', upsert: { counter: 1 } }

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


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' } }

Parameters:

  • arguments (Hash) (defaults to: {})

    a customizable set of options

Options Hash (arguments):

  • :id (String)

    Document ID (Required)

  • :ignore (Number, List)

    The list of HTTP errors to ignore; only 404 supported at the moment

  • :index (String)

    The name of the index (Required)

  • :type (String)

    The type of the document (Required)

  • :body (Hash)

    The request definition using either script or partial doc (Required)

  • :consistency (String)

    Explicit write consistency setting for the operation (options: one, quorum, all)

  • :fields (List)

    A comma-separated list of fields to return in the response

  • :lang (String)

    The script language (default: mvel)

  • :parent (String)

    ID of the parent document

  • :percolate (String)

    Perform percolation during the operation; use specific registered query name, attribute, or wildcard

  • :refresh (Boolean)

    Refresh the index after performing the operation

  • :replication (String)

    Specific replication type (options: sync, async)

  • :retry_on_conflict (Number)

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

  • :routing (String)

    Specific routing value

  • :script (String)

    The URL-encoded script definition (instead of using request body)

  • :timeout (Time)

    Explicit operation timeout

  • :timestamp (Time)

    Explicit timestamp for the document

  • :ttl (Duration)

    Expiration time for the document

  • :version (Number)

    Explicit version number for concurrency control

  • :version_type (Number)

    Explicit version number for concurrency control

Raises:

  • (ArgumentError)

See Also:

Since:

  • 0.20



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/elasticsearch/api/actions/update.rb', line 64

def update(arguments={})
  raise ArgumentError, "Required argument 'index' missing" unless arguments[:index]
  raise ArgumentError, "Required argument 'type' missing"  unless arguments[:type]
  raise ArgumentError, "Required argument 'id' missing"    unless arguments[:id]

  valid_params = [
    :consistency,
    :fields,
    :lang,
    :parent,
    :percolate,
    :refresh,
    :replication,
    :retry_on_conflict,
    :routing,
    :script,
    :timeout,
    :timestamp,
    :ttl,
    :version,
    :version_type ]

  method = HTTP_POST
  path   = Utils.__pathify Utils.__escape(arguments[:index]),
                           Utils.__escape(arguments[:type]),
                           Utils.__escape(arguments[:id]),
                           '_update'

  params = Utils.__validate_and_extract_params arguments, valid_params
  body   = arguments[:body]

  params[:fields] = Utils.__listify(params[:fields]) if params[:fields]

  if Array(arguments[:ignore]).include?(404)
    Utils.__rescue_from_not_found { perform_request(method, path, params, body).body }
  else
    perform_request(method, path, params, body).body
  end
end