76 lines
1.8 KiB
Plaintext
76 lines
1.8 KiB
Plaintext
[[reindex_examples]]
|
|
=== Reindex
|
|
|
|
The `reindex` API extracts the document source from the source index and indexes the documents into the destination index. You can copy all documents to the destination index, reindex a subset of the documents or update the source before to reindex it.
|
|
|
|
In the following example we have a `game-of-thrones` index which contains different quotes of various characters, we want to create a new index only for the house Stark and remove the `house` field from the document source.
|
|
|
|
[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.',
|
|
house: 'stark'
|
|
}
|
|
})
|
|
|
|
await client.index({
|
|
index: 'game-of-thrones',
|
|
body: {
|
|
character: 'Arya Stark',
|
|
quote: 'A girl is Arya Stark of Winterfell. And I\'m going home.',
|
|
house: 'stark'
|
|
}
|
|
})
|
|
|
|
await client.index({
|
|
index: 'game-of-thrones',
|
|
refresh: true,
|
|
body: {
|
|
character: 'Tyrion Lannister',
|
|
quote: 'A Lannister always pays his debts.',
|
|
house: 'lannister'
|
|
}
|
|
})
|
|
|
|
await client.reindex({
|
|
waitForCompletion: true,
|
|
refresh: true,
|
|
body: {
|
|
source: {
|
|
index: 'game-of-thrones',
|
|
query: {
|
|
match: { character: 'stark' }
|
|
}
|
|
},
|
|
dest: {
|
|
index: 'stark-index'
|
|
},
|
|
script: {
|
|
lang: 'painless',
|
|
source: 'ctx._source.remove("house")'
|
|
}
|
|
}
|
|
})
|
|
|
|
const { body } = await client.search({
|
|
index: 'stark-index',
|
|
body: {
|
|
query: { match_all: {} }
|
|
}
|
|
})
|
|
|
|
console.log(body.hits.hits)
|
|
}
|
|
|
|
run().catch(console.log)
|
|
----
|