1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Anatomy of an Algolia Search API Response

    Raphael TerrierRaphael Terrier
    algoliaAlgolia

    Before playing with Instantsearch widgets and build a rich search experience, the fundamental step is to understand what the library will be dealing with. In this lesson we we’ll cover the anatomy of the JSON search API response send back by Algolia for each search query and how it can be used.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: This is the final state of the project we are going to be building throughout this course. As you can see, it's a simple instant search page containing a variety of widgets.

    In order to display all that, the instant search library interacts with Algolia servers, sending queries and receiving responses. With each API response comes all the information necessary for instant search to build all those widgets. That's what we'll dig into.

    To take out what the response looks like, let's open the developer console, click on the network tab, filter on XHR. Then we'll simply perform a query on the website. This is what the API response looks like. It's a simple JSON object containing a dozen attributes referring to the hits, the facets, the query, and some stats.

    The main one is the hits array. This is what is holding the actual hits that will be displayed on-screen. Here, the setting is set to send 20 hits per page.

    Let's open the first one. What it contains will obviously depend on how your records are structured and what you've set as retrievable attributes. Here, to name a few, we have a mix of arrays for categories, strings for brand or name, numeric values for popularity, price, and rating, and a Boolean for free shipping.

    If highlighting is enabling, it is by default. For each results matching the query, we will return the records attribute where it matched in this highlight result object. We will cover highlighting in more details in the following video.

    If enabled, snippeting works in a similar fashion to retrieve only part of a long text attribute. It is not in this example.

    Other notable attributes from our API response are number hits, number of page, hits per page, and page. That gives us information about the volume of hits for that query. It is helpful for pagination, for instance. Processing time and number hits are also used for stats.

    The query attribute refers to the actual text query, whereas params contains the whole query and refinements passed to the engine to retrieve those results. Finally, we have attributes related to facets that are returned for each query as well. These gives us the available facets and some stats. That's pretty much all we need to build a nice search front end.