Added examples
This commit is contained in:
51
docs/examples/msearch.asciidoc
Normal file
51
docs/examples/msearch.asciidoc
Normal file
@ -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)
|
||||||
|
|
||||||
|
----
|
||||||
@ -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)
|
run().catch(console.log)
|
||||||
----
|
----
|
||||||
59
docs/examples/suggest.asciidoc
Normal file
59
docs/examples/suggest.asciidoc
Normal file
@ -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)
|
||||||
|
|
||||||
|
----
|
||||||
@ -8,28 +8,30 @@ NOTE: If you are using TypeScript you will be required to use _snake_case_ style
|
|||||||
----
|
----
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
import { Client, ApiResponse } from '@elastic/elasticsearch'
|
import { Client, ApiResponse, RequestParams } from '@elastic/elasticsearch'
|
||||||
const client = new Client({ node: 'http://localhost:9200' })
|
const client = new Client({ node: 'http://localhost:9200' })
|
||||||
|
|
||||||
async function run (): void {
|
async function run (): void {
|
||||||
// Let's start by indexing some data
|
// Let's start by indexing some data
|
||||||
await client.index({
|
const doc1: RequestParams.Index = {
|
||||||
index: 'game-of-thrones',
|
index: 'game-of-thrones',
|
||||||
body: {
|
body: {
|
||||||
character: 'Ned Stark',
|
character: 'Ned Stark',
|
||||||
quote: 'Winter is coming.'
|
quote: 'Winter is coming.'
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
await client.index(doc1)
|
||||||
|
|
||||||
await client.index({
|
const doc2: RequestParams.Index = {
|
||||||
index: 'game-of-thrones',
|
index: 'game-of-thrones',
|
||||||
body: {
|
body: {
|
||||||
character: 'Daenerys Targaryen',
|
character: 'Daenerys Targaryen',
|
||||||
quote: 'I am the blood of the dragon.'
|
quote: 'I am the blood of the dragon.'
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
await client.index(doc2)
|
||||||
|
|
||||||
await client.index({
|
const doc3: RequestParams.Index = {
|
||||||
index: 'game-of-thrones',
|
index: 'game-of-thrones',
|
||||||
// here we are forcing an index refresh,
|
// here we are forcing an index refresh,
|
||||||
// otherwise we will not get any result
|
// otherwise we will not get any result
|
||||||
@ -39,20 +41,22 @@ async function run (): void {
|
|||||||
character: 'Tyrion Lannister',
|
character: 'Tyrion Lannister',
|
||||||
quote: 'A mind needs books like a sword needs a whetstone.'
|
quote: 'A mind needs books like a sword needs a whetstone.'
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
await client.index(doc3)
|
||||||
|
|
||||||
// Let's search!
|
// Let's search!
|
||||||
client
|
const params: RequestParams.Search = {
|
||||||
.search({
|
index: 'game-of-thrones',
|
||||||
index: 'game-of-thrones',
|
body: {
|
||||||
body: {
|
query: {
|
||||||
query: {
|
match: {
|
||||||
match: {
|
quote: 'winter'
|
||||||
quote: 'winter'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
client
|
||||||
|
.search(params)
|
||||||
.then((result: ApiResponse) => {
|
.then((result: ApiResponse) => {
|
||||||
console.og(result.body.hits.hits)
|
console.og(result.body.hits.hits)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user