Files
elasticsearch-js/docs/examples/update_by_query.asciidoc
Tomas Della Vedova cd61e30bb3 Add examples to reference (#1076)
* Updated examples urls

* Added links to examples

* Updated docs generation script to include code examples

* Fixes

* Skip index api

* Fix link

* Fix url generation

* API generation

* Fix new line

* API generation

* Fix leftover

* API generation
2020-02-04 10:35:59 +01:00

60 lines
1.2 KiB
Plaintext

[[update_by_query_examples]]
== Update By Query
The simplest usage of _update_by_query just performs an update on every document in the index without changing the source. This is useful to pick up a new property or some other online mapping change.
[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',
body: {
character: 'Ned Stark',
quote: 'Winter is coming.'
}
})
await client.index({
index: 'game-of-thrones',
refresh: true,
body: {
character: 'Arya Stark',
quote: 'A girl is Arya Stark of Winterfell. And I\'m going home.'
}
})
await client.updateByQuery({
index: 'game-of-thrones',
refresh: true,
body: {
script: {
lang: 'painless',
source: 'ctx._source["house"] = "stark"'
},
query: {
match: {
character: 'stark'
}
}
}
})
const { body } = await client.search({
index: 'game-of-thrones',
body: {
query: { match_all: {} }
}
})
console.log(body.hits.hits)
}
run().catch(console.log)
---------