Files
elasticsearch-js/docs/examples/search.asciidoc
github-actions[bot] 3e6b02b504 [Backport 8.1] Update docs for v8 (#1620)
Co-authored-by: Tomas Della Vedova <delvedor@users.noreply.github.com>
2022-02-11 10:23:54 +01:00

61 lines
1.4 KiB
Plaintext

[[search_examples]]
=== 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',
document: {
character: 'Ned Stark',
quote: 'Winter is coming.'
}
})
await client.index({
index: 'game-of-thrones',
document: {
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,
document: {
character: 'Tyrion Lannister',
quote: 'A mind needs books like a sword needs a whetstone.'
}
})
// Let's search!
const result = await client.search({
index: 'game-of-thrones',
query: {
match: {
quote: 'winter'
}
}
})
console.log(result.hits.hits)
}
run().catch(console.log)
----