Add examples to reference (#1076)

* Updated examples urls

* Added links to examples

* Updated docs generation script to include code examples

* Fixes

* Skip index api

* Fix link

* Fix url generation

* API generation

* Fix new line

* API generation

* Fix leftover

* API generation
This commit is contained in:
Tomas Della Vedova
2020-02-03 17:23:49 +01:00
committed by delvedor
parent 21683e6826
commit cd61e30bb3
5 changed files with 397 additions and 248 deletions

View File

@ -7,6 +7,10 @@ Following you can find some examples on how to use the client.
* Executing a <<bulk_examples,bulk>> request;
* Executing a <<exists_examples,exists>> request;
* Executing a <<get_examples,get>> request;
* Executing a <<sql_query_examples,sql.query>> request;
* Executing a <<update_examples,update>> request;
* Executing a <<update_by_query_examples,update by query>> request;
* Executing a <<reindex_examples,reindex>> request;
* Use of the <<ignore_examples,ignore>> parameter;
* Executing a <<msearch_examples,msearch>> request;
* How do I <<scroll_examples,scroll>>?
@ -26,3 +30,7 @@ include::search.asciidoc[]
include::suggest.asciidoc[]
include::transport.request.asciidoc[]
include::typescript.asciidoc[]
include::sql.query.asciidoc[]
include::update.asciidoc[]
include::update_by_query.asciidoc[]
include::reindex.asciidoc[]

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -4,8 +4,14 @@
'use strict'
const { readdirSync } = require('fs')
const { join } = require('path')
const dedent = require('dedent')
const codeExamples = readdirSync(join(__dirname, '..', '..', 'docs', 'examples'))
.map(file => file.slice(0, -9))
.filter(api => api !== 'index')
function generateDocs (common, spec) {
var doc = dedent`
[[api-reference]]
@ -67,7 +73,7 @@ function commonParameters (spec) {
=== Common parameters
Parameters that are accepted by all API endpoints.
link:{ref}/common-options.html[Reference]
link:{ref}/common-options.html[Documentation]
[cols=2*]
|===\n`
Object.keys(spec.params).forEach(key => {
@ -170,7 +176,10 @@ function generateApiDoc (spec) {
client.${camelify(name)}(${codeParameters.length > 0 ? `{\n ${codeParameters}\n}` : ''})
----\n`
if (documentationUrl) {
doc += `link:${documentationUrl}[Reference]\n`
doc += `link:${documentationUrl}[Documentation] +\n`
}
if (codeExamples.includes(name)) {
doc += `{jsclient}/${name.replace(/\./g, '_')}_examples.html[Code Example] +\n`
}
if (params.length !== 0) {