ES學(xué)習(xí)四 -- Update

Update

Request

POST /<index>/_update/<_id>

Description

Enables you to script document updates. The script can update, delete, or skip modifying the document. The update API also supports passing a partial document, which is merged into the existing document. To fully replace an existing document, use the index API.

This operation

  1. Gets the document (collocated with the shard) from the index.
  2. Runs the specified script.
  3. Indexes the result.

Query parameters

  • if_seq_no
    (Optional, integer) Only perform the operation if the document has this sequence number. See Optimistic concurrency control.
  • if_primary_term
    (Optional, integer) Only perform the operation if the document has this primary term. See Optimistic concurrency control.
  • lang
    (Optional, string) The script language. Default: painless.
  • require_alias
    (Optional, Boolean) If true, the destination must be an index alias. Defaults to false.
  • refresh
    (Optional, enum) If true, Elasticsearch refreshes the affected 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 do nothing with refreshes. Valid values: true, false, wait_for. Default: false.
  • retry_on_conflict
    (Optional, integer) Specify how many times should the operation be retried when a conflict occurs. Default: 0.
  • routing
    (Optional, string) Target the specified primary shard.
  • _source
    (Optional, list) Set to false to disable source retrieval (default: true). You can also specify a comma-separated list of the fields you want to retrieve.
  • _source_excludes
    (Optional, list) Specify the source fields you want to exclude.
  • _source_includes
    (Optional, list) Specify the source fields you want to retrieve.
  • timeout
    (Optional, time units) Period to wait for the following operations:

Dynamic mapping updates
Waiting for active shards

Defaults to 1m (one minute). This guarantees Elasticsearch waits for at least the timeout before failing. The actual wait time could be longer, particularly when multiple waits occur.

  • wait_for_active_shards
    (Optional, string) The number of shard copies that must be active before proceeding with the operation. Set to all or any positive integer up to the total number of shards in the index (number_of_replicas+1). Default: 1, the primary shard.

Examples

First, let’s index a simple doc:

PUT test/_doc/1
{
  "counter" : 1,
  "tags" : ["red"]
}

To increment the counter, you can submit an update request with the following script:

POST test/_update/1
{
  "script" : {
    "source": "ctx._source.counter += params.count",
    "lang": "painless",
    "params" : {
      "count" : 4
    }
  }
}

Similarly, you could use and update script to add a tag to the list of tags (this is just a list, so the tag is added even it exists):

POST test/_update/1
{
  "script": {
    "source": "ctx._source.tags.add(params.tag)",
    "lang": "painless",
    "params": {
      "tag": "blue"
    }
  }
}

You could also remove a tag from the list of tags. The Painless function to remove a tag takes the array index of the element you want to remove. To avoid a possible runtime error, you first need to make sure the tag exists. If the list contains duplicates of the tag, this script just removes one occurrence.

POST test/_update/1
{
  "script": {
    "source": "if (ctx._source.tags.contains(params.tag)) { ctx._source.tags.remove(ctx._source.tags.indexOf(params.tag)) }",
    "lang": "painless",
    "params": {
      "tag": "blue"
    }
  }
}

You can also add and remove fields from a document. For example, this script adds the field new_field:

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

Conversely, this script removes the field new_field:

POST test/_update/1
{
  "script" : "ctx._source.remove('new_field')"
}

Instead of updating the document, you can also change the operation that is executed from within the script. For example, this request deletes the doc if the tags field contains green, otherwise it does nothing (noop):

POST test/_update/1
{
  "script": {
    "source": "if (ctx._source.tags.contains(params.tag)) { ctx.op = 'delete' } else { ctx.op = 'none' }",
    "lang": "painless",
    "params": {
      "tag": "green"
    }
  }
}

update part of a document

The following partial update adds a new field to the existing document:

POST test/_update/1
{
  "doc": {
    "name": "new_name"
  }
}

If both doc and script are specified, then doc is ignored. If you specify a scripted update, include the fields you want to update in the script.

Detect noop updates

By default updates that don’t change anything detect that they don’t change anything and return "result": "noop":

POST test/_update/1
{
  "doc": {
    "name": "new_name"
  }
}

If the value of name is already new_name, the update request is ignored and the result element in the response returns noop:

{
   "_shards": {
        "total": 0,
        "successful": 0,
        "failed": 0
   },
   "_index": "test",
   "_type": "_doc",
   "_id": "1",
   "_version": 7,
   "_primary_term": 1,
   "_seq_no": 6,
   "result": "noop"
}

You can disable this behavior by setting "detect_noop": false:

POST test/_update/1
{
  "doc": {
    "name": "new_name"
  },
  "detect_noop": false
}

Upsert

If the document does not already exist, the contents of the upsert element are inserted as a new document. If the document exists, the script is executed:

POST test/_update/1
{
  "script": {
    "source": "ctx._source.counter += params.count",
    "lang": "painless",
    "params": {
      "count": 4
    }
  },
  "upsert": {
    "counter": 1
  }
}

Scripted upsert

To run the script whether or not the document exists, set scripted_upsert to true:

POST sessions/_update/dh3sgudg8gsrgl
{
  "scripted_upsert": true,
  "script": {
    "id": "my_web_session_summariser",
    "params": {
      "pageViewEvent": {
        "url": "foo.com/bar",
        "response": 404,
        "time": "2014-01-01 12:32"
      }
    }
  },
  "upsert": {}
}

Doc as upsert

Instead of sending a partial doc plus an upsert doc, you can set doc_as_upsert to true to use the contents of doc as the upsert value:

POST test/_update/1
{
  "doc": {
    "name": "new_name"
  },
  "doc_as_upsert": true
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末此衅,一起剝皮案震驚了整個濱河市赡若,隨后出現(xiàn)的幾起案子愈污,更是在濱河造成了極大的恐慌佑钾,老刑警劉巖方面,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡虑粥,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門宪哩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來娩贷,“玉大人,你說我怎么就攤上這事锁孟”蜃妫” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵品抽,是天一觀的道長储笑。 經(jīng)常有香客問我,道長圆恤,這世上最難降的妖魔是什么突倍? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮盆昙,結(jié)果婚禮上羽历,老公的妹妹穿的比我還像新娘。我一直安慰自己弱左,他們只是感情好窄陡,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著拆火,像睡著了一般跳夭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上们镜,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天币叹,我揣著相機(jī)與錄音,去河邊找鬼模狭。 笑死颈抚,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的嚼鹉。 我是一名探鬼主播贩汉,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼锚赤!你這毒婦竟也來了匹舞?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤线脚,失蹤者是張志新(化名)和其女友劉穎赐稽,沒想到半個月后叫榕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡姊舵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年晰绎,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片括丁。...
    茶點(diǎn)故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡荞下,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出躏将,到底是詐尸還是另有隱情锄弱,我是刑警寧澤考蕾,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布祸憋,位于F島的核電站,受9級特大地震影響肖卧,放射性物質(zhì)發(fā)生泄漏蚯窥。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一塞帐、第九天 我趴在偏房一處隱蔽的房頂上張望拦赠。 院中可真熱鬧,春花似錦葵姥、人聲如沸荷鼠。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽允乐。三九已至,卻和暖如春削咆,著一層夾襖步出監(jiān)牢的瞬間牍疏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工拨齐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留鳞陨,地道東北人。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓瞻惋,卻偏偏與公主長得像厦滤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子歼狼,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容