Eleventy: Using Algolia for search

Created on 29 Dec 2017  路  13Comments  路  Source: 11ty/eleventy

I would be great if we could use Algolia for search.

The easy part is the user facing search interface, because it doesn't depend on eleventy.

The hard part is indexing. Here's an overview of indexing into Algolia.

enhancement

Most helpful comment

@zachleat @nhoizey I work at Algolia and would be happy to assist you on this.
Let me know once you've evaluated it and if you give a green light, I'd be happy to open a PR.

P.S. happy holidays 馃巹

All 13 comments

@zachleat @nhoizey I work at Algolia and would be happy to assist you on this.
Let me know once you've evaluated it and if you give a green light, I'd be happy to open a PR.

P.S. happy holidays 馃巹

Moving this comment here since it鈥檚 more relevant to this ticket:

Also can be used for:
https://twitter.com/ChrisFerdinandi/status/949022607390707712
JSON file mapping all of the content for RSS/Search

cc @cferdinandi

IMO this could easily be done if we could output different file formats than .html.

Especially with EJS. Just create a special collection w/ the fields you want, and then put this in your template:

eleventyConfig.js:

module.exports = (eleventyConfig) => {
  eleventyConfig.addCollection("algolia", function(collection) {
    var algoliaCollection = collection.getAll().map(hit => {
        return {
          title: hit.title,
          // any other fields you want...
        }
    })
  });
}

index.json:

templateEngineOverride: ejs
---
<%= JSON.stringify(collections.algolia) -%>

Interesting @chrisdmacrae! You can output any file/extension with permalink, no?

Rather, use the file name index.ejs and use permalink: index.json in your front matter.

Amazing. Phenomenal. Eleventy is great.

In this scenario, you could add a plugin that utilizes the algoliasearch client to push the data from the JSON file(s).

@JonasBa, how does that sound?

Related: https://github.com/11ty/eleventy/pull/107 brings in:

https://github.com/11ty/eleventy/blob/master/docs/permalinks.md#custom-file-formats

This is basically all that鈥檚 needed for this issue, but I鈥檒l leave it open until we have something concrete in the docs.

This repository is now using lodash style issue management for enhancements (see https://twitter.com/samselikoff/status/991395669016436736)

This means enhancement issues will now be closed instead of leaving them open. The enhancement backlog can be found here: https://github.com/11ty/eleventy/issues?utf8=%E2%9C%93&q=label%3Aneeds-votes+sort%3Areactions-%2B1-desc+

Slight error here

<%= JSON.stringify(collections.algolia) -%>

Should be

<%- JSON.stringify(collections.algolia) -%>

This lets EJS creates an unescaped JSON file (which is what we're after).

@chrisdmacrae I realize this issue is pretty old but I was curious to see how you found this approach. Were you able to get it all working with Algolia?

I was also curious to see if you're just sending them the entire JSON every time, or if you're doing incremental updates and trying to only update posts that have new information.

I did implement a local search on the 11ty.io docs, might be of interest to people on this issue. Might expose this as a plugin later.

Data file here: https://github.com/11ty/11ty.io/blob/master/docs/searchData.njk
https://twitter.com/eleven_ty/status/1197550926636158982

Yes please :-)
Would also be interesting to see, how to optimise the rank on large collections of content.

Is there a way to know which collection the hit came from??? I need to know that.

Was this page helpful?
0 / 5 - 0 ratings