Skip to content

Commit 7330709

Browse files
committed
Added new examples (#1031)
* Added new examples * Fixed examples links
1 parent 726d182 commit 7330709

File tree

4 files changed

+290
-0
lines changed

4 files changed

+290
-0
lines changed

docs/examples/reindex.asciidoc

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
[[reindex_examples]]
2+
== Reindex
3+
4+
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.
5+
6+
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.
7+
8+
[source,js]
9+
----
10+
'use strict'
11+
12+
const { Client } = require('@elastic/elasticsearch')
13+
const client = new Client({ node: 'http://localhost:9200' })
14+
15+
async function run () {
16+
await client.index({
17+
index: 'game-of-thrones',
18+
body: {
19+
character: 'Ned Stark',
20+
quote: 'Winter is coming.',
21+
house: 'stark'
22+
}
23+
})
24+
25+
await client.index({
26+
index: 'game-of-thrones',
27+
body: {
28+
character: 'Arya Stark',
29+
quote: 'A girl is Arya Stark of Winterfell. And I\'m going home.',
30+
house: 'stark'
31+
}
32+
})
33+
34+
await client.index({
35+
index: 'game-of-thrones',
36+
refresh: true,
37+
body: {
38+
character: 'Tyrion Lannister',
39+
quote: 'A Lannister always pays his debts.',
40+
house: 'lannister'
41+
}
42+
})
43+
44+
await client.reindex({
45+
waitForCompletion: true,
46+
refresh: true,
47+
body: {
48+
source: {
49+
index: 'game-of-thrones',
50+
query: {
51+
match: { character: 'stark' }
52+
}
53+
},
54+
dest: {
55+
index: 'stark-index'
56+
},
57+
script: {
58+
lang: 'painless',
59+
source: 'ctx._source.remove("house")'
60+
}
61+
}
62+
})
63+
64+
const { body } = await client.search({
65+
index: 'stark-index',
66+
body: {
67+
query: { match_all: {} }
68+
}
69+
})
70+
71+
console.log(body.hits.hits)
72+
}
73+
74+
run().catch(console.log)
75+
----

docs/examples/sql.asciidoc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[[sql_examples]]
2+
== SQL
3+
4+
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.
5+
6+
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.
7+
8+
[source,js]
9+
----
10+
'use strict'
11+
12+
const { Client } = require('@elastic/elasticsearch')
13+
const client = new Client({ node: 'http://localhost:9200' })
14+
15+
async function run () {
16+
await client.index({
17+
index: 'game-of-thrones',
18+
body: {
19+
character: 'Ned Stark',
20+
quote: 'Winter is coming.',
21+
house: 'stark'
22+
}
23+
})
24+
25+
await client.index({
26+
index: 'game-of-thrones',
27+
body: {
28+
character: 'Arya Stark',
29+
quote: 'A girl is Arya Stark of Winterfell. And I\'m going home.',
30+
house: 'stark'
31+
}
32+
})
33+
34+
await client.index({
35+
index: 'game-of-thrones',
36+
refresh: true,
37+
body: {
38+
character: 'Tyrion Lannister',
39+
quote: 'A Lannister always pays his debts.',
40+
house: 'lannister'
41+
}
42+
})
43+
44+
const { body } = await client.sql.query({
45+
body: {
46+
query: "SELECT * FROM \"game-of-thrones\" WHERE house='stark'"
47+
}
48+
})
49+
50+
console.log(body)
51+
52+
const data = body.rows.map(row => {
53+
const obj = {}
54+
for (var i = 0; i < row.length; i++) {
55+
obj[body.columns[i].name] = row[i]
56+
}
57+
return obj
58+
})
59+
60+
console.log(data)
61+
}
62+
63+
run().catch(console.log)
64+
----
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
[[update_by_query_examples]]
2+
== Update By Query
3+
4+
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.
5+
6+
[source,js]
7+
---------
8+
'use strict'
9+
10+
const { Client } = require('@elastic/elasticsearch')
11+
const client = new Client({ node: 'http://localhost:9200' })
12+
13+
async function run () {
14+
await client.index({
15+
index: 'game-of-thrones',
16+
body: {
17+
character: 'Ned Stark',
18+
quote: 'Winter is coming.'
19+
}
20+
})
21+
22+
await client.index({
23+
index: 'game-of-thrones',
24+
refresh: true,
25+
body: {
26+
character: 'Arya Stark',
27+
quote: 'A girl is Arya Stark of Winterfell. And I\'m going home.'
28+
}
29+
})
30+
31+
await client.updateByQuery({
32+
index: 'game-of-thrones',
33+
refresh: true,
34+
body: {
35+
script: {
36+
lang: 'painless',
37+
source: 'ctx._source["house"] = "stark"'
38+
},
39+
query: {
40+
match: {
41+
character: 'stark'
42+
}
43+
}
44+
}
45+
})
46+
47+
const { body } = await client.search({
48+
index: 'game-of-thrones',
49+
body: {
50+
query: { match_all: {} }
51+
}
52+
})
53+
54+
console.log(body.hits.hits)
55+
}
56+
57+
run().catch(console.log)
58+
59+
---------

docs/examples/update.asciidoc

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[[update_examples]]
2+
== Update
3+
4+
The update API allows updates of a specific document using the given script. +
5+
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.
6+
7+
[source,js]
8+
---------
9+
'use strict'
10+
11+
const { Client } = require('@elastic/elasticsearch')
12+
const client = new Client({ node: 'http://localhost:9200' })
13+
14+
async function run () {
15+
await client.index({
16+
index: 'game-of-thrones',
17+
id: '1',
18+
body: {
19+
character: 'Ned Stark',
20+
quote: 'Winter is coming.',
21+
times: 0
22+
}
23+
})
24+
25+
await client.update({
26+
index: 'game-of-thrones',
27+
id: '1',
28+
body: {
29+
script: {
30+
lang: 'painless',
31+
source: 'ctx._source.times++'
32+
// you can also use parameters
33+
// source: 'ctx._source.times += params.count',
34+
// params: { count: 1 }
35+
}
36+
}
37+
})
38+
39+
const { body } = await client.get({
40+
index: 'game-of-thrones',
41+
id: '1'
42+
})
43+
44+
console.log(body)
45+
}
46+
47+
run().catch(console.log)
48+
49+
---------
50+
51+
With the update API, you can also run a partial update of a document.
52+
53+
[source,js]
54+
---------
55+
'use strict'
56+
57+
const { Client } = require('@elastic/elasticsearch')
58+
const client = new Client({ node: 'http://localhost:9200' })
59+
60+
async function run () {
61+
await client.index({
62+
index: 'game-of-thrones',
63+
id: '1',
64+
body: {
65+
character: 'Ned Stark',
66+
quote: 'Winter is coming.',
67+
isAlive: true
68+
}
69+
})
70+
71+
await client.update({
72+
index: 'game-of-thrones',
73+
id: '1',
74+
body: {
75+
doc: {
76+
isAlive: false
77+
}
78+
}
79+
})
80+
81+
const { body } = await client.get({
82+
index: 'game-of-thrones',
83+
id: '1'
84+
})
85+
86+
console.log(body)
87+
}
88+
89+
run().catch(console.log)
90+
91+
92+
---------

0 commit comments

Comments
 (0)