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:
@ -109,26 +109,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: {
|
||||
@ -141,8 +136,6 @@ client.count({
|
||||
}
|
||||
}
|
||||
}
|
||||
}, function (err, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
@ -189,7 +182,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',
|
||||
@ -200,12 +193,11 @@ client.create({
|
||||
published_at: '2013-01-01',
|
||||
counter: 1
|
||||
}
|
||||
}, function (error, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -270,16 +262,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]
|
||||
@ -338,30 +329,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]
|
||||
@ -426,20 +414,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]
|
||||
@ -479,7 +462,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',
|
||||
@ -487,15 +470,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',
|
||||
@ -504,12 +485,11 @@ client.explain({
|
||||
match: { title: 'test' }
|
||||
}
|
||||
}
|
||||
}, function (error, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -573,16 +553,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]
|
||||
@ -685,7 +664,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',
|
||||
@ -694,12 +673,11 @@ client.index({
|
||||
tags: ['y', 'z'],
|
||||
published: true,
|
||||
}
|
||||
}, function (error, response) {
|
||||
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -786,7 +764,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' },
|
||||
@ -794,26 +772,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]
|
||||
@ -855,17 +830,16 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
|
||||
.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) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -933,7 +907,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
|
||||
{},
|
||||
@ -985,43 +959,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: {
|
||||
@ -1029,15 +1001,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: {
|
||||
@ -1045,19 +1017,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]
|
||||
@ -1163,18 +1136,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: {
|
||||
@ -1190,12 +1161,11 @@ client.search({
|
||||
}
|
||||
}
|
||||
}
|
||||
}, function (error, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -1302,17 +1272,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:
|
||||
//
|
||||
// {
|
||||
@ -1331,10 +1302,11 @@ body: {
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
});
|
||||
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -1375,7 +1347,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',
|
||||
@ -1385,15 +1357,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',
|
||||
@ -1401,15 +1371,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',
|
||||
@ -1419,15 +1387,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',
|
||||
@ -1437,12 +1403,11 @@ client.update({
|
||||
tag: 'to-delete'
|
||||
}
|
||||
}
|
||||
}, function (error, response) {
|
||||
// ...
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -1628,15 +1593,13 @@ Check the *<<api-conventions>>* and https://www.elastic.co/guide/en/elasticsearc
|
||||
.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
|
||||
});
|
||||
---------
|
||||
|
||||
|
||||
|
||||
*Params*
|
||||
|
||||
[horizontal]
|
||||
@ -2952,19 +2915,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