盖茨比内容搜索

Gatsby Contentful search

我有一个使用 Contentful 的 Gatsby 项目。很好 - 例如,我可以检索博客并显示它们。

但是如果我想提供一个搜索工具来搜索可能的 1000 多个帖子并显示相关结果 - 我该怎么做?

我什至不确定如何启动它 - 大概 "result page" 会是一条不同的路由,因为当前路由已经解析为静态文件 - 但我不确定我将如何路由它无论如何,当 Gatsby 已经有路由时。

有人有这方面的入门模板吗?有一个就好了!

谢谢

解决这个问题的方法很少;

  1. 使用像 elesticlunr 这样的库进行离线搜索,但它需要您在构建时创建索引。

还好可以使用gatsby-plugin-elasticlunr-search插件实现

在你的gatsby-config.js中:

module.exports = {
    plugins: [
        {
            resolve: `@andrew-codes/gatsby-plugin-elasticlunr-search`,
            options: {
                // Fields to index
                fields: [
                    'title',
                    'description',
                ],
                // 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
                    ContentProduct: {
                        title: node => node.title,
                        description: node => node.description,
                    },
                },
            },
        },
    ],
};
  1. 如果您的网站类型是在线文档,您可以使用 Algolia docs 功能。

Agolia 将抓取 DOM 并自动构建搜索索引,您剩下要做的就是:构建一个界面来呈现 search results.

  1. 使用 Algolia 并在构建时收集搜索索引并将其上传到 Algolia 并猜测是什么:有 plugin