Added examples
This commit is contained in:
58
docs/examples/bulk.asciidoc
Normal file
58
docs/examples/bulk.asciidoc
Normal file
@ -0,0 +1,58 @@
|
||||
= Bulk
|
||||
|
||||
The `bulk` API makes it possible to perform many index/delete operations in a single API call. +
|
||||
This can greatly increase the indexing speed.
|
||||
|
||||
[source,js]
|
||||
----
|
||||
'use strict'
|
||||
|
||||
const { Client } = require('@elastic/elasticsearch')
|
||||
const client = new Client({ node: 'http://localhost:9200' })
|
||||
|
||||
async function run () {
|
||||
await client.bulk({
|
||||
// here we are forcing an index refresh,
|
||||
// otherwise we will not get any result
|
||||
// in the consequent search
|
||||
refresh: true,
|
||||
body: [
|
||||
// operation to perform
|
||||
{ index: { _index: 'game-of-thrones' } },
|
||||
// the document to index
|
||||
{
|
||||
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.'
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// Let's search!
|
||||
const { body } = await client.search({
|
||||
index: 'game-of-thrones',
|
||||
body: {
|
||||
query: {
|
||||
match: {
|
||||
quote: 'winter'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
console.log(body.hits.hits)
|
||||
}
|
||||
|
||||
run().catch(console.log)
|
||||
----
|
||||
33
docs/examples/exists.asciidoc
Normal file
33
docs/examples/exists.asciidoc
Normal file
@ -0,0 +1,33 @@
|
||||
= Exists
|
||||
|
||||
Check that the document `/game-of-thrones/1` exists.
|
||||
|
||||
NOTE: Since this API uses the `HEAD` method, the body value will be boolean.
|
||||
|
||||
[source,js]
|
||||
---------
|
||||
'use strict'
|
||||
|
||||
const { Client } = require('@elastic/elasticsearch')
|
||||
const client = new Client({ node: 'http://localhost:9200' })
|
||||
|
||||
async function run () {
|
||||
await client.index({
|
||||
index: 'game-of-thrones',
|
||||
id: '1',
|
||||
body: {
|
||||
character: 'Ned Stark',
|
||||
quote: 'Winter is coming.'
|
||||
}
|
||||
})
|
||||
|
||||
const { body } = await client.exists({
|
||||
index: 'game-of-thrones',
|
||||
id: 1
|
||||
})
|
||||
|
||||
console.log(body) // true
|
||||
}
|
||||
|
||||
run().catch(console.log)
|
||||
---------
|
||||
32
docs/examples/get.asciidoc
Normal file
32
docs/examples/get.asciidoc
Normal file
@ -0,0 +1,32 @@
|
||||
= Get
|
||||
|
||||
The get API allows to get a typed JSON document from the index based on its id. +
|
||||
The following example gets a JSON document from an index called `game-of-thrones`, under a type called `_doc`, with id valued `'1'`.
|
||||
|
||||
[source,js]
|
||||
---------
|
||||
'use strict'
|
||||
|
||||
const { Client } = require('@elastic/elasticsearch')
|
||||
const client = new Client({ node: 'http://localhost:9200' })
|
||||
|
||||
async function run () {
|
||||
await client.index({
|
||||
index: 'game-of-thrones',
|
||||
id: '1',
|
||||
body: {
|
||||
character: 'Ned Stark',
|
||||
quote: 'Winter is coming.'
|
||||
}
|
||||
})
|
||||
|
||||
const { body } = await client.get({
|
||||
index: 'game-of-thrones',
|
||||
id: '1'
|
||||
})
|
||||
|
||||
console.log(body)
|
||||
}
|
||||
|
||||
run().catch(console.log)
|
||||
---------
|
||||
95
docs/examples/scroll.asciidoc
Normal file
95
docs/examples/scroll.asciidoc
Normal file
@ -0,0 +1,95 @@
|
||||
= Scroll
|
||||
|
||||
While a search request returns a single “page” of results, the scroll API can be used to retrieve large numbers of results (or even all results) from a single search request, in much the same way as you would use a cursor on a traditional database.
|
||||
|
||||
Scrolling is not intended for real time user requests, but rather for processing large amounts of data, e.g. in order to reindex the contents of one index into a new index with a different configuration.
|
||||
|
||||
NOTE: The results that are returned from a scroll request reflect the state of the index at the time that the initial search request was made, like a snapshot in time. Subsequent changes to documents (index, update or delete) will only affect later search requests.
|
||||
|
||||
In order to use scrolling, the initial search request should specify the scroll parameter in the query string, which tells Elasticsearch how long it should keep the “search context” alive.
|
||||
|
||||
[source,js]
|
||||
----
|
||||
'use strict'
|
||||
|
||||
const { Client } = require('@elastic/elasticsearch')
|
||||
const client = new Client({ node: 'http://localhost:9200' })
|
||||
|
||||
async function run () {
|
||||
const allQuotes = []
|
||||
const responseQueue = []
|
||||
|
||||
// Let's index some data!
|
||||
await client.bulk({
|
||||
// here we are forcing an index refresh,
|
||||
// otherwise we will not get any result
|
||||
// in the consequent search
|
||||
refresh: true,
|
||||
body: [
|
||||
// operation to perform
|
||||
{ index: { _index: 'game-of-thrones' } },
|
||||
// the document to index
|
||||
{
|
||||
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.'
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// start things off by searching, setting a scroll timeout, and pushing
|
||||
// our first response into the queue to be processed
|
||||
const response = await client.search({
|
||||
index: 'game-of-thrones',
|
||||
// keep the search results "scrollable" for 30 seconds
|
||||
scroll: '30s',
|
||||
// for the sake of this example, we will get only one result per search
|
||||
size: 1,
|
||||
// filter the source to only include the quote field
|
||||
_source: ['quote'],
|
||||
body: {
|
||||
query: {
|
||||
match_all: {}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
responseQueue.push(response)
|
||||
|
||||
while (responseQueue.length) {
|
||||
const { body } = responseQueue.shift()
|
||||
|
||||
// collect the titles from this response
|
||||
body.hits.hits.forEach(function (hit) {
|
||||
allQuotes.push(hit._source.quote)
|
||||
})
|
||||
|
||||
// check to see if we have collected all of the quotes
|
||||
if (body.hits.total.value === allQuotes.length) {
|
||||
console.log('Every quote', allQuotes)
|
||||
break
|
||||
}
|
||||
|
||||
// get the next response if there are more quotes to fetch
|
||||
responseQueue.push(
|
||||
await client.scroll({
|
||||
scrollId: body._scroll_id,
|
||||
scroll: '30s'
|
||||
})
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
run().catch(console.log)
|
||||
----
|
||||
59
docs/examples/search.asciidoc
Normal file
59
docs/examples/search.asciidoc
Normal file
@ -0,0 +1,59 @@
|
||||
= Search
|
||||
|
||||
The `search` API allows you to execute a search query and get back search hits that match the query. +
|
||||
The query can either be provided using a simple https://www.elastic.co/guide/en/elasticsearch/reference/6.6/search-uri-request.html[query string as a parameter], or using a https://www.elastic.co/guide/en/elasticsearch/reference/6.6/search-request-body.html[request body].
|
||||
|
||||
[source,js]
|
||||
----
|
||||
'use strict'
|
||||
|
||||
const { Client } = require('@elastic/elasticsearch')
|
||||
const client = new Client({ node: 'http://localhost:9200' })
|
||||
|
||||
async function run () {
|
||||
// Let's start by indexing some data
|
||||
await client.index({
|
||||
index: 'game-of-thrones',
|
||||
body: {
|
||||
character: 'Ned Stark',
|
||||
quote: 'Winter is coming.'
|
||||
}
|
||||
})
|
||||
|
||||
await client.index({
|
||||
index: 'game-of-thrones',
|
||||
body: {
|
||||
character: 'Daenerys Targaryen',
|
||||
quote: 'I am the blood of the dragon.'
|
||||
}
|
||||
})
|
||||
|
||||
await client.index({
|
||||
index: 'game-of-thrones',
|
||||
// here we are forcing an index refresh,
|
||||
// otherwise we will not get any result
|
||||
// in the consequent search
|
||||
refresh: true,
|
||||
body: {
|
||||
character: 'Tyrion Lannister',
|
||||
quote: 'A mind needs books like a sword needs a whetstone.'
|
||||
}
|
||||
})
|
||||
|
||||
// Let's search!
|
||||
const { body } = await client.search({
|
||||
index: 'game-of-thrones',
|
||||
body: {
|
||||
query: {
|
||||
match: {
|
||||
quote: 'winter'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
console.log(body.hits.hits)
|
||||
}
|
||||
|
||||
run().catch(console.log)
|
||||
----
|
||||
65
docs/examples/typescript.asciidoc
Normal file
65
docs/examples/typescript.asciidoc
Normal file
@ -0,0 +1,65 @@
|
||||
= Typescript
|
||||
|
||||
The client offers a first-class support for TypeScript, since it ships the type definitions for every exposed API.
|
||||
|
||||
NOTE: If you are using TypeScript you will be required to use _snake_case_ style to define the API parameters instead of _camelCase_.
|
||||
|
||||
[source,ts]
|
||||
----
|
||||
'use strict'
|
||||
|
||||
import { Client, ApiResponse } 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({
|
||||
index: 'game-of-thrones',
|
||||
body: {
|
||||
character: 'Ned Stark',
|
||||
quote: 'Winter is coming.'
|
||||
}
|
||||
})
|
||||
|
||||
await client.index({
|
||||
index: 'game-of-thrones',
|
||||
body: {
|
||||
character: 'Daenerys Targaryen',
|
||||
quote: 'I am the blood of the dragon.'
|
||||
}
|
||||
})
|
||||
|
||||
await client.index({
|
||||
index: 'game-of-thrones',
|
||||
// here we are forcing an index refresh,
|
||||
// otherwise we will not get any result
|
||||
// in the consequent search
|
||||
refresh: true,
|
||||
body: {
|
||||
character: 'Tyrion Lannister',
|
||||
quote: 'A mind needs books like a sword needs a whetstone.'
|
||||
}
|
||||
})
|
||||
|
||||
// Let's search!
|
||||
client
|
||||
.search({
|
||||
index: 'game-of-thrones',
|
||||
body: {
|
||||
query: {
|
||||
match: {
|
||||
quote: 'winter'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.then((result: ApiResponse) => {
|
||||
console.og(result.body.hits.hits)
|
||||
})
|
||||
.catch((err: Error) => {
|
||||
console.log(err)
|
||||
})
|
||||
}
|
||||
|
||||
run()
|
||||
----
|
||||
Reference in New Issue
Block a user