MeiliSearch | Documentation | Slack | Roadmap | Website | FAQ
docs-searchbar.js is a front-end SDK for MeiliSearch to handle your search bar documentation.
MeiliSearch is an open-source search engine. Discover what MeiliSearch is!
💡 If you use VuePress for your website, you should check out our VuePress plugin for MeiliSearch.
The goal of this library is to provide a front-end search bar into your own documentation. To make that possible, you need to gather your website content in advance, and index it in a MeiliSearch instance.
Luckily, we provide all the tools that you need, and can help you through the whole process, if you follow this guide 🚀
However, as a first introduction, you might only want to test this library without connecting it to your website.
You can do it by running the playground provided in this repository:
yarn install
yarn serve
The data comes from MeiliSearch documentation.
Type create an indxe
to live the MeiliSearch experience with the typo tolerance.
We recommend using the docs-scraper
tool to scrape your website, but this is not mandatory.
If you already have your own scraper but you still want to use MeiliSearch and docs-searchbar.js
, check out this discussion.
<!DOCTYPE html>
<html>
<head>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/docs-searchbar.js@{version}/dist/cdn/docs-searchbar.min.css"
/>
</head>
<body>
<input type="search" id="search-bar-input" />
<script src="https://cdn.jsdelivr.net/npm/docs-searchbar.js@{version}/dist/cdn/docs-searchbar.min.js"></script>
<script>
docsSearchBar({
hostUrl: "https://mymeilisearch.com",
apiKey: "XXX",
indexUid: "docs",
inputSelector: "#search-bar-input",
debug: true, // Set debug to true if you want to inspect the dropdown
});
</script>
</body>
</html>
The hostUrl
and the apiKey
fields are the credentials of your MeiliSearch instance.
indexUid
is the index identifier in your MeiliSearch instance in which your website content is stored.
inputSelector
is the id
attribute of the HTML search input tag.
Your documentation content is not indexed yet? Check out this tutorial.
WARNING: We recommend providing the MeiliSearch public key, which is enough to perform search requests.
Read more about MeiliSearch authentication.
The default behavior of this library fits perfectly for a documentation search bar, but you might need some customizations.
- Optional parameters (when calling
docsSearchBar
method) - Styling (with CSS)
When calling the docsSearchBar
method, you can add optional fields:
queryHook
takes a callback function as value. This function will be called on every keystroke to transform the typed keywords before querying MeiliSearch. By default, it does not do anything, but it is the perfect place for you to add some preprocessing or custom functionality.
transformData
takes a callback function as value. This function will be called on every hit before displaying them. By default, it does not do anything, but it lets you add any post-processing around the data you received from MeiliSearch.
handleSelected
takes a callback function a value. This function is called when a suggestion is selected (either from a click or a keystroke). By default, it displays anchor links to the results page. Here is an example to override this behavior:
docsSearchBar({
// ...
handleSelected: function (input, event, suggestion, datasetNumber, context) {
// Prevents the default behavior on click and rather opens the suggestion
// in a new tab.
if (context.selectionMethod === "click") {
input.setVal("");
const windowReference = window.open(suggestion.url, "_blank");
windowReference.focus();
}
},
});
Note that, by default, you can already open a new tab thanks to the CMD/CTRL + Click action.
The function is called with the following arguments:
-
input
: a reference to the search input element. It comes with the.open()
,.close()
,.getVal()
and.setVal()
methods. -
event
: the actual event triggering the selection. -
suggestion
: the object representing the current selection. It contains a.url
key representing the destination. -
datasetNumber
: this should always be equal to 1 asdocs-searchbar.js
is searching into one dataset at a time. You can ignore this attribute. -
context
: additional information about the selection. Contains a.selectionMethod
key that can be eitherclick
,enterKey
,tabKey
orblur
, depending on how the suggestion was selected.
You can forward search parameters to the MeiliSearch API by using the meilisearchOptions
key. Checkout out the MeiliSearch documentation about search parameters.
For example, you might want to increase the number of results displayed in the dropdown:
docsSearchBar({
meilisearchOptions: {
limit: 10,
},
});
Here is a basic HTML file used in the playground of this repository.
As a more concrete example, you can check out the configuration applied in the MeiliSearch plugin for VuePress.
/* Main dropdown wrapper */
.meilisearch-autocomplete .dsb-dropdown-menu {
width: 500px;
}
/* Main category */
.meilisearch-autocomplete .docs-searchbar-suggestion--category-header {
color: darkgray;
border: 1px solid gray;
}
/* Category */
.meilisearch-autocomplete .docs-searchbar-suggestion--subcategory-column {
color: gray;
}
/* Title */
.meilisearch-autocomplete .docs-searchbar-suggestion--title {
font-weight: bold;
color: black;
}
/* Description */
.meilisearch-autocomplete .docs-searchbar-suggestion--text {
font-size: 0.8rem;
color: gray;
}
/* Highlighted text */
.meilisearch-autocomplete .docs-searchbar-suggestion--highlight {
color: blue;
}
TIPS: When inspecting the dropdown markup with your browser tools, you should add debug: true
to your docsSearchBar
call to prevent it from closing on inspection.
Here is the CSS customization applied in the MeiliSearch plugin for VuePress.
This package only guarantees the compatibility with the version v0.20.0 of MeiliSearch.
Any new contribution is more than welcome in this project!
If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!
Based on Algolia DocSearch repository from this commit.
Due to a lot of future changes in this repository compared to the original one, we don't maintain it as an official fork.
MeiliSearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the integration-guides repository.