Skip to content

Added new examples #1031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions docs/examples/reindex.asciidoc
Original file line number Diff line number Diff line change
@@ -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)
----
64 changes: 64 additions & 0 deletions docs/examples/sql.asciidoc
Original file line number Diff line number Diff line change
@@ -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)
----
59 changes: 59 additions & 0 deletions docs/examples/update-by-query.asciidoc
Original file line number Diff line number Diff line change
@@ -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)

---------
92 changes: 92 additions & 0 deletions docs/examples/update.asciidoc
Original file line number Diff line number Diff line change
@@ -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)


---------