搜索 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)
所有节点助手的文档 :