All Projects → andrew-codes → Gatsby Plugin Elasticlunr Search

andrew-codes / Gatsby Plugin Elasticlunr Search

Licence: mit
Gatsby search plugin via elastic lunr client-side search index.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Gatsby Plugin Elasticlunr Search

Gatsby Plugin Algolia
A plugin to push to Algolia based on graphQl queries
Stars: ✭ 154 (+75%)
Mutual labels:  search, gatsby
Rw
Ryan Wiemer's Digital Portfolio
Stars: ✭ 87 (-1.14%)
Mutual labels:  gatsby
Gatsby Starter Typescript Rebass Netlifycms
My default Gatsby setup. Includes rich MDX support.
Stars: ✭ 79 (-10.23%)
Mutual labels:  gatsby
Gatsby Mdx Netlify Cms Starter
Gatsby-MDX with Netlify CMS. Support React components in your CMS editor!
Stars: ✭ 84 (-4.55%)
Mutual labels:  gatsby
Gatsby Advanced Starter
A high performance skeleton starter for GatsbyJS that focuses on SEO/Social features/development environment.
Stars: ✭ 1,224 (+1290.91%)
Mutual labels:  gatsby
Gatsby Bulma Quickstart
🚀 + ⚛️ A Quick Way to bootstrap your next Gatsby + Bulma site.
Stars: ✭ 84 (-4.55%)
Mutual labels:  gatsby
Gatsby Themes
Get high-quality and customizable Gatsby themes to quickly bootstrap your website! Choose from many professionally created and impressive designs with a wide variety of features and customization options.
Stars: ✭ 1,208 (+1272.73%)
Mutual labels:  gatsby
Gatsby Using Page Transitions
Gatsby example site using page transitions
Stars: ✭ 88 (+0%)
Mutual labels:  gatsby
Autocomplete
🔮 Fast and full-featured autocomplete library
Stars: ✭ 1,268 (+1340.91%)
Mutual labels:  search
C
Collection of various algorithms in mathematics, machine learning, computer science, physics, etc implemented in C for educational purposes.
Stars: ✭ 11,897 (+13419.32%)
Mutual labels:  search
Gatsby With Unstructured Data
A simple example of creating pages dynamically in Gatsby without using GraphQL.
Stars: ✭ 83 (-5.68%)
Mutual labels:  gatsby
Metasearch
Search aggregator for Slack, Google Docs, GitHub, and more 🔍
Stars: ✭ 81 (-7.95%)
Mutual labels:  search
Gatsby Plugin Google Fonts
Bring Google Fonts to Gatsby https://www.gatsbyjs.org/
Stars: ✭ 84 (-4.55%)
Mutual labels:  gatsby
Flash
Golang Keyword extraction/replacement Datastructure using Tries instead of regexes
Stars: ✭ 79 (-10.23%)
Mutual labels:  search
Yana
Powerful note-taking app with nested documents, full-text search, rich-text editor, code snippet editor and more
Stars: ✭ 87 (-1.14%)
Mutual labels:  search
Reactime
Chrome extension for improving and optimizing performance in React applications (Gatsby and Next.js compatible).
Stars: ✭ 1,219 (+1285.23%)
Mutual labels:  gatsby
Gatsby Starter Docz
📝 Gatsby starter with Docz and a blog for your documentation
Stars: ✭ 81 (-7.95%)
Mutual labels:  gatsby
Blog
Source for my blazing fast blog
Stars: ✭ 83 (-5.68%)
Mutual labels:  gatsby
Gatsby Starter Procyon
An opinionated Gatsby starter designed for trash-eating pandas.
Stars: ✭ 88 (+0%)
Mutual labels:  gatsby
Gatsby Theme Try Ghost
A Gatsby theme to build flaring fast blogs from headless Ghost CMS
Stars: ✭ 88 (+0%)
Mutual labels:  gatsby

Maintainability Codacy Badge

Search Plugin for Gatsby

This plugin enables search integration via elastic lunr. Content is indexed and then made available via graphql to rehydrate into an elasticlunr index. From there, queries can be made against this index to retrieve pages by their ID.

Getting Started

Install the plugin via npm install -D @andrew-codes/gatsby-plugin-elasticlunr-search. See the demo site repo for more specific implementation details.

Next, update your gatsby-config.js file to utilize the plugin.

Setup in gatsby-config

module.exports = {
    plugins: [
        {
            resolve: `@andrew-codes/gatsby-plugin-elasticlunr-search`,
            options: {
                // Fields to index
                fields: [
                    'title',
                    'keywords',
                ],
                // 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,
                        keywords: node => node.frontmatter.keywords,
                    },
                },
            },
        },
    ],
};

Consuming in Your Site

The serialized search index will be available via graphql. Once queried, a component can create a new elastic lunr index with the value retrieved from the graphql query. Search queries can be made against the hydrated search index. The results is an array of document IDs. The index can return the full document given a document ID

import React, {Component} from 'react';
import {Index} from 'elasticlunr';

// Graphql query used to retrieve the serialized search index.
export const query = graphql`query
SearchIndexExampleQuery {
    siteSearchIndex {
      index
    }
}`;

// 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>
                            {page.title}: {page.keywords.join(`,`)}
                        </li>
                    ))}
                </ul>
            </div>
        );
    }

    getOrCreateIndex = () => this.index
        ? this.index
        // Create an elastic lunr index and hydrate with graphql query results
        : Index.load(this.props.data.siteSearchIndex.index);

    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)),
        });
    }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].