Added doc examples (#1011)

* Added generate-docs-examples script

* Added doc_examples folder wth the initial examples
This commit is contained in:
Tomas Della Vedova
2019-11-29 14:40:05 +01:00
committed by GitHub
parent 177aae20e6
commit 1e1252383d
57 changed files with 1099 additions and 0 deletions

View File

@ -0,0 +1,17 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.create({
index: 'twitter',
id: '1',
body: {
user: 'kimchy',
post_date: '2009-11-15T14:12:12',
message: 'trying out Elasticsearch'
}
})
console.log(response)
----

View File

@ -0,0 +1,19 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.index({
index: 'twitter',
id: '2',
routing: 'user1',
body: {
counter: 1,
tags: [
'white'
]
}
})
console.log(response)
----

View File

@ -0,0 +1,18 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'twitter',
body: {
query: {
term: {
user: 'kimchy'
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,13 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.get({
index: 'twitter',
id: '0',
_source: 'false'
})
console.log(response)
----

View File

@ -0,0 +1,13 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.get({
index: 'twitter',
id: '2',
routing: 'user1'
})
console.log(response)
----

View File

@ -0,0 +1,17 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.index({
index: 'twitter',
id: '1',
version: '2',
version_type: 'external',
body: {
message: 'elasticsearch now has versioning support, double cool!'
}
})
console.log(response)
----

View File

@ -0,0 +1,18 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'bank',
body: {
query: {
match_phrase: {
address: 'mill lane'
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,12 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.existsSource({
index: 'twitter',
id: '1'
})
console.log(response)
----

View File

@ -0,0 +1,28 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'bank',
body: {
query: {
bool: {
must: {
match_all: {}
},
filter: {
range: {
balance: {
gte: 20000,
lte: 30000
}
}
}
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,20 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
fields: [
'city.*'
],
query: 'this AND that OR thus'
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,15 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.index({
index: 'customer',
id: '1',
body: {
name: 'John Doe'
}
})
console.log(response)
----

View File

@ -0,0 +1,16 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.index({
index: 'twitter',
body: {
user: 'kimchy',
post_date: '2009-11-15T14:12:12',
message: 'trying out Elasticsearch'
}
})
console.log(response)
----

View File

@ -0,0 +1,9 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.info()
console.log(response)
----

View File

@ -0,0 +1,12 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.get({
index: 'customer',
id: '1'
})
console.log(response)
----

View File

@ -0,0 +1,13 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.delete({
index: 'twitter',
id: '1',
routing: 'kimchy'
})
console.log(response)
----

View File

@ -0,0 +1,31 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'bank',
body: {
query: {
bool: {
must: [
{
match: {
age: '40'
}
}
],
must_not: [
{
match: {
state: 'ID'
}
}
]
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,23 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'bank',
body: {
query: {
match_all: {}
},
sort: [
{
account_number: 'asc'
}
],
from: 10,
size: 10
}
})
console.log(response)
----

View File

@ -0,0 +1,21 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'bank',
body: {
query: {
match_all: {}
},
sort: [
{
account_number: 'asc'
}
]
}
})
console.log(response)
----

View File

@ -0,0 +1,21 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
fields: [
'content',
'name.*^5'
],
query: 'this AND that OR thus'
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,18 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.index({
index: 'twitter',
id: '1',
body: {
counter: 1,
tags: [
'red'
]
}
})
console.log(response)
----

View File

@ -0,0 +1,11 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.indices.getMapping({
index: 'my-index'
})
console.log(response)
----

View File

@ -0,0 +1,21 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
fields: [
'title'
],
query: 'this that thus',
minimum_should_match: 2
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,17 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.index({
index: 'twitter',
routing: 'kimchy',
body: {
user: 'kimchy',
post_date: '2009-11-15T14:12:12',
message: 'trying out Elasticsearch'
}
})
console.log(response)
----

View File

@ -0,0 +1,31 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'bank',
body: {
size: 0,
aggs: {
group_by_state: {
terms: {
field: 'state.keyword',
order: {
average_balance: 'desc'
}
},
aggs: {
average_balance: {
avg: {
field: 'balance'
}
}
}
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,14 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.get({
index: 'twitter',
id: '2',
routing: 'user1',
stored_fields: 'tags,counter'
})
console.log(response)
----

View File

@ -0,0 +1,23 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
fields: [
'title',
'content'
],
query: 'this OR that OR thus',
type: 'cross_fields',
minimum_should_match: 2
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,13 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.get({
index: 'twitter',
id: '1',
stored_fields: 'tags,counter'
})
console.log(response)
----

View File

@ -0,0 +1,19 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.indices.putMapping({
index: 'my-index',
body: {
properties: {
'employee-id': {
type: 'keyword',
index: false
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,13 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.get({
index: 'twitter',
id: '0',
_source: '*.id,retweeted'
})
console.log(response)
----

View File

@ -0,0 +1,33 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response0 = await client.cluster.putSettings({
body: {
persistent: {
'action.auto_create_index': 'twitter,index10,-index1*,+ind*'
}
}
})
console.log(response0)
const response1 = await client.cluster.putSettings({
body: {
persistent: {
'action.auto_create_index': 'false'
}
}
})
console.log(response1)
const response2 = await client.cluster.putSettings({
body: {
persistent: {
'action.auto_create_index': 'true'
}
}
})
console.log(response2)
----

View File

@ -0,0 +1,12 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.getSource({
index: 'twitter',
id: '1'
})
console.log(response)
----

View File

@ -0,0 +1,14 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.get({
index: 'twitter',
id: '0',
_source_includes: '*.id',
_source_excludes: 'entities'
})
console.log(response)
----

View File

@ -0,0 +1,25 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.indices.create({
index: 'twitter',
body: {
mappings: {
properties: {
counter: {
type: 'integer',
store: false
},
tags: {
type: 'keyword',
store: true
}
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,12 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.exists({
index: 'twitter',
id: '0'
})
console.log(response)
----

View File

@ -0,0 +1,12 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.indices.getFieldMapping({
index: 'my-index',
fields: 'employee-id'
})
console.log(response)
----

View File

@ -0,0 +1,22 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
fields: [
'content',
'name^5'
],
query: 'this AND that OR thus',
tie_breaker: 0
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,18 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
query: '(new york city) OR (big apple)',
default_field: 'content'
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,18 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.index({
index: 'twitter',
id: '1',
timeout: '5m',
body: {
user: 'kimchy',
post_date: '2009-11-15T14:12:12',
message: 'trying out Elasticsearch'
}
})
console.log(response)
----

View File

@ -0,0 +1,17 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.index({
index: 'twitter',
id: '1',
body: {
user: 'kimchy',
post_date: '2009-11-15T14:12:12',
message: 'trying out Elasticsearch'
}
})
console.log(response)
----

View File

@ -0,0 +1,22 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
fields: [
'title',
'content'
],
query: 'this that thus',
minimum_should_match: 2
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,13 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
q: 'message:number',
size: '0',
terminate_after: '1'
})
console.log(response)
----

View File

@ -0,0 +1,12 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.delete({
index: 'twitter',
id: '1'
})
console.log(response)
----

View File

@ -0,0 +1,18 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'bank',
body: {
query: {
match: {
address: 'mill lane'
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,28 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'bank',
body: {
size: 0,
aggs: {
group_by_state: {
terms: {
field: 'state.keyword'
},
aggs: {
average_balance: {
avg: {
field: 'balance'
}
}
}
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,14 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.getSource({
index: 'twitter',
id: '1',
_source_includes: '*.id',
_source_excludes: 'entities'
})
console.log(response)
----

View File

@ -0,0 +1,18 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.index({
index: 'twitter',
id: '1',
op_type: 'create',
body: {
user: 'kimchy',
post_date: '2009-11-15T14:12:12',
message: 'trying out Elasticsearch'
}
})
console.log(response)
----

View File

@ -0,0 +1,26 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.indices.create({
index: 'my-index',
body: {
mappings: {
properties: {
age: {
type: 'integer'
},
email: {
type: 'keyword'
},
name: {
type: 'text'
}
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,13 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.delete({
index: 'twitter',
id: '1',
timeout: '5m'
})
console.log(response)
----

View File

@ -0,0 +1,17 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
query: 'city.\\*:(this AND that OR thus)'
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,17 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
query: '(content:this OR name:this) AND (content:that OR name:that)'
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,21 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
fields: [
'content',
'name'
],
query: 'this AND that'
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,19 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
default_field: 'title',
query: 'ny city',
auto_generate_synonyms_phrase_query: false
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,11 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.cat.health({
v: ''
})
console.log(response)
----

View File

@ -0,0 +1,12 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.get({
index: 'twitter',
id: '0'
})
console.log(response)
----

View File

@ -0,0 +1,22 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
body: {
query: {
query_string: {
fields: [
'title',
'content'
],
query: 'this OR that OR thus',
minimum_should_match: 2
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,21 @@
// This file is autogenerated, DO NOT EDIT
// Use `node scripts/generate-docs-examples.js` to generate the docs examples
[source, js]
----
const response = await client.search({
index: 'bank',
body: {
size: 0,
aggs: {
group_by_state: {
terms: {
field: 'state.keyword'
}
}
}
}
})
console.log(response)
----

View File

@ -0,0 +1,96 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
'use strict'
/**
* To run this generator you must have the
* `alternatives_report.spec.json` placed in the root of this project.
* To get the `alternatives_report.spec.json` you must run the script
* to parse the original `alternatives_report.json`, which is not yet public
* and lives in github.com/elastic/clients-team/tree/master/scripts/docs-json-generator
*
* This script will remove the content of the `docs/doc_examples` folder and generate
* all the files present in the `enabledFiles` list below.
* You can run it with the following command:
*
* ```bash
* $ node scripts/generate-docs-examples.js
* ```
*/
const { join } = require('path')
const { writeFileSync } = require('fs')
const rimraf = require('rimraf')
const standard = require('standard')
const dedent = require('dedent')
const docsExamplesDir = join('docs', 'doc_examples')
const enabledFiles = [
'docs/delete.asciidoc',
'docs/get.asciidoc',
'docs/index_.asciidoc',
'getting-started.asciidoc',
'query-dsl/query-string-query.asciidoc',
'query-dsl.asciidoc',
'search/request-body.asciidoc',
'setup/install/check-running.asciidoc',
'mapping.asciidoc'
]
function generate () {
rimraf.sync(join(docsExamplesDir, '*'))
const examples = require(join(__dirname, '..', 'alternatives_report.spec.json'))
for (const example of examples) {
if (example.lang !== 'console') continue
if (!enabledFiles.includes(example.source_location.file)) continue
const asciidoc = generateAsciidoc(example.parsed_source)
writeFileSync(
join(docsExamplesDir, `${example.digest}.asciidoc`),
asciidoc,
'utf8'
)
}
}
function generateAsciidoc (source) {
var asciidoc = '// This file is autogenerated, DO NOT EDIT\n'
asciidoc += '// Use `node scripts/generate-docs-examples.js` to generate the docs examples\n\n'
var code = 'async function run (client) {\n// START\n'
for (var i = 0; i < source.length; i++) {
const { api, query, params, body } = source[i]
const apiArguments = Object.assign({}, params, query, body ? { body } : body)
var serializedApiArguments = Object.keys(apiArguments).length > 0
? JSON.stringify(apiArguments, null, 2)
: ''
serializedApiArguments = serializedApiArguments.replace(/"/g, "'")
code += `const response${getResponsePostfix(i)} = await client.${api.replace(/_([a-z])/g, g => g[1].toUpperCase())}(${serializedApiArguments})
console.log(response${getResponsePostfix(i)})
\n`
}
code += '// END\n}'
const { results } = standard.lintTextSync(code, { fix: true })
code = results[0].output
code = code.slice(code.indexOf('// START\n') + 9, code.indexOf('\n\n// END'))
asciidoc += `[source, js]
----
${dedent(code)}
----
`
return asciidoc
function getResponsePostfix (i) {
if (source.length === 1) return ''
return String(i)
}
}
generate()