@fastify/opensearch

中文文档
2.0.2发布于 2026/10/08MITTypeScriptCommonJS构建来源已验证近 30 天下载 3115

快速打开搜索插件

中文使用文档
文档目录4 节
  1. 安装
  2. 使用量
  3. 版本
  4. 许可证

@ fastify/ 打开搜索

CI NPM version neostandard javascript style

快捷插件 打开搜索 用于在服务器的每个部分共享相同的 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
})

许可证

许可证根据 麻省理工学院。 。 。 .

版本与发布记录

@fastify/opensearch 共发布 3 个版本,首次发布于 2024/05/02;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.2latest2026/10/081 个月前
2.0.02025/04/111.6 年前
1.0.02024/05/022 年前

关于 @fastify/opensearch 的常见问题

@fastify/opensearch 是什么?

快速打开搜索插件

如何安装 @fastify/opensearch?

在项目目录运行 npm install @fastify/opensearch,或使用 pnpm add / yarn add / bun add。

@fastify/opensearch 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fastify/opensearch 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@fastify/opensearch 可以商用吗?

@fastify/opensearch 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fastify/opensearch 的下载量有多少?

根据 npm 官方下载统计,@fastify/opensearch 在 2026/10/15 – 2026/11/13 这 30 天内被下载 3,115 次,日均约 104 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastify/opensearch 还在维护吗?

最新版本 2.0.2 发布于 2026/10/08(1 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 1 个;首次发布于 2024/05/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。