@ fastify/ 打开搜索
快捷插件 打开搜索 用于在服务器的每个部分共享相同的 ES 客户端. 在车盖下,官员 打开搜索 模块被使用.
安装
npm i @fastify/opensearch
使用量
添加到您的项目中 register 你们就完了!
该插件接受 相同选项 作为客户.
const fastify = require('fastify')()
fastify.register(require('@fastify/opensearch'), {
node: 'https://localhost:9200',
auth: {
username: 'admin',
password: 'admin'
}
})
fastify.get('/user', async function (req, reply) {
const { body } = await this.opensearch.search({
index: 'tweets',
body: {
query: { match: { text: req.query.q }}
}
})
return body.hits.hits
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
默认, @fastify/opensearch 将尝试在您开始快速化后立即打开集群,但在某些情况下,由于用户的权限,ping可能不会被支持. 如果您想要, 您可以使用 healthcheck 选项 :
fastify.register(require('@fastify/opensearch'), {
node: 'https://localhost:9200',
healthcheck: false,
auth: {
username: 'admin',
password: 'admin'
}
})
如果您需要连接到不同的集群, 您也可以通过一个 namespace 选项 :
const fastify = require('fastify')()
fastify.register(require('@fastify/opensearch'), {
node: 'https://localhost:9200',
namespace: 'cluster1',
auth: {
username: 'admin',
password: 'admin'
}
})
fastify.register(require('@fastify/opensearch'), {
node: 'http://localhost:9201',
namespace: 'cluster2',
auth: {
username: 'admin',
password: 'admin'
}
})
fastify.get('/user', async function (req, reply) {
const { body } = await this.opensearch.cluster1.search({
index: 'tweets',
body: {
query: { match: { text: req.query.q }}
}
})
return body.hits.hits
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
版本
默认使用 Opensearch 客户端的最新且最大的版本,请参见 兼容性 表格来理解嵌入式客户端是否正确.
如果不是,您可以通过 client 选项.
const fastify = require('fastify')()
const { Client } = require('@opensearch-project/opensearch')
fastify.register(require('@fastify/opensearch'), {
client: new Client({ node: 'https://localhost:9200' })
})
fastify.get('/user', async function (req, reply) {
const { body } = await this.opensearch.search({
index: 'tweets',
body: {
query: { match: { text: req.query.q }}
}
})
return body.hits.hits
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
})
许可证
许可证根据 麻省理工学院。 。 。 .