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 delvedor
parent 726d1824bd
commit 733070963b
4 changed files with 290 additions and 0 deletions

View File

@ -0,0 +1,75 @@
[[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)
----

View File

@ -0,0 +1,64 @@
[[sql_examples]]
== SQL
Elasticsearch SQL is an X-Pack component that allows SQL-like queries to be executed in real-time against Elasticsearch. Whether using the REST interface, command-line or JDBC, any client can use SQL to search and aggregate data natively inside Elasticsearch. One can think of Elasticsearch SQL as a translator, one that understands both SQL and Elasticsearch and makes it easy to read and process data in real-time, at scale by leveraging Elasticsearch capabilities.
In the following example we will search all the documents that has the field `house` equals to `stark`, log the result with the tabular view and then manipulate the result to obtain an object easy to navigate.
[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'
}
})
const { body } = await client.sql.query({
body: {
query: "SELECT * FROM \"game-of-thrones\" WHERE house='stark'"
}
})
console.log(body)
const data = body.rows.map(row => {
const obj = {}
for (var i = 0; i < row.length; i++) {
obj[body.columns[i].name] = row[i]
}
return obj
})
console.log(data)
}
run().catch(console.log)
----

View File

@ -0,0 +1,59 @@
[[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)
---------

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)
---------