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:
@ -116,26 +116,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: {
|
||||
@ -148,8 +143,6 @@ client.count({
|
||||
}
|
||||
}
|
||||
}
|
||||
}, function (err, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
@ -277,7 +270,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',
|
||||
@ -288,12 +281,11 @@ client.create({
|
||||
published_at: '2013-01-01',
|
||||
counter: 1
|
||||
}
|
||||
}, function (error, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -355,16 +347,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]
|
||||
@ -420,30 +411,27 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
|
||||
.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) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -609,20 +597,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]
|
||||
@ -735,7 +718,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',
|
||||
@ -743,15 +726,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',
|
||||
@ -760,12 +741,11 @@ client.explain({
|
||||
match: { title: 'test' }
|
||||
}
|
||||
}
|
||||
}, function (error, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -909,16 +889,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]
|
||||
@ -1081,7 +1060,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',
|
||||
@ -1090,12 +1069,11 @@ client.index({
|
||||
tags: ['y', 'z'],
|
||||
published: true,
|
||||
}
|
||||
}, function (error, response) {
|
||||
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -1179,7 +1157,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' },
|
||||
@ -1187,26 +1165,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]
|
||||
@ -1289,7 +1264,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
|
||||
{},
|
||||
@ -1443,43 +1418,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: {
|
||||
@ -1487,15 +1460,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: {
|
||||
@ -1503,19 +1476,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]
|
||||
@ -1743,30 +1717,40 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
|
||||
.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);
|
||||
}
|
||||
});
|
||||
})
|
||||
);
|
||||
}
|
||||
---------
|
||||
|
||||
|
||||
@ -1802,18 +1786,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: {
|
||||
@ -1829,12 +1811,11 @@ client.search({
|
||||
}
|
||||
}
|
||||
}
|
||||
}, function (error, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -2059,17 +2040,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:
|
||||
//
|
||||
// {
|
||||
@ -2088,10 +2070,11 @@ body: {
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
});
|
||||
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -2195,7 +2178,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',
|
||||
@ -2205,15 +2188,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',
|
||||
@ -2221,15 +2202,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',
|
||||
@ -2239,15 +2218,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',
|
||||
@ -2257,12 +2234,11 @@ client.update({
|
||||
tag: 'to-delete'
|
||||
}
|
||||
}
|
||||
}, function (error, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -4818,19 +4794,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]
|
||||
|
||||
Reference in New Issue
Block a user