From 83a464ac6697df828cb359ffcbfd9a9d28dce48d Mon Sep 17 00:00:00 2001 From: Spencer Date: Wed, 23 May 2018 14:31:34 -0700 Subject: [PATCH] Update docs to use async/await (#667) * [docs] update readme and examples to use async/await * [apis] regenerate (including updated examples) --- README.md | 27 +- docs/_examples/cluster.nodeInfo.asciidoc | 11 +- docs/_examples/count.asciidoc | 13 +- docs/_examples/create.asciidoc | 6 +- docs/_examples/delete.asciidoc | 6 +- docs/_examples/deleteByQuery.asciidoc | 10 +- docs/_examples/exists.asciidoc | 10 +- docs/_examples/explain.asciidoc | 10 +- docs/_examples/get.asciidoc | 6 +- docs/_examples/index.asciidoc | 6 +- docs/_examples/indices.updateAliases.asciidoc | 8 +- docs/_examples/mget.asciidoc | 10 +- docs/_examples/mlt.asciidoc | 6 +- docs/_examples/msearch.asciidoc | 2 +- docs/_examples/percolate.asciidoc | 86 +++--- docs/_examples/scroll.asciidoc | 38 ++- docs/_examples/search.asciidoc | 10 +- docs/_examples/suggest.asciidoc | 23 +- docs/_examples/update.asciidoc | 18 +- docs/api_methods.asciidoc | 142 ++++------ docs/api_methods_0_90.asciidoc | 226 +++++++--------- docs/api_methods_1_7.asciidoc | 216 +++++++-------- docs/api_methods_2_4.asciidoc | 202 ++++++-------- docs/api_methods_5_0.asciidoc | 249 ++++++++---------- docs/api_methods_5_1.asciidoc | 249 ++++++++---------- docs/api_methods_5_2.asciidoc | 249 ++++++++---------- docs/api_methods_5_3.asciidoc | 249 ++++++++---------- docs/api_methods_5_4.asciidoc | 249 ++++++++---------- docs/api_methods_5_5.asciidoc | 249 ++++++++---------- docs/api_methods_5_6.asciidoc | 249 ++++++++---------- docs/api_methods_6_0.asciidoc | 142 ++++------ docs/api_methods_6_1.asciidoc | 142 ++++------ src/lib/apis/6_x.js | 5 + 33 files changed, 1364 insertions(+), 1760 deletions(-) diff --git a/README.md b/README.md index 87c418ac3..61a34fd8e 100644 --- a/README.md +++ b/README.md @@ -70,18 +70,19 @@ client.ping({ Skip the callback to get a promise back ```js -client.search({ - q: 'pants' -}).then(function (body) { - var hits = body.hits.hits; -}, function (error) { - console.trace(error.message); -}); +try { + const response = await client.search({ + q: 'pants' + }); + console.log(response.hits.hits) +} catch (error) { + console.trace(error.message) +} ``` Find tweets that have "elasticsearch" in their body field ```js -client.search({ +const response = await client.search({ index: 'twitter', type: 'tweets', body: { @@ -91,11 +92,11 @@ client.search({ } } } -}).then(function (resp) { - var hits = resp.hits.hits; -}, function (err) { - console.trace(err.message); -}); +}) + +for (const tweet of response.hits.hits) { + console.log('tweet:', tweet); +} ``` More examples and detailed information about each method are available [here](http://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html) diff --git a/docs/_examples/cluster.nodeInfo.asciidoc b/docs/_examples/cluster.nodeInfo.asciidoc index 027085a4c..b9787f8d7 100644 --- a/docs/_examples/cluster.nodeInfo.asciidoc +++ b/docs/_examples/cluster.nodeInfo.asciidoc @@ -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! - }) ---------- \ No newline at end of file +const response = await client.cluster.nodeInfo({ + jvm: true +}); +--------- diff --git a/docs/_examples/count.asciidoc b/docs/_examples/count.asciidoc index dbdf194d4..6ec51108f 100644 --- a/docs/_examples/count.asciidoc +++ b/docs/_examples/count.asciidoc @@ -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) { - // ... }); --------- diff --git a/docs/_examples/create.asciidoc b/docs/_examples/create.asciidoc index 119c0c0df..4cdfd8ab1 100644 --- a/docs/_examples/create.asciidoc +++ b/docs/_examples/create.asciidoc @@ -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) { - // ... }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/delete.asciidoc b/docs/_examples/delete.asciidoc index 50d5672e5..99d4fbd30 100644 --- a/docs/_examples/delete.asciidoc +++ b/docs/_examples/delete.asciidoc @@ -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) { - // ... }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/deleteByQuery.asciidoc b/docs/_examples/deleteByQuery.asciidoc index d3ced3c7c..ec2cc3721 100644 --- a/docs/_examples/deleteByQuery.asciidoc +++ b/docs/_examples/deleteByQuery.asciidoc @@ -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) { - // ... }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/exists.asciidoc b/docs/_examples/exists.asciidoc index 7d6d43d5a..d95809299 100644 --- a/docs/_examples/exists.asciidoc +++ b/docs/_examples/exists.asciidoc @@ -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 { - // ... - } }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/explain.asciidoc b/docs/_examples/explain.asciidoc index a82b87808..47d8a4f9f 100644 --- a/docs/_examples/explain.asciidoc +++ b/docs/_examples/explain.asciidoc @@ -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) { - // ... }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/get.asciidoc b/docs/_examples/get.asciidoc index a0bb4cc37..cd007bc3f 100644 --- a/docs/_examples/get.asciidoc +++ b/docs/_examples/get.asciidoc @@ -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) { - // ... }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/index.asciidoc b/docs/_examples/index.asciidoc index 10c710ec6..89c8f843d 100644 --- a/docs/_examples/index.asciidoc +++ b/docs/_examples/index.asciidoc @@ -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) { - }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/indices.updateAliases.asciidoc b/docs/_examples/indices.updateAliases.asciidoc index 0e97c7eaf..c2d9633b9 100644 --- a/docs/_examples/indices.updateAliases.asciidoc +++ b/docs/_examples/indices.updateAliases.asciidoc @@ -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); ---------- \ No newline at end of file +}); +--------- diff --git a/docs/_examples/mget.asciidoc b/docs/_examples/mget.asciidoc index 964d4bba2..0d0addcd9 100644 --- a/docs/_examples/mget.asciidoc +++ b/docs/_examples/mget.asciidoc @@ -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){ - // ... }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/mlt.asciidoc b/docs/_examples/mlt.asciidoc index f26cf5b68..8adfa9c7c 100644 --- a/docs/_examples/mlt.asciidoc +++ b/docs/_examples/mlt.asciidoc @@ -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) { - // ... }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/msearch.asciidoc b/docs/_examples/msearch.asciidoc index 3c798f5d1..441ca9bf2 100644 --- a/docs/_examples/msearch.asciidoc +++ b/docs/_examples/msearch.asciidoc @@ -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 {}, diff --git a/docs/_examples/percolate.asciidoc b/docs/_examples/percolate.asciidoc index df367e2bd..8d6c19af6 100644 --- a/docs/_examples/percolate.asciidoc +++ b/docs/_examples/percolate.asciidoc @@ -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' } - // ] - // } }); ---------- \ No newline at end of file + +// response2 should look something like +// { +// total: 2, +// matches: [ +// { _index: 'myindex', _id: 'alert-1' }, +// { _index: 'myindex', _id: 'alert-2' } +// ] +// } +--------- diff --git a/docs/_examples/scroll.asciidoc b/docs/_examples/scroll.asciidoc index 02e8269db..7e31f87d6 100644 --- a/docs/_examples/scroll.asciidoc +++ b/docs/_examples/scroll.asciidoc @@ -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); - } -}); + }) + ); +} --------- diff --git a/docs/_examples/search.asciidoc b/docs/_examples/search.asciidoc index 179544aeb..bac80fd02 100644 --- a/docs/_examples/search.asciidoc +++ b/docs/_examples/search.asciidoc @@ -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) { - // ... }); ---------- \ No newline at end of file +--------- diff --git a/docs/_examples/suggest.asciidoc b/docs/_examples/suggest.asciidoc index 28789494c..35cce1011 100644 --- a/docs/_examples/suggest.asciidoc +++ b/docs/_examples/suggest.asciidoc @@ -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: { // } // ] // } -}); ---------- \ No newline at end of file + +--------- diff --git a/docs/_examples/update.asciidoc b/docs/_examples/update.asciidoc index bca50ab04..eac20fb86 100644 --- a/docs/_examples/update.asciidoc +++ b/docs/_examples/update.asciidoc @@ -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) { - // ... }); ---------- \ No newline at end of file +--------- diff --git a/docs/api_methods.asciidoc b/docs/api_methods.asciidoc index 4a113a415..c252a4e26 100644 --- a/docs/api_methods.asciidoc +++ b/docs/api_methods.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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) { - // ... }); --------- @@ -218,7 +211,7 @@ Check the *<>* and https://www.elastic.co/guide/en/elasticsearc .Create a document [source,js] --------- -client.create({ +await client.create({ index: 'myindex', type: 'mytype', id: '1', @@ -229,12 +222,11 @@ client.create({ published_at: '2013-01-01', counter: 1 } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -292,16 +284,15 @@ Check the *<>* 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] @@ -357,30 +348,27 @@ Check the *<>* 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] @@ -519,20 +507,15 @@ Check the *<>* 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] @@ -645,7 +628,7 @@ Check the *<>* 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', @@ -653,15 +636,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', @@ -670,12 +651,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -774,16 +754,15 @@ Check the *<>* 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] @@ -921,7 +900,7 @@ Check the *<>* 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', @@ -930,12 +909,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1015,7 +993,7 @@ Check the *<>* 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' }, @@ -1023,26 +1001,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] @@ -1086,7 +1061,7 @@ Check the *<>* 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 {}, @@ -1401,30 +1376,40 @@ Check the *<>* 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); - } -}); + }) + ); +} --------- @@ -1460,18 +1445,16 @@ Check the *<>* 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: { @@ -1487,12 +1470,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -1777,7 +1759,7 @@ Check the *<>* 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', @@ -1787,15 +1769,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', @@ -1803,15 +1783,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', @@ -1821,15 +1799,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', @@ -1839,12 +1815,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4419,19 +4394,18 @@ Check the *<>* 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] diff --git a/docs/api_methods_0_90.asciidoc b/docs/api_methods_0_90.asciidoc index 78fdd70f9..a74659255 100644 --- a/docs/api_methods_0_90.asciidoc +++ b/docs/api_methods_0_90.asciidoc @@ -109,26 +109,21 @@ Check the *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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] diff --git a/docs/api_methods_1_7.asciidoc b/docs/api_methods_1_7.asciidoc index bdb790c08..c3a7ed40c 100644 --- a/docs/api_methods_1_7.asciidoc +++ b/docs/api_methods_1_7.asciidoc @@ -111,26 +111,21 @@ Check the *<>* 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: { @@ -143,8 +138,6 @@ client.count({ } } } -}, function (err, response) { - // ... }); --------- @@ -272,7 +265,7 @@ Check the *<>* and https://www.elastic.co/guide/en/elasticsearc .Create a document [source,js] --------- -client.create({ +await client.create({ index: 'myindex', type: 'mytype', id: '1', @@ -283,12 +276,11 @@ client.create({ published_at: '2013-01-01', counter: 1 } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -353,16 +345,15 @@ Check the *<>* 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] @@ -423,30 +414,27 @@ Check the *<>* 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] @@ -587,20 +575,15 @@ Check the *<>* 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] @@ -640,7 +623,7 @@ Check the *<>* 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', @@ -648,15 +631,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', @@ -665,12 +646,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -777,16 +757,15 @@ Check the *<>* 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] @@ -969,7 +948,7 @@ Check the *<>* 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', @@ -978,12 +957,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1070,7 +1048,7 @@ Check the *<>* 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' }, @@ -1078,26 +1056,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] @@ -1139,17 +1114,16 @@ Check the *<>* 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] @@ -1254,7 +1228,7 @@ Check the *<>* 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 {}, @@ -1355,43 +1329,41 @@ Check the *<>* 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: { @@ -1399,15 +1371,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: { @@ -1415,19 +1387,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] @@ -1656,18 +1629,16 @@ Check the *<>* 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: { @@ -1683,12 +1654,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -1966,17 +1936,18 @@ Check the *<>* 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: // // { @@ -1995,10 +1966,11 @@ body: { // } // ] // } -}); + --------- + *Params* [horizontal] @@ -2092,7 +2064,7 @@ Check the *<>* 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', @@ -2102,15 +2074,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', @@ -2118,15 +2088,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', @@ -2136,15 +2104,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', @@ -2154,12 +2120,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4340,19 +4305,18 @@ Check the *<>* 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] diff --git a/docs/api_methods_2_4.asciidoc b/docs/api_methods_2_4.asciidoc index 7f17e9163..acba36d67 100644 --- a/docs/api_methods_2_4.asciidoc +++ b/docs/api_methods_2_4.asciidoc @@ -107,26 +107,21 @@ Check the *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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] diff --git a/docs/api_methods_5_0.asciidoc b/docs/api_methods_5_0.asciidoc index 6aa22d88a..14376d928 100644 --- a/docs/api_methods_5_0.asciidoc +++ b/docs/api_methods_5_0.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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 *<>* 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 *<>* 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 *<>* 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] @@ -605,20 +593,15 @@ Check the *<>* 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] @@ -658,7 +641,7 @@ Check the *<>* 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', @@ -666,15 +649,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', @@ -683,12 +664,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -795,16 +775,15 @@ Check the *<>* 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] @@ -967,7 +946,7 @@ Check the *<>* 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', @@ -976,12 +955,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1065,7 +1043,7 @@ Check the *<>* 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' }, @@ -1073,26 +1051,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] @@ -1175,7 +1150,7 @@ Check the *<>* 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 {}, @@ -1321,43 +1296,41 @@ Check the *<>* 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: { @@ -1365,15 +1338,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: { @@ -1381,19 +1354,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] @@ -1615,30 +1589,40 @@ Check the *<>* 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); - } -}); + }) + ); +} --------- @@ -1674,18 +1658,16 @@ Check the *<>* 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: { @@ -1701,12 +1683,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -1919,17 +1900,18 @@ Check the *<>* 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: // // { @@ -1948,10 +1930,11 @@ body: { // } // ] // } -}); + --------- + *Params* [horizontal] @@ -2055,7 +2038,7 @@ Check the *<>* 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', @@ -2065,15 +2048,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', @@ -2081,15 +2062,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', @@ -2099,15 +2078,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', @@ -2117,12 +2094,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4561,19 +4537,18 @@ Check the *<>* 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] diff --git a/docs/api_methods_5_1.asciidoc b/docs/api_methods_5_1.asciidoc index 8b4e00649..68c64b07c 100644 --- a/docs/api_methods_5_1.asciidoc +++ b/docs/api_methods_5_1.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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) { - // ... }); --------- @@ -275,7 +268,7 @@ Check the *<>* and https://www.elastic.co/guide/en/elasticsearc .Create a document [source,js] --------- -client.create({ +await client.create({ index: 'myindex', type: 'mytype', id: '1', @@ -286,12 +279,11 @@ client.create({ published_at: '2013-01-01', counter: 1 } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -353,16 +345,15 @@ Check the *<>* 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] @@ -418,30 +409,27 @@ Check the *<>* 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] @@ -603,20 +591,15 @@ Check the *<>* 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] @@ -656,7 +639,7 @@ Check the *<>* 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', @@ -664,15 +647,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', @@ -681,12 +662,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -791,16 +771,15 @@ Check the *<>* 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] @@ -963,7 +942,7 @@ Check the *<>* 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', @@ -972,12 +951,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1061,7 +1039,7 @@ Check the *<>* 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' }, @@ -1069,26 +1047,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] @@ -1171,7 +1146,7 @@ Check the *<>* 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 {}, @@ -1317,43 +1292,41 @@ Check the *<>* 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: { @@ -1361,15 +1334,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: { @@ -1377,19 +1350,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] @@ -1613,30 +1587,40 @@ Check the *<>* 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); - } -}); + }) + ); +} --------- @@ -1672,18 +1656,16 @@ Check the *<>* 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: { @@ -1699,12 +1681,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -1919,17 +1900,18 @@ Check the *<>* 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: // // { @@ -1948,10 +1930,11 @@ body: { // } // ] // } -}); + --------- + *Params* [horizontal] @@ -2055,7 +2038,7 @@ Check the *<>* 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', @@ -2065,15 +2048,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', @@ -2081,15 +2062,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', @@ -2099,15 +2078,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', @@ -2117,12 +2094,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4638,19 +4614,18 @@ Check the *<>* 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] diff --git a/docs/api_methods_5_2.asciidoc b/docs/api_methods_5_2.asciidoc index 2fdd0e0c5..b35211af0 100644 --- a/docs/api_methods_5_2.asciidoc +++ b/docs/api_methods_5_2.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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) { - // ... }); --------- @@ -275,7 +268,7 @@ Check the *<>* and https://www.elastic.co/guide/en/elasticsearc .Create a document [source,js] --------- -client.create({ +await client.create({ index: 'myindex', type: 'mytype', id: '1', @@ -286,12 +279,11 @@ client.create({ published_at: '2013-01-01', counter: 1 } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -353,16 +345,15 @@ Check the *<>* 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] @@ -418,30 +409,27 @@ Check the *<>* 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] @@ -603,20 +591,15 @@ Check the *<>* 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] @@ -656,7 +639,7 @@ Check the *<>* 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', @@ -664,15 +647,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', @@ -681,12 +662,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -791,16 +771,15 @@ Check the *<>* 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] @@ -963,7 +942,7 @@ Check the *<>* 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', @@ -972,12 +951,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1061,7 +1039,7 @@ Check the *<>* 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' }, @@ -1069,26 +1047,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] @@ -1171,7 +1146,7 @@ Check the *<>* 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 {}, @@ -1317,43 +1292,41 @@ Check the *<>* 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: { @@ -1361,15 +1334,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: { @@ -1377,19 +1350,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] @@ -1613,30 +1587,40 @@ Check the *<>* 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); - } -}); + }) + ); +} --------- @@ -1672,18 +1656,16 @@ Check the *<>* 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: { @@ -1699,12 +1681,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -1919,17 +1900,18 @@ Check the *<>* 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: // // { @@ -1948,10 +1930,11 @@ body: { // } // ] // } -}); + --------- + *Params* [horizontal] @@ -2055,7 +2038,7 @@ Check the *<>* 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', @@ -2065,15 +2048,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', @@ -2081,15 +2062,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', @@ -2099,15 +2078,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', @@ -2117,12 +2094,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4638,19 +4614,18 @@ Check the *<>* 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] diff --git a/docs/api_methods_5_3.asciidoc b/docs/api_methods_5_3.asciidoc index cfe56608d..56016abea 100644 --- a/docs/api_methods_5_3.asciidoc +++ b/docs/api_methods_5_3.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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) { - // ... }); --------- @@ -275,7 +268,7 @@ Check the *<>* and https://www.elastic.co/guide/en/elasticsearc .Create a document [source,js] --------- -client.create({ +await client.create({ index: 'myindex', type: 'mytype', id: '1', @@ -286,12 +279,11 @@ client.create({ published_at: '2013-01-01', counter: 1 } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -353,16 +345,15 @@ Check the *<>* 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] @@ -418,30 +409,27 @@ Check the *<>* 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] @@ -603,20 +591,15 @@ Check the *<>* 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] @@ -656,7 +639,7 @@ Check the *<>* 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', @@ -664,15 +647,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', @@ -681,12 +662,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -791,16 +771,15 @@ Check the *<>* 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] @@ -963,7 +942,7 @@ Check the *<>* 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', @@ -972,12 +951,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1061,7 +1039,7 @@ Check the *<>* 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' }, @@ -1069,26 +1047,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] @@ -1171,7 +1146,7 @@ Check the *<>* 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 {}, @@ -1317,43 +1292,41 @@ Check the *<>* 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: { @@ -1361,15 +1334,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: { @@ -1377,19 +1350,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] @@ -1613,30 +1587,40 @@ Check the *<>* 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); - } -}); + }) + ); +} --------- @@ -1672,18 +1656,16 @@ Check the *<>* 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: { @@ -1699,12 +1681,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -1919,17 +1900,18 @@ Check the *<>* 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: // // { @@ -1948,10 +1930,11 @@ body: { // } // ] // } -}); + --------- + *Params* [horizontal] @@ -2055,7 +2038,7 @@ Check the *<>* 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', @@ -2065,15 +2048,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', @@ -2081,15 +2062,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', @@ -2099,15 +2078,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', @@ -2117,12 +2094,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4624,19 +4600,18 @@ Check the *<>* 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] diff --git a/docs/api_methods_5_4.asciidoc b/docs/api_methods_5_4.asciidoc index 29f35f27e..28685627e 100644 --- a/docs/api_methods_5_4.asciidoc +++ b/docs/api_methods_5_4.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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) { - // ... }); --------- @@ -275,7 +268,7 @@ Check the *<>* and https://www.elastic.co/guide/en/elasticsearc .Create a document [source,js] --------- -client.create({ +await client.create({ index: 'myindex', type: 'mytype', id: '1', @@ -286,12 +279,11 @@ client.create({ published_at: '2013-01-01', counter: 1 } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -353,16 +345,15 @@ Check the *<>* 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] @@ -418,30 +409,27 @@ Check the *<>* 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] @@ -603,20 +591,15 @@ Check the *<>* 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] @@ -729,7 +712,7 @@ Check the *<>* 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', @@ -737,15 +720,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', @@ -754,12 +735,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -903,16 +883,15 @@ Check the *<>* 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] @@ -1075,7 +1054,7 @@ Check the *<>* 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', @@ -1084,12 +1063,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1173,7 +1151,7 @@ Check the *<>* 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' }, @@ -1181,26 +1159,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] @@ -1283,7 +1258,7 @@ Check the *<>* 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 {}, @@ -1433,43 +1408,41 @@ Check the *<>* 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: { @@ -1477,15 +1450,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: { @@ -1493,19 +1466,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] @@ -1729,30 +1703,40 @@ Check the *<>* 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); - } -}); + }) + ); +} --------- @@ -1788,18 +1772,16 @@ Check the *<>* 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: { @@ -1815,12 +1797,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -2041,17 +2022,18 @@ Check the *<>* 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: // // { @@ -2070,10 +2052,11 @@ body: { // } // ] // } -}); + --------- + *Params* [horizontal] @@ -2177,7 +2160,7 @@ Check the *<>* 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', @@ -2187,15 +2170,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', @@ -2203,15 +2184,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', @@ -2221,15 +2200,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', @@ -2239,12 +2216,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4754,19 +4730,18 @@ Check the *<>* 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] diff --git a/docs/api_methods_5_5.asciidoc b/docs/api_methods_5_5.asciidoc index ef08a62b5..beaeae55b 100644 --- a/docs/api_methods_5_5.asciidoc +++ b/docs/api_methods_5_5.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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) { - // ... }); --------- @@ -275,7 +268,7 @@ Check the *<>* and https://www.elastic.co/guide/en/elasticsearc .Create a document [source,js] --------- -client.create({ +await client.create({ index: 'myindex', type: 'mytype', id: '1', @@ -286,12 +279,11 @@ client.create({ published_at: '2013-01-01', counter: 1 } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -353,16 +345,15 @@ Check the *<>* 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] @@ -418,30 +409,27 @@ Check the *<>* 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] @@ -603,20 +591,15 @@ Check the *<>* 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] @@ -729,7 +712,7 @@ Check the *<>* 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', @@ -737,15 +720,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', @@ -754,12 +735,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -903,16 +883,15 @@ Check the *<>* 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] @@ -1075,7 +1054,7 @@ Check the *<>* 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', @@ -1084,12 +1063,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1173,7 +1151,7 @@ Check the *<>* 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' }, @@ -1181,26 +1159,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] @@ -1283,7 +1258,7 @@ Check the *<>* 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 {}, @@ -1435,43 +1410,41 @@ Check the *<>* 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: { @@ -1479,15 +1452,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: { @@ -1495,19 +1468,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] @@ -1731,30 +1705,40 @@ Check the *<>* 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); - } -}); + }) + ); +} --------- @@ -1790,18 +1774,16 @@ Check the *<>* 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: { @@ -1817,12 +1799,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -2043,17 +2024,18 @@ Check the *<>* 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: // // { @@ -2072,10 +2054,11 @@ body: { // } // ] // } -}); + --------- + *Params* [horizontal] @@ -2179,7 +2162,7 @@ Check the *<>* 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', @@ -2189,15 +2172,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', @@ -2205,15 +2186,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', @@ -2223,15 +2202,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', @@ -2241,12 +2218,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4756,19 +4732,18 @@ Check the *<>* 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] diff --git a/docs/api_methods_5_6.asciidoc b/docs/api_methods_5_6.asciidoc index 2ebcebc2b..1fff9158e 100644 --- a/docs/api_methods_5_6.asciidoc +++ b/docs/api_methods_5_6.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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 *<>* 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] diff --git a/docs/api_methods_6_0.asciidoc b/docs/api_methods_6_0.asciidoc index d3fe4310f..13dbf181e 100644 --- a/docs/api_methods_6_0.asciidoc +++ b/docs/api_methods_6_0.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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) { - // ... }); --------- @@ -218,7 +211,7 @@ Check the *<>* and https://www.elastic.co/guide/en/elasticsearc .Create a document [source,js] --------- -client.create({ +await client.create({ index: 'myindex', type: 'mytype', id: '1', @@ -229,12 +222,11 @@ client.create({ published_at: '2013-01-01', counter: 1 } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -292,16 +284,15 @@ Check the *<>* 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] @@ -357,30 +348,27 @@ Check the *<>* 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] @@ -519,20 +507,15 @@ Check the *<>* 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] @@ -645,7 +628,7 @@ Check the *<>* 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', @@ -653,15 +636,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', @@ -670,12 +651,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -774,16 +754,15 @@ Check the *<>* 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] @@ -921,7 +900,7 @@ Check the *<>* 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', @@ -930,12 +909,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1015,7 +993,7 @@ Check the *<>* 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' }, @@ -1023,26 +1001,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] @@ -1086,7 +1061,7 @@ Check the *<>* 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 {}, @@ -1374,30 +1349,40 @@ Check the *<>* 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); - } -}); + }) + ); +} --------- @@ -1433,18 +1418,16 @@ Check the *<>* 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: { @@ -1460,12 +1443,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -1750,7 +1732,7 @@ Check the *<>* 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', @@ -1760,15 +1742,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', @@ -1776,15 +1756,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', @@ -1794,15 +1772,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', @@ -1812,12 +1788,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4363,19 +4338,18 @@ Check the *<>* 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] diff --git a/docs/api_methods_6_1.asciidoc b/docs/api_methods_6_1.asciidoc index 29b4ebb0d..052171fff 100644 --- a/docs/api_methods_6_1.asciidoc +++ b/docs/api_methods_6_1.asciidoc @@ -116,26 +116,21 @@ Check the *<>* 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) { - // ... }); --------- @@ -218,7 +211,7 @@ Check the *<>* and https://www.elastic.co/guide/en/elasticsearc .Create a document [source,js] --------- -client.create({ +await client.create({ index: 'myindex', type: 'mytype', id: '1', @@ -229,12 +222,11 @@ client.create({ published_at: '2013-01-01', counter: 1 } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -292,16 +284,15 @@ Check the *<>* 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] @@ -357,30 +348,27 @@ Check the *<>* 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] @@ -519,20 +507,15 @@ Check the *<>* 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] @@ -645,7 +628,7 @@ Check the *<>* 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', @@ -653,15 +636,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', @@ -670,12 +651,11 @@ client.explain({ match: { title: 'test' } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -774,16 +754,15 @@ Check the *<>* 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] @@ -921,7 +900,7 @@ Check the *<>* 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', @@ -930,12 +909,11 @@ client.index({ tags: ['y', 'z'], published: true, } -}, function (error, response) { - }); --------- + *Params* [horizontal] @@ -1015,7 +993,7 @@ Check the *<>* 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' }, @@ -1023,26 +1001,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] @@ -1086,7 +1061,7 @@ Check the *<>* 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 {}, @@ -1374,30 +1349,40 @@ Check the *<>* 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); - } -}); + }) + ); +} --------- @@ -1433,18 +1418,16 @@ Check the *<>* 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: { @@ -1460,12 +1443,11 @@ client.search({ } } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -1750,7 +1732,7 @@ Check the *<>* 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', @@ -1760,15 +1742,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', @@ -1776,15 +1756,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', @@ -1794,15 +1772,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', @@ -1812,12 +1788,11 @@ client.update({ tag: 'to-delete' } } -}, function (error, response) { - // ... }); --------- + *Params* [horizontal] @@ -4398,19 +4373,18 @@ Check the *<>* 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] diff --git a/src/lib/apis/6_x.js b/src/lib/apis/6_x.js index 8ea76c430..5ee6fdfc2 100644 --- a/src/lib/apis/6_x.js +++ b/src/lib/apis/6_x.js @@ -3664,6 +3664,7 @@ api.indices.prototype.getMapping = ca({ * Perform a [indices.getSettings](https://www.elastic.co/guide/en/elasticsearch/reference/6.x/indices-get-settings.html) request * * @param {Object} params - An object with parameters used to carry out this action + * @param {<>} params.masterTimeout - Specify timeout for connection to master * @param {<>} params.ignoreUnavailable - Whether specified concrete indices should be ignored when unavailable (missing or closed) * @param {<>} params.allowNoIndices - Whether to ignore if a wildcard indices expression resolves into no concrete indices. (This includes `_all` string or when no indices have been specified) * @param {<>} [params.expandWildcards=open,closed] - Whether to expand wildcard expression to concrete indices that are open, closed or both. @@ -3675,6 +3676,10 @@ api.indices.prototype.getMapping = ca({ */ api.indices.prototype.getSettings = ca({ params: { + masterTimeout: { + type: 'time', + name: 'master_timeout' + }, ignoreUnavailable: { type: 'boolean', name: 'ignore_unavailable'