Algolia InstantSearch
repository·master·Indexed 25 days ago
https://github.com/algolia/instantsearchA comprehensive JavaScript ecosystem for building search interfaces powered by Algolia. It provides UI components and logic wrappers for vanilla JS, React, and Vue to accelerate the development of search-driven user experiences.
What's inside InstantSearch
- Vue InstantSearch is a library built by Algolia that provides lightning-fast search UI components specifically for Vue.js applications.
Overview of InstantSearch
masterInstantSearch is a JavaScript library used to build performant, instant search experiences using the Algolia search API. It provides a set of tools to create search UIs in vanilla JavaScript, React, and Vue. The ecosystem includes framework-specific wrappers and a family of libraries for other platforms including Android and iOS.What is algolia-experiences
masterThealgolia-experiencespackage allows you to implement Algolia search on a website without writing code. You create the search experiences visually within the Algolia dashboard and then embed them into your website using a script and a specific container attribute.What is AlgoliaSearchHelper and how it works
masterThe
AlgoliaSearchHelperis the primary interface of the Helper library. It manages search parameters and coordinates the search cycle using an event-driven architecture. Instead of just returning a promise, it emits events to ensure that the UI remains synchronized with the latest request, preventing race conditions where older search results might arrive after newer ones due to network latency.Key Events:
change: Triggered when a search parameter is set or updated.search: Triggered when a search request is sent to Algolia.result: Triggered when results are retrieved from Algolia.error: Triggered when Algolia returns an error.searchQueueEmpty: Triggered when there are no more pending searches.searchForFacetValues: Triggered when a search is sent viasearchForFacetValues.searchOnce: Triggered when a search is sent viasearchOnce.
What is algoliasearch-helper and when to use it
masterThe
algoliasearch-helperis a companion library for thealgoliasearch-client-javascript. It provides a higher-level API to manage search parameters, handle pagination, manage facet exclusions, and implement disjunctive faceting (searching across two or more values of the same facet).Recommendation:
- For building a complete search interface, use InstantSearch.
- For building an autocomplete menu, use Autocomplete.
- Use the Helper when you need a programmatic way to track search parameters and manage the search state without a full UI framework.
What is the algoliasearch-helper
masterThealgoliasearch-helperis a high-level API built on top of the Algolia JS client. While the JS client provides basic API access, the Helper is specifically focused on search-only features, providing a structured foundation for building search-as-you-type experiences and easy access to advanced search filters. It is framework-agnostic and can be used with any JS library (e.g., jQuery, React, Vue) or without a framework entirely.How the Highlight widget works
masterThe Highlight widget is used to display search results with the matching query terms visually highlighted. It wraps the text in a root span with the class.ais-Highlight. The non-matching parts of the text are wrapped in spans with the class.ais-Highlight-nonHighlighted, and the matching (highlighted) parts are wrapped in a tag (defaulting to<mark>) with the class.ais-Highlight-highlighted.Implement Disjunctive faceting
masterBy default, selecting a facet value filters the results, which may hide other available facet values for that same attribute. Disjunctive faceting solves this by performing a second request specifically to retrieve all possible facet values for a given attribute, regardless of the current filters. The Helper merges the results of the main search and this second request to provide a complete list of values to the UI.How to create a custom widget using a connector
masterConnectors allow you to decouple the search logic from the UI rendering. To create a custom widget:
- Define a render function that receives
renderOptionsandisFirstRender. - Wrap this render function with the connector (e.g.,
connect{{ pascalCaseName }}). - Register the resulting custom widget using
search.addWidgets().
import { connect{{ pascalCaseName }} } from '{{ packageName }}'; // 1. Create a render function const render{{ pascalCaseName }} = (renderOptions, isFirstRender) => { // Rendering logic }; // 2. Create the custom widget const custom{{ pascalCaseName }} = connect{{ pascalCaseName }}( render{{ pascalCaseName }} ); // 3. Instantiate search.addWidgets([ custom{{ pascalCaseName }}( { // instance params }, ), ]);- Define a render function that receives
How the algoliasearch-helper lifecycle works
masterThe helper follows a cyclical state-driven lifecycle:
- Modify Parameters: Update search state via user interactions (e.g.,
helper.setQuery('iphone')). - Trigger Search: Execute the query after modifications are applied using
helper.search(). - Read Results: Use the
resultevent handler to receive the transformed Algolia answers and update the UI. - Repeat: Return to step 1 based on further user interaction.
- Modify Parameters: Update search state via user interactions (e.g.,
How the Helper handles pagination automatically
masterTo ensure users see the most relevant results after changing a filter or query, the Helper automatically resets the page to 0 whenever a write method is called, except for thesetPagemethod itself. This removes the need to manually call.setPage(0)before every.search()call.Create multi-queries using Helper derivation
masterDerivation allows you to create 'virtual helpers' based on a main Helper instance. This enables you to query multiple indices simultaneously or search the same index with different parameters. Virtual helpers are created using thederivemethod with a derivation function. Whensearch()is called on the main helper, the derivation function is executed for each virtual helper, and all requests are sent together. Results are dispatched to each helper viaresultorerrorevents.