@fastify/elasticsearch
TypeScript icon, indicating that this package has built-in type declarations

3.1.0 • Public • Published

@fastify/elasticsearch

CI NPM version js-standard-style

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.

Install

npm i @fastify/elasticsearch

Usage

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
})

Versioning

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
})

License

Licensed under MIT.

Package Sidebar

Install

npm i @fastify/elasticsearch

Weekly Downloads

423

Version

3.1.0

License

MIT

Unpacked Size

13.6 kB

Total Files

12

Last publish

Collaborators

  • gurgunday
  • metcoder95
  • galvez
  • simenb
  • coopflow
  • simoneb
  • rafaelgss
  • starptech
  • delvedor
  • matteo.collina
  • allevo
  • jsumners
  • zekth
  • eomm
  • fox1t
  • airhorns
  • kibertoad
  • climba03003
  • is2ei
  • fdawgs