I have a documentation site built using Docusaurus.io, and am using Docsearch as my search in the navbar - this is working fine. However, I'd like to implement Algolia instantsearch on the landing page of the site as well. The code is working fine with an old index set up with instantsearch, but when I plug in the API key and ID of the new Docsearch index into the instantsearch config function, I get a stream of JSON objects that I can't make sense of.
This is the template I'm trying to use (which worked on my old index on a Jekyll site):
const hitTemplate = function(hit) {
console.log(hit);
let url = hit.url;
const title = hit.lvl1;
const content = hit._highlightResult.matchedWords;
return `
<div class="post-item">
<a class="post-link" href="${url}">
<h4>${title}</h4>
</a>
<div class="search-article-description">${content}</div>
<a href="${url}" class="read-more">Read More »</a>
</div>
`;
That results in a blank <div>
.
When no template is applied, my results come back as nested JSON objects.
What I'm trying to implement is an additional instantsearch that only appears on the homepage of the docs site. From what I understand, I can't have two input selectors in Docsearch, which is why I've been trying to use instantsearch. I need a template that works with the way the Docsearch API returns data.
After a week of trial and error and some very valuable help from the Docsearch devs via github, I came up with a solution that works for my documentation site, and am sharing it here in the hopes that other frustrated users of Docusaurus, Docsearch and Instantsearch find their way to this solution:
In
instantsearch.js
(found in website/static/js):And this is my pages/en/index.js:
Things to note:
filters: 'version:your-version'
or the instantsearch will return results for every version you have, leading to tons of repetition.instantsearch.js
is for displaying and hiding the<div>
where your search results appear depending on a keyup event in the search imputconst mainSearch = instantsearch({...
instead ofconst search
is that the default navbar Docsearch already uses thesearch
variable. If you have more than one search on the page, you'll need to do this.