Update docs to use async/await (#667)

* [docs] update readme and examples to use async/await

* [apis] regenerate (including updated examples)
This commit is contained in:
Spencer
2018-05-23 14:31:34 -07:00
committed by GitHub
parent a187ed6298
commit 83a464ac66
33 changed files with 1364 additions and 1760 deletions

View File

@ -107,26 +107,21 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Get the number of all documents in the cluster
[source,js]
---------
client.count(function (error, response, status) {
// check for and handle error
var count = response.count;
});
const { count } = await client.count();
---------
.Get the number of documents in an index
[source,js]
---------
client.count({
const { count } = await client.count({
index: 'index_name'
}, function (error, response) {
// ...
});
---------
.Get the number of documents matching a query
[source,js]
---------
client.count({
const { count } = await client.count({
index: 'index_name',
body: {
query: {
@ -139,8 +134,6 @@ client.count({
}
}
}
}, function (err, response) {
// ...
});
---------
@ -268,7 +261,7 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Create a document
[source,js]
---------
client.create({
await client.create({
index: 'myindex',
type: 'mytype',
id: '1',
@ -279,12 +272,11 @@ client.create({
published_at: '2013-01-01',
counter: 1
}
}, function (error, response) {
// ...
});
---------
*Params*
[horizontal]
@ -343,16 +335,15 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Delete the document `/myindex/mytype/1`
[source,js]
---------
client.delete({
await client.delete({
index: 'myindex',
type: 'mytype',
id: '1'
}, function (error, response) {
// ...
});
---------
*Params*
[horizontal]
@ -479,20 +470,15 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Check that the document `/myindex/mytype/1` exist
[source,js]
---------
client.exists({
const exists = await client.exists({
index: 'myindex',
type: 'mytype',
id: 1
}, function (error, exists) {
if (exists === true) {
// ...
} else {
// ...
}
});
---------
*Params*
[horizontal]
@ -532,7 +518,7 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.See how a document is scored against a simple query
[source,js]
---------
client.explain({
const response = await client.explain({
// the document to test
index: 'myindex',
type: 'mytype',
@ -540,15 +526,13 @@ client.explain({
// the query to score it against
q: 'field:value'
}, function (error, response) {
// ...
});
---------
.See how a document is scored against a query written in the Query DSL
[source,js]
---------
client.explain({
const response = await client.explain({
index: 'myindex',
type: 'mytype',
id: '1',
@ -557,12 +541,11 @@ client.explain({
match: { title: 'test' }
}
}
}, function (error, response) {
// ...
});
---------
*Params*
[horizontal]
@ -669,16 +652,15 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Get `/myindex/mytype/1`
[source,js]
---------
client.get({
const response = await client.get({
index: 'myindex',
type: 'mytype',
id: 1
}, function (error, response) {
// ...
});
---------
*Params*
[horizontal]
@ -861,7 +843,7 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Create or update a document
[source,js]
---------
client.index({
const response = await client.index({
index: 'myindex',
type: 'mytype',
id: '1',
@ -870,12 +852,11 @@ client.index({
tags: ['y', 'z'],
published: true,
}
}, function (error, response) {
});
---------
*Params*
[horizontal]
@ -956,7 +937,7 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.An array of doc locations. Useful for getting documents from different indices.
[source,js]
---------
client.mget({
const response = await client.mget({
body: {
docs: [
{ _index: 'indexA', _type: 'typeA', _id: '1' },
@ -964,26 +945,23 @@ client.mget({
{ _index: 'indexC', _type: 'typeC', _id: '1' }
]
}
}, function(error, response){
// ...
});
---------
.An array of ids. You must also specify the `index` and `type` that apply to all of the ids.
[source,js]
---------
client.mget({
const response = await client.mget({
index: 'myindex',
type: 'mytype',
body: {
ids: [1, 2, 3]
}
}, function(error, response){
// ...
});
---------
*Params*
[horizontal]
@ -1066,7 +1044,7 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Perform multiple different searches, the body is made up of meta/data pairs
[source,js]
---------
client.msearch({
const response = await client.msearch({
body: [
// match all query, on all indices and types
{},
@ -1177,43 +1155,41 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.First, Register queries named “alert-1” and “alert-2” for the “myindex” index
[source,js]
---------
client.index({
index: 'myindex',
type: '.percolator',
id: 'alert-1',
body: {
// This query will be run against documents sent to percolate
query: {
query_string: {
query: 'foo'
await Promise.all([
client.index({
index: 'myindex',
type: '.percolator',
id: 'alert-1',
body: {
// This query will be run against documents sent to percolate
query: {
query_string: {
query: 'foo'
}
}
}
}
}, function (error, response) {
// ...
});
}),
client.index({
index: 'myindex',
type: '.percolator',
id: 'alert-2',
body: {
// This query will also be run against documents sent to percolate
query: {
query_string: {
query: 'bar'
client.index({
index: 'myindex',
type: '.percolator',
id: 'alert-2',
body: {
// This query will also be run against documents sent to percolate
query: {
query_string: {
query: 'bar'
}
}
}
}
}, function (error, response) {
// ...
});
})
]);
---------
.Then you can send documents to learn which query `_percolator` queries they match
[source,js]
---------
client.percolate({
const response1 = await client.percolate({
index: 'myindex',
type: 'mytype',
body: {
@ -1221,15 +1197,15 @@ client.percolate({
title: "Foo"
}
}
}, function (error, response) {
// response would equal
// {
// total: 1,
// matches: [ { _index: 'myindex', _id: 'alert-1' } ]
// }
});
client.percolate({
// response1 should look something like
// {
// total: 1,
// matches: [ { _index: 'myindex', _id: 'alert-1' } ]
// }
const response2 = await client.percolate({
index: 'myindex',
type: 'mytype',
body: {
@ -1237,19 +1213,20 @@ client.percolate({
title: "Foo Bar"
}
}
}, function (error, response) {
// response would equal
// {
// total: 2,
// matches: [
// { _index: 'myindex', _id: 'alert-1' },
// { _index: 'myindex', _id: 'alert-2' }
// ]
// }
});
// response2 should look something like
// {
// total: 2,
// matches: [
// { _index: 'myindex', _id: 'alert-1' },
// { _index: 'myindex', _id: 'alert-2' }
// ]
// }
---------
*Params*
[horizontal]
@ -1568,18 +1545,16 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Search with a simple query string query
[source,js]
---------
client.search({
const response = await client.search({
index: 'myindex',
q: 'title:test'
}, function (error, response) {
// ...
});
---------
.Passing a full request definition in the Elasticsearch's Query DSL as a `Hash`
[source,js]
---------
client.search({
const response = await client.search({
index: 'myindex',
body: {
query: {
@ -1595,12 +1570,11 @@ client.search({
}
}
}
}, function (error, response) {
// ...
});
---------
*Params*
[horizontal]
@ -1878,17 +1852,18 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Return query terms suggestions (“auto-correction”)
[source,js]
---------
client.suggest({
index: 'myindex',
body: {
mysuggester: {
text: 'tset',
term: {
field: 'title'
const response = await client.suggest({
index: 'myindex',
body: {
mysuggester: {
text: 'tset',
term: {
field: 'title'
}
}
}
}
}, function (error, response) {
});
// response will be formatted like so:
//
// {
@ -1907,10 +1882,11 @@ body: {
// }
// ]
// }
});
---------
*Params*
[horizontal]
@ -2016,7 +1992,7 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Update document title using partial document
[source,js]
---------
client.update({
const response = await client.update({
index: 'myindex',
type: 'mytype',
id: '1',
@ -2026,15 +2002,13 @@ client.update({
title: 'Updated'
}
}
}, function (error, response) {
// ...
})
---------
.Add a tag to document `tags` property using a `script`
[source,js]
---------
client.update({
const response = await client.update({
index: 'myindex',
type: 'mytype',
id: '1',
@ -2042,15 +2016,13 @@ client.update({
script: 'ctx._source.tags += tag',
params: { tag: 'some new tag' }
}
}, function (error, response) {
// ...
});
---------
.Increment a document counter by 1 or initialize it, when the document does not exist
[source,js]
---------
client.update({
const response = await client.update({
index: 'myindex',
type: 'mytype',
id: '777',
@ -2060,15 +2032,13 @@ client.update({
counter: 1
}
}
}, function (error, response) {
// ...
})
---------
.Delete a document if it's tagged “to-delete”
[source,js]
---------
client.update({
const response = await client.update({
index: 'myindex',
type: 'mytype',
id: '1',
@ -2078,12 +2048,11 @@ client.update({
tag: 'to-delete'
}
}
}, function (error, response) {
// ...
});
---------
*Params*
[horizontal]
@ -4514,19 +4483,18 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
.Perform an atomic alias swap, for a rotating index
[source,js]
---------
client.indices.updateAliases({
const response = await client.indices.updateAliases({
body: {
actions: [
{ remove: { index: 'logstash-2014.04', alias: 'logstash-current' } },
{ add: { index: 'logstash-2014.05', alias: 'logstash-current' } }
]
}
}).then(function (response) {
// ...
}, errorHandler);
});
---------
*Params*
[horizontal]