Back End Search with an API Client
On this page
To query your Algolia index, you need an Application ID and a valid API key. You can find them in your Algolia Dashboard in the API Keys section. These credentials let you connect to Algolia and perform operations on your data.
You should also select one of Algolia’s API clients to benefit from the SLA, which relies on a robust retry logic to guarantee end-to-end reliability.
Basic queries
To retrieve results from Algolia, use the Search index method. Querying Algolia involves:
- choosing the index to query against
- sending the query string
- adding optional parameters
Along with the user’s text, you can provide any number of query parameters in your query; some override an index’s settings, such as disabling typo tolerance; others enrich the search, such as filtering. Adding optional parameters can alter textual relevance behavior at query-time. For example, it’s possible to define stop words or change plurals behavior only for certain queries.
Algolia recommends querying directly from the user’s browser, mobile device, or client to ensure optimal performance by reducing latency (and benefiting from offloading your servers).
Here’s a basic query example:
1
2
3
index.search('query', {
ignorePlurals: true
});
Empty queries
Sometimes, you might not need to pass in a textual query. It’s possible to retrieve results from Algolia by specifying an index and other optional parameters (most likely, a filter). In this way, it’s possible to use Algolia beyond a standard “search results” experience. For example, one common use case is to leverage filtering capabilities to generate category landing pages.
Browsing your index
Algolia allows you to fetch all records in an index using the browse
method. If you need to retrieve the full contents of your index (for backup, SEO purposes or for running a script on it), leverage the browse
method instead of search
, as search
is optimized for speed. Results are returned ranked by attributes and custom ranking. Additionally, browse
supports most search parameters, meaning you can easily apply filters to retrieve a subset of results.
Multiple indices/multiple queries
Multiple indices are used when you have different kinds of data (movies + actors), or you need to display results with different rankings. You can do this by using the same query / response logic, one for each index or query.
Response format
If a query is successful, your request will generate a JSON response in return. This response contains both the matching results (“hits”) as well as other information useful for building a full-fledged search experience (for example: facets, highlighting, hits count, paging, and more).
Highlighting and snippeting
Highlighting is an important tool to demonstrate to searchers why a result matched their query by providing different styling to all matched query words. By default, Highlighting is enabled on all searchable attributes.
Snippeting will return only a portion of the matched attribute; namely, the matched words and some words around them. Unlike highlighting, snippeting must be proactively enabled for each attribute you wish to snippet, although you can set the value *
to snippet all attributes.
Pagination
Pagination gives you full control over how you retrieve query results. You can implement standard paging or retrieve specific record subsets that ignore page boundaries (infinite scrolling).
You can set pagination defaults at indexing time and override them at query time.
Faceting
Facets are used to create categories on a select group of attributes. For example, useful facets for an index of books might be author and genre. Additionally, Algolia calculates the count of records for each facet. You can display facets and facet counts in the UI to give users the ability to filter results (for example, by author or genre).
Search for facet values
Closely tied to faceting is searching for facet values. With this powerful feature, you give your user the ability to navigate through maybe hundreds of different facet values. And because they can search, you are no longer concerned about creating too many values—they will find what they need.