From f704d4fcf39e1d0e6865ca6354383b8f5c105060 Mon Sep 17 00:00:00 2001 From: delvedor Date: Thu, 14 Feb 2019 18:34:20 +0100 Subject: [PATCH] Added examples --- docs/examples/msearch.asciidoc | 51 ++++++++++++++++++++ docs/examples/scroll.asciidoc | 79 +++++++++++++++++++++++++++++++ docs/examples/suggest.asciidoc | 59 +++++++++++++++++++++++ docs/examples/typescript.asciidoc | 36 +++++++------- 4 files changed, 209 insertions(+), 16 deletions(-) create mode 100644 docs/examples/msearch.asciidoc create mode 100644 docs/examples/suggest.asciidoc diff --git a/docs/examples/msearch.asciidoc b/docs/examples/msearch.asciidoc new file mode 100644 index 000000000..f81cc0c04 --- /dev/null +++ b/docs/examples/msearch.asciidoc @@ -0,0 +1,51 @@ += MSearch + +The multi search API allows to execute several search requests within the same API. + +[source,js] +---- +'use strict' + +const { Client } = require('@elastic/elasticsearch') +const client = new Client({ node: 'http://localhost:9200' }) + +async function run () { + await client.bulk({ + refresh: true, + body: [ + { index: { _index: 'game-of-thrones' } }, + { + character: 'Ned Stark', + quote: 'Winter is coming.' + }, + + { index: { _index: 'game-of-thrones' } }, + { + character: 'Daenerys Targaryen', + quote: 'I am the blood of the dragon.' + }, + + { index: { _index: 'game-of-thrones' } }, + { + character: 'Tyrion Lannister', + quote: 'A mind needs books like a sword needs a whetstone.' + } + ] + }) + + const { body } = await client.msearch({ + body: [ + { index: 'game-of-thrones' }, + { query: { match: { character: 'Daenerys' } } }, + + { index: 'game-of-thrones' }, + { query: { match: { character: 'Tyrion' } } } + ] + }) + + console.log(body.responses) +} + +run().catch(console.log) + +---- \ No newline at end of file diff --git a/docs/examples/scroll.asciidoc b/docs/examples/scroll.asciidoc index eadddef55..90ade2fb6 100644 --- a/docs/examples/scroll.asciidoc +++ b/docs/examples/scroll.asciidoc @@ -91,5 +91,84 @@ async function run () { } } +run().catch(console.log) +---- + +Another cool usage of the `scroll` API can be done with Node.js ≥ 10, by using async iteration! + +[source,js] +---- +'use strict' + +const { Client } = require('@elastic/elasticsearch') +const client = new Client({ node: 'http://localhost:9200' }) + +// Scroll utility +async function * scrollSearch (params) { + var response = await client.search(params) + + while (true) { + const sourceHits = response.body.hits.hits + + if (sourceHits.length === 0) { + break + } + + for (const hit of sourceHits) { + yield hit + } + + if (!response.body._scroll_id) { + break + } + + response = await client.scroll({ + scrollId: response.body._scroll_id, + scroll: params.scroll + }) + } +} + +async function run () { + await client.bulk({ + refresh: true, + body: [ + { index: { _index: 'game-of-thrones' } }, + { + character: 'Ned Stark', + quote: 'Winter is coming.' + }, + + { index: { _index: 'game-of-thrones' } }, + { + character: 'Daenerys Targaryen', + quote: 'I am the blood of the dragon.' + }, + + { index: { _index: 'game-of-thrones' } }, + { + character: 'Tyrion Lannister', + quote: 'A mind needs books like a sword needs a whetstone.' + } + ] + }) + + const params = { + index: 'game-of-thrones', + scroll: '30s', + size: 1, + _source: ['quote'], + body: { + query: { + match_all: {} + } + } + } + + for await (const hit of scrollSearch(params)) { + console.log(hit._source) + } +} + run().catch(console.log) ---- \ No newline at end of file diff --git a/docs/examples/suggest.asciidoc b/docs/examples/suggest.asciidoc new file mode 100644 index 000000000..d691d8344 --- /dev/null +++ b/docs/examples/suggest.asciidoc @@ -0,0 +1,59 @@ += Suggest + +The suggest feature suggests similar looking terms based on a provided text by using a suggester. _Parts of the suggest feature are still under development._ + +The suggest request part is defined alongside the query part in a `search` request. + +If the query part is left out, only suggestions are returned. + +[source,js] +---- +'use strict' + +const { Client } = require('@elastic/elasticsearch') +const client = new Client({ node: 'http://localhost:9200' }) + +async function run () { + await client.bulk({ + refresh: true, + body: [ + { index: { _index: 'game-of-thrones' } }, + { + character: 'Ned Stark', + quote: 'Winter is coming.' + }, + + { index: { _index: 'game-of-thrones' } }, + { + character: 'Daenerys Targaryen', + quote: 'I am the blood of the dragon.' + }, + + { index: { _index: 'game-of-thrones' } }, + { + character: 'Tyrion Lannister', + quote: 'A mind needs books like a sword needs a whetstone.' + } + ] + }) + + const { body } = await client.search({ + index: 'game-of-thrones', + body: { + query: { + match: { quote: 'witner' } + }, + suggest: { + gotsuggest: { + text: 'witner', + term: { field: 'quote' } + } + } + } + }) + + console.log(body) +} + +run().catch(console.log) + +---- \ No newline at end of file diff --git a/docs/examples/typescript.asciidoc b/docs/examples/typescript.asciidoc index 2591fdd7d..b3df28b74 100644 --- a/docs/examples/typescript.asciidoc +++ b/docs/examples/typescript.asciidoc @@ -8,28 +8,30 @@ NOTE: If you are using TypeScript you will be required to use _snake_case_ style ---- 'use strict' -import { Client, ApiResponse } from '@elastic/elasticsearch' +import { Client, ApiResponse, RequestParams } from '@elastic/elasticsearch' const client = new Client({ node: 'http://localhost:9200' }) async function run (): void { // Let's start by indexing some data - await client.index({ + const doc1: RequestParams.Index = { index: 'game-of-thrones', body: { character: 'Ned Stark', quote: 'Winter is coming.' } - }) + } + await client.index(doc1) - await client.index({ + const doc2: RequestParams.Index = { index: 'game-of-thrones', body: { character: 'Daenerys Targaryen', quote: 'I am the blood of the dragon.' } - }) + } + await client.index(doc2) - await client.index({ + const doc3: RequestParams.Index = { index: 'game-of-thrones', // here we are forcing an index refresh, // otherwise we will not get any result @@ -39,20 +41,22 @@ async function run (): void { character: 'Tyrion Lannister', quote: 'A mind needs books like a sword needs a whetstone.' } - }) + } + await client.index(doc3) // Let's search! - client - .search({ - index: 'game-of-thrones', - body: { - query: { - match: { - quote: 'winter' - } + const params: RequestParams.Search = { + index: 'game-of-thrones', + body: { + query: { + match: { + quote: 'winter' } } - }) + } + } + client + .search(params) .then((result: ApiResponse) => { console.og(result.body.hits.hits) })