@gatsby-contrib/gatsby-plugin-elasticlunr-search

中文文档
3.0.2发布于 2021/03/16MITCommonJS近 30 天下载 3569

搜索 Gatsby; 通过弹性调取器执行 .

中文使用文档
文档目录4 节
  1. 设置在 gatsby-config
  2. 正在您的站点中运行
  3. 部分搜索
  4. 优化以嵌入式节点处理数据模型

搜索 Gatsby 的插件

此插件允许通过弹性 lunr 进行搜索集成 。 内容编制索引 然后通过图克l提供再水合物 elasticlunr 指数。 从 中,可以在此索引中查询,以获取页面的ID.

这是叉子 gatsby-plugin-弹性搜索器 用于 gatsby- v2 的插件.

开始

通过安装插件 npm install --save @gatsby-contrib/gatsby-plugin-elasticlunr-search。 。 。 .

见 实例 站点 代码 具体执行细节.

下一个,更新您的 gatsby-config.js 用于使用插件的文件.

设置在 gatsby-config

以下是一个网站的例子,它使用平分创建页面,其中你 您要允许搜索功能 title 财务报告和审定财务报表 tags 前题条目.

gatsby-config.js

module.exports = {
  plugins: [
    {
      resolve: `@gatsby-contrib/gatsby-plugin-elasticlunr-search`,
      options: {
        // Fields to index
        fields: [`title`, `tags`],
        // How to resolve each field`s value for a supported node type
        resolvers: {
          // For any node of type MarkdownRemark, list how to resolve the fields` values
          MarkdownRemark: {
            title: node => node.frontmatter.title,
            tags: node => node.frontmatter.tags,
            path: node => node.frontmatter.path,
          },
        },
        // Optional filter to limit indexed nodes
        filter: (node, getNode) => node.frontmatter.tags !== "exempt",
      },
    },
  ],
}

正在您的站点中运行

序列式搜索索引将通过图ql提供。 一经询问,a 组件可以创建新的弹性调取器索引,其值来自 图克勒查询. 可以根据水合物搜索索引进行搜索查询. 这个 结果就是一系列文档ID。 索引可以返回完整文档 提供了文档编号.

在 Gatsby- v2 中,可以使用图克l 查询在组件内部使用 StaticQuery。 。 。 .

假设你想包括 Search a 内组件 Header 组件. 当然,你也可以查询 siteSearchIndex 从 layout.js 组件,并将其作为道具传递给任何需要的组件。 )

首先,查询数据与 StaticQuery 内部 Header 组件,并通过 作为道具 Search 组件.

components/header.js

import React from "react"
import { StaticQuery, graphql } from "gatsby"

import Search from "./search"

const Header = () => (
  <StaticQuery
    query={graphql`
      query SearchIndexQuery {
        siteSearchIndex {
          index
        }
      }
    `}
    render={data => (
      <header>
        ... header stuff...
        <Search searchIndex={data.siteSearchIndex.index} />
      </header>
    )}
  />
)

export default Header

然后使用 searchIndex 在你体内 Search 组件.

components/search.js

import React, { Component } from "react"
import { Index } from "elasticlunr"
import { Link } from "gatsby"

// Search component
export default class Search extends Component {
  constructor(props) {
    super(props)
    this.state = {
      query: ``,
      results: [],
    }
  }

  render() {
    return (
      <div>
        <input type="text" value={this.state.query} onChange={this.search} />
        <ul>
          {this.state.results.map(page => (
            <li key={page.id}>
              <Link to={"/" + page.path}>{page.title}</Link>
              {": " + page.tags.join(`,`)}
            </li>
          ))}
        </ul>
      </div>
    )
  }
  getOrCreateIndex = () =>
    this.index
      ? this.index
      : // Create an elastic lunr index and hydrate with graphql query results
        Index.load(this.props.searchIndex)

  search = evt => {
    const query = evt.target.value
    this.index = this.getOrCreateIndex()
    this.setState({
      query,
      // Query the index with search string to get an [] of IDs
      results: this.index
        .search(query, {})
        // Map over each ID and return the full document
        .map(({ ref }) => this.index.documentStore.getDoc(ref)),
    })
  }
}

