Search code examples
rubyelasticsearchelasticsearch-ruby

update elasticcache record without totally replacing it


I'm using this to add a bunch of records to the elasticcache index:

  def self.index_trends(trend_hashes)
    formatted_trends = trend_hashes.map do |trend_hash|
      {
        index: {
          _index: 'trends',
          _type: 'trend',
          _id: trend_hash["id"],
          data: trend_hash
        }
      }
    end
    elasticsearch.bulk({
      body: formatted_trends
    })
  end

Now I need to update a record (given its id). I want to just add one key-val to the data hash, not replace the whole state. How can I do this using elasticsearch-ruby?


Solution

  • I dont know how to do this with ruby, but in ES there is update API.

    Which look like this

    POST test/type1/1/_update
    {
        "script" : "ctx._source.new_field = \"value_of_new_field\""
    }
    

    From example with ruby should be

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