Concepts / Building Search UI / Back end search with an API client
Feb. 18, 2019

Back End Search with an API Client

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’ll also want to choose one of our API Clients. We have support 10 languages (php, javascript, C#, Java, Ruby, Scala, Go, Swift/iOs, Android, and Python). The benefit of using the API clients is that you can take advantage of our SLA, which relies on a robust retry logic to guarantee end-to-end reliability.

Basic Queries

To retrieve results from Algolia, you will need to leverage 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.

Note: We recommend querying Algolia directly from the end user’s browser, mobile device, or client to ensure optimal performance by reducing latency (and giving you the benefit of offloading your servers!).

Here’s a basic query example:

1
2
3
4
index.search({
    query: 'query',
    ignorePlurals: true
});

Empty Queries

In some cases, 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 browse through all objects of an index using the Browse index method. If you need to retrieve all the content of your index (for backup, SEO purposes or for running a script on it), we recommend leveraging browse as opposed to querying, as this method is optimized for speed. Results will be returned ranked by attributes and custom ranking. Additionally, browse supports almost all of the search parameters, meaning you can easily apply filters to retrieve a subset of results. To view code examples, see our exporting your data tutorial.

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 & 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, on an index of books, useful facets might be author and genre. Additionally, Algolia calculates the count of records for each facet. Thereafter, facets and facet counts can be displayed on the UI, to give users the ability to filter results (e.g. 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.

Did you find this page helpful?