@ fastify/ 弹性搜索
快捷插件 弹性搜索 用于在服务器的每个部分共享相同的 ES 客户端. 在车盖下,官员 弹性搜索 模块被使用.
安装
npm i @fastify/elasticsearch @elastic/elasticsearch
兼容性
| 插件版本 | 快速化版本 |
|---|---|
>=4.x | ^5.x |
^3.x | ^4.x |
^2.x | ^3.x |
^1.x | ^2.x |
^1.x | ^1.x |
请注意,如果一个快取版本没有支持,那么该插件的相应版本也一样 上表列出. 见 快速化 LTS 政策 更多细节.
使用量
添加到您的项目中 register 你们就完了!
该插件接受 相同选项 作为客户.
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
})
默认, @fastify/elasticsearch 将尝试在您开始快速化后立即打开集群,但在某些情况下,由于用户的权限,ping可能不会被支持. 如果您想要, 您可以使用 healthcheck 选项 :
fastify.register(require('@fastify/elasticsearch'), {
node: 'http://localhost:9200',
healthcheck: false
})
如果您需要连接到不同的集群, 您也可以通过一个 namespace 选项 :
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
})
版本
@fastify/elasticsearch 支持弹性搜索客户端版本8和9.
默认使用 Elasticsearch 客户端的最新和最伟大的版本,请参见 兼容性 表格来理解嵌入式客户端是否正确.
如果不是,您可以通过 client 选项.
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
})
许可证
许可证根据 麻省理工学院。 。 。 .