From 35f0eac449d4388321de56e9d2e18618f0ddd905 Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 20 Dec 2019 15:52:41 +0100 Subject: [PATCH 1/2] Added new examples --- docs/examples/reindex.asciidoc | 75 +++++++++++++++++++++ docs/examples/sql.asciidoc | 64 ++++++++++++++++++ docs/examples/update-by-query.asciidoc | 59 +++++++++++++++++ docs/examples/update.asciidoc | 92 ++++++++++++++++++++++++++ 4 files changed, 290 insertions(+) create mode 100644 docs/examples/reindex.asciidoc create mode 100644 docs/examples/sql.asciidoc create mode 100644 docs/examples/update-by-query.asciidoc create mode 100644 docs/examples/update.asciidoc diff --git a/docs/examples/reindex.asciidoc b/docs/examples/reindex.asciidoc new file mode 100644 index 000000000..f67607c72 --- /dev/null +++ b/docs/examples/reindex.asciidoc @@ -0,0 +1,75 @@ +[[bulk_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) +---- diff --git a/docs/examples/sql.asciidoc b/docs/examples/sql.asciidoc new file mode 100644 index 000000000..8e190abaf --- /dev/null +++ b/docs/examples/sql.asciidoc @@ -0,0 +1,64 @@ +[[bulk_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) +---- diff --git a/docs/examples/update-by-query.asciidoc b/docs/examples/update-by-query.asciidoc new file mode 100644 index 000000000..54a6d8b9c --- /dev/null +++ b/docs/examples/update-by-query.asciidoc @@ -0,0 +1,59 @@ +[[get_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) + +--------- diff --git a/docs/examples/update.asciidoc b/docs/examples/update.asciidoc new file mode 100644 index 000000000..45a63b395 --- /dev/null +++ b/docs/examples/update.asciidoc @@ -0,0 +1,92 @@ +[[get_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) + + +--------- \ No newline at end of file From 6c1ee64bcce61716763d243aa2f690b539899816 Mon Sep 17 00:00:00 2001 From: delvedor Date: Fri, 10 Jan 2020 15:57:12 +0100 Subject: [PATCH 2/2] Fixed examples links --- docs/examples/reindex.asciidoc | 2 +- docs/examples/sql.asciidoc | 2 +- docs/examples/update-by-query.asciidoc | 2 +- docs/examples/update.asciidoc | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/examples/reindex.asciidoc b/docs/examples/reindex.asciidoc index f67607c72..a99984d69 100644 --- a/docs/examples/reindex.asciidoc +++ b/docs/examples/reindex.asciidoc @@ -1,4 +1,4 @@ -[[bulk_examples]] +[[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. diff --git a/docs/examples/sql.asciidoc b/docs/examples/sql.asciidoc index 8e190abaf..cccc641ae 100644 --- a/docs/examples/sql.asciidoc +++ b/docs/examples/sql.asciidoc @@ -1,4 +1,4 @@ -[[bulk_examples]] +[[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. diff --git a/docs/examples/update-by-query.asciidoc b/docs/examples/update-by-query.asciidoc index 54a6d8b9c..7d4a647e7 100644 --- a/docs/examples/update-by-query.asciidoc +++ b/docs/examples/update-by-query.asciidoc @@ -1,4 +1,4 @@ -[[get_examples]] +[[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. diff --git a/docs/examples/update.asciidoc b/docs/examples/update.asciidoc index 45a63b395..ab08f79fb 100644 --- a/docs/examples/update.asciidoc +++ b/docs/examples/update.asciidoc @@ -1,4 +1,4 @@ -[[get_examples]] +[[update_examples]] == Update The update API allows updates of a specific document using the given script. +