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

@ -1,10 +1,7 @@
.Return information about JVM
[source,js]
---------
client.cluster.nodeInfo({ jvm: true })
.then(function (response) {
// enjoy your sweet info!
}, function (error) {
// scream!
})
---------
const response = await client.cluster.nodeInfo({
jvm: true
});
---------

View File

@ -1,26 +1,21 @@
.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: {
@ -33,7 +28,5 @@ client.count({
}
}
}
}, function (err, response) {
// ...
});
---------

View File

@ -1,7 +1,7 @@
.Create a document
[source,js]
---------
client.create({
await client.create({
index: 'myindex',
type: 'mytype',
id: '1',
@ -12,7 +12,5 @@ client.create({
published_at: '2013-01-01',
counter: 1
}
}, function (error, response) {
// ...
});
---------
---------

View File

@ -1,11 +1,9 @@
.Delete the document `/myindex/mytype/1`
[source,js]
---------
client.delete({
await client.delete({
index: 'myindex',
type: 'mytype',
id: '1'
}, function (error, response) {
// ...
});
---------
---------

View File

@ -1,25 +1,21 @@
.Deleting documents with a simple query
[source,js]
---------
client.deleteByQuery({
await client.deleteByQuery({
index: 'myindex',
q: 'test'
}, function (error, response) {
// ...
});
---------
.Deleting documents using the Query DSL
[source,js]
---------
client.deleteByQuery({
await client.deleteByQuery({
index: 'posts',
body: {
query: {
term: { published: false }
}
}
}, function (error, response) {
// ...
});
---------
---------

View File

@ -1,15 +1,9 @@
.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 {
// ...
}
});
---------
---------

View File

@ -1,7 +1,7 @@
.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',
@ -9,15 +9,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',
@ -26,7 +24,5 @@ client.explain({
match: { title: 'test' }
}
}
}, function (error, response) {
// ...
});
---------
---------

View File

@ -1,11 +1,9 @@
.Get `/myindex/mytype/1`
[source,js]
---------
client.get({
const response = await client.get({
index: 'myindex',
type: 'mytype',
id: 1
}, function (error, response) {
// ...
});
---------
---------

View File

@ -1,7 +1,7 @@
.Create or update a document
[source,js]
---------
client.index({
const response = await client.index({
index: 'myindex',
type: 'mytype',
id: '1',
@ -10,7 +10,5 @@ client.index({
tags: ['y', 'z'],
published: true,
}
}, function (error, response) {
});
---------
---------

View File

@ -1,14 +1,12 @@
.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);
---------
});
---------

View File

@ -1,7 +1,7 @@
.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' },
@ -9,21 +9,17 @@ 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){
// ...
});
---------
---------

View File

@ -1,12 +1,10 @@
.Search for similar documents using the `title` property of document `myindex/mytype/1`
[source,js]
---------
client.mlt({
const response = await client.mlt({
index: 'myindex',
type: 'mytype',
id: 1,
mlt_fields: 'title'
}, function (errors, response) {
// ...
});
---------
---------

View File

@ -1,7 +1,7 @@
.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
{},

View File

@ -1,43 +1,41 @@
.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: {
@ -45,15 +43,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: {
@ -61,14 +59,14 @@ 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' }
// ]
// }
---------

View File

@ -1,28 +1,38 @@
.Collect every title in the index that contains the word "test"
[source,js]
---------
var allTitles = [];
const allTitles = [];
const responseQueue = [];
// first we do a search, and specify a scroll timeout
client.search({
// start things off by searching, setting a scroll timeout, and pushing
// our first response into the queue to be processed
await client.search({
index: 'myindex',
scroll: '30s', // keep the search results "scrollable" for 30 seconds
source: ['title'], // filter the source to only include the title field
q: 'title:test'
}, function getMoreUntilDone(error, response) {
// collect the title from each response
})
while (responseQueue.length) {
const response = responseQueue.shift();
// collect the titles from this response
response.hits.hits.forEach(function (hit) {
allTitles.push(hit._source.title);
allTitles.push(hit.fields.title);
});
if (response.hits.total > allTitles.length) {
// ask elasticsearch for the next set of hits from this search
client.scroll({
// check to see if we have collected all of the titles
if (response.hits.total === allTitles.length) {
console.log('every "test" title', allTitles);
break
}
// get the next response if there are more titles to fetch
responseQueue.push(
await client.scroll({
scrollId: response._scroll_id,
scroll: '30s'
}, getMoreUntilDone);
} else {
console.log('every "test" title', allTitles);
}
});
})
);
}
---------

View File

@ -1,18 +1,16 @@
.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: {
@ -28,7 +26,5 @@ client.search({
}
}
}
}, function (error, response) {
// ...
});
---------
---------

View File

@ -1,17 +1,18 @@
.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:
//
// {
@ -30,5 +31,5 @@ body: {
// }
// ]
// }
});
---------
---------

View File

@ -1,7 +1,7 @@
.Update document title using partial document
[source,js]
---------
client.update({
const response = await client.update({
index: 'myindex',
type: 'mytype',
id: '1',
@ -11,15 +11,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',
@ -27,15 +25,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',
@ -45,15 +41,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',
@ -63,7 +57,5 @@ client.update({
tag: 'to-delete'
}
}
}, function (error, response) {
// ...
});
---------
---------