
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
@fastify/elasticsearch
Advanced tools
Fastify plugin for Elasticsearch for sharing the same ES client in every part of your server. Under the hood, the official elasticsearch module is used.
npm i @fastify/elasticsearch
Plugin version | Fastify version |
---|---|
^4.x | ^5.x |
^3.x | ^4.x |
^2.x | ^3.x |
^1.x | ^2.x |
^1.x | ^1.x |
Please note that if a Fastify version is out of support, then so are the corresponding versions of this plugin in the table above. See Fastify's LTS policy for more details.
Add it to your project with register
and you are done!
The plugin accepts the same options as the client.
const fastify = require('fastify')()
fastify.register(require('@fastify/elasticsearch'), { node: 'http://localhost:9200' })
fastify.get('/user', async function (req, reply) {
const { body } = await this.elastic.search({
index: 'tweets',
body: {
query: { match: { text: req.query.q }}
}
})
return body.hits.hits
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
By default, @fastify/elasticsearch
will try to ping the cluster as soon as you start Fastify, but in some cases pinging may not be supported due to the user permissions. If you want, you can disable the initial ping with the healthcheck
option:
fastify.register(require('@fastify/elasticsearch'), {
node: 'http://localhost:9200',
healthcheck: false
})
If you need to connect to different clusters, you can also pass a namespace
option:
const fastify = require('fastify')()
fastify.register(require('@fastify/elasticsearch'), {
node: 'http://localhost:9200',
namespace: 'cluster1'
})
fastify.register(require('@fastify/elasticsearch'), {
node: 'http://localhost:9201',
namespace: 'cluster2'
})
fastify.get('/user', async function (req, reply) {
const { body } = await this.elastic.cluster1.search({
index: 'tweets',
body: {
query: { match: { text: req.query.q }}
}
})
return body.hits.hits
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
By default the latest and greatest version of the Elasticsearch client is used, see the compatibility table to understand if the embedded client is correct for you.
If it is not, you can pass a custom client via the client
option.
const fastify = require('fastify')()
const { Client } = require('@elastic/elasticsearch')
fastify.register(require('@fastify/elasticsearch'), {
client: new Client({ node: 'http://localhost:9200' })
})
fastify.get('/user', async function (req, reply) {
const { body } = await this.elastic.search({
index: 'tweets',
body: {
query: { match: { text: req.query.q }}
}
})
return body.hits.hits
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
Licensed under MIT.
FAQs
Fastify plugin for elastic search
The npm package @fastify/elasticsearch receives a total of 1,120 weekly downloads. As such, @fastify/elasticsearch popularity was classified as popular.
We found that @fastify/elasticsearch demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.