部分搜索

如果您想要搜索包含部分匹配, 例如您有 下列数据:

sku: ["ab21345", "ab98765", "abcdef12"]

并想要一个搜索 "页:1" 返回所有这些数据,然后你可以 仅包含 { expand: true } 作为第二个参数 this.index.search() 设置时 results 状态.

以上述实施为例,调整 search 函数 Search 由下列部分组成:

search = evt => {
  const query = evt.target.value
  this.index = this.getOrCreateIndex()
  this.setState({
    query,
    // Query the index with search string to get an [] of IDs
    results: this.index
      .search(query, { expand: true }) // Accept partial matches
      // Map over each ID and return the full document
      .map(({ ref }) => this.index.documentStore.getDoc(ref)),
  })
}

优化以嵌入式节点处理数据模型

时有数据模型已经筑出节点. 示例 解析器配置在 gatsby-config.js数字 :

resolvers : {
  // For any node of BlogPost, list how to resolve the fields' values
  BlogPost : {
    title         : node => node.title,
    featuredImage : node => node.featuredImage___NODE // featuredImage is of type Asset below and is an id reference to Asset
  },

  // For any node of type Asset, this is how BlogPost featuredImage is resolved
  Asset : {
    fileUrl : node => node.file && node.file.url
  }
}

以上解析器配置的问题在于它包括所有 资产模式 elasticlunr 指数,有可能膨胀 elasticlunr 索引并导致大捆绑大小和更慢的页面负载时间.

解决方案是利用第二个传送到每个字段的参数 调用解析器函数 getNode。 。 。 . getNode 是同一函数,由 开始 设置在 GraphQLNodeType 上的字段 节点 api 方法, 当使用数据模型 id 调用时, 它会返回一个节点 与所有的数据。 以上例子 BlogPost 有巢穴的模型 featuredImage 类型属性 Asset 然后变成:

resolvers : {
  // For any node of BlogPost, list how to resolve the fields' values
  BlogPost : {
    title         : node => node.title,
    featuredImage : (node, getNode) => getNode(node.featuredImage___NODE) // featuredImage is of type Asset and is now the Asset model itself
  }
}

现在你可以使用 featuredImage 数据 BlogPost 模式,但不包括 全部 Asset 模式 elasticlunr 指数 (更多见PR #3) 详细情况)。 。 。 .

您现在也可以用 getNodesByType 财务报告和审定财务报表 getNodes 因此,节点解析的全部签名是:

(node, getNode, getNodesByType, getNodes)

所有节点助手的文档 :

版本与发布记录

@gatsby-contrib/gatsby-plugin-elasticlunr-search 共发布 14 个版本,首次发布于 2018/10/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.2latest2021/03/165 年前
3.0.12021/03/165 年前
3.0.02021/03/165 年前
2.4.22020/06/116 年前
2.4.12020/06/116 年前
2.4.02020/06/116 年前

关于 @gatsby-contrib/gatsby-plugin-elasticlunr-search 的常见问题

@gatsby-contrib/gatsby-plugin-elasticlunr-search 是什么?

搜索 Gatsby; 通过弹性调取器执行 .

如何安装 @gatsby-contrib/gatsby-plugin-elasticlunr-search?

在项目目录运行 npm install @gatsby-contrib/gatsby-plugin-elasticlunr-search,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:gatsby(>3.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@gatsby-contrib/gatsby-plugin-elasticlunr-search 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/gatsby-contrib__gatsby-plugin-elasticlunr-search 是否存在。

@gatsby-contrib/gatsby-plugin-elasticlunr-search 支持 ESM 还是 CommonJS?

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

@gatsby-contrib/gatsby-plugin-elasticlunr-search 可以商用吗?

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

@gatsby-contrib/gatsby-plugin-elasticlunr-search 的下载量有多少?

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

@gatsby-contrib/gatsby-plugin-elasticlunr-search 还在维护吗?

最新版本 3.0.2 发布于 2021/03/16(5 年前);至今共发布 14 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/10/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。