Added new examples (#1031)

* Added new examples

* Fixed examples links
This commit is contained in:
Tomas Della Vedova
2020-01-13 10:57:46 +01:00
committed by GitHub
parent fc5156dbff
commit c548055886
4 changed files with 290 additions and 0 deletions

View File

@ -0,0 +1,92 @@
[[update_examples]]
== Update
The update API allows updates of a specific document using the given script. +
In the following example, we will index a document that also tracks how many times a character has said the given quote, and then we will update the `times` field.
[source,js]
---------
'use strict'
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
async function run () {
await client.index({
index: 'game-of-thrones',
id: '1',
body: {
character: 'Ned Stark',
quote: 'Winter is coming.',
times: 0
}
})
await client.update({
index: 'game-of-thrones',
id: '1',
body: {
script: {
lang: 'painless',
source: 'ctx._source.times++'
// you can also use parameters
// source: 'ctx._source.times += params.count',
// params: { count: 1 }
}
}
})
const { body } = await client.get({
index: 'game-of-thrones',
id: '1'
})
console.log(body)
}
run().catch(console.log)
---------
With the update API, you can also run a partial update of a document.
[source,js]
---------
'use strict'
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })
async function run () {
await client.index({
index: 'game-of-thrones',
id: '1',
body: {
character: 'Ned Stark',
quote: 'Winter is coming.',
isAlive: true
}
})
await client.update({
index: 'game-of-thrones',
id: '1',
body: {
doc: {
isAlive: false
}
}
})
const { body } = await client.get({
index: 'game-of-thrones',
id: '1'
})
console.log(body)
}
run().catch(console.log)
---------