Search Lessons, Code Snippets, and Videos
search by algolia
#native_cta# #native_desc# Sponsored by #native_company#

Angular Full Text Search With Algolia Frontend - Part 1

Episode 27 written by Jeff Delaney
full courses for pro members

In this lesson, we are going to build a full text search feature using Algolia and Angular 4. It will be broken down into two sections, with the first part focusing the frontend in Angular and the second part focusing on building your own searchable index with Firebase Cloud Functions

Algolia Lesson Series

instant search demo in Angular 4 with Algolia

Setting up Angular 4 with Algolia and InstantSearch.js

The first step is to signup for a free Algolia account. It will give you an initial starter index of 500 Hollywood actors/actresses named getstarted_actors. We will be using this index for the first half of this tutorial.

Add the API Key to the Environment

In Angular, you only need to include the “search-only” API key. I’m adding it to the environment.ts file.

export const environment = {
production: false,

algolia: {
appId: 'APP_ID',
indexName: 'getstarted_actors',
urlSync: false

Install InstantSearchJS

The next step is to include the instantsearch.js package. This package is maintained by Algolia and includes an amazing collection of JavaScript widgets to quickly build a full text search interface. In total, there are about 20 different widgets to choose from.

npm install instantsearch.js --save

If you want to use the Algolia CSS and templates, add the following lines to the head of the index.html file.

<link rel="stylesheet" type="text/css" href="[email protected]/dist/instantsearch.min.css">
<link rel="stylesheet" type="text/css" href="[email protected]/dist/instantsearch-theme-algolia.min.css">

Search Component

Our entire search feature will be wrapped up in the SearchUiComponent, which you can generate with ng g component search-ui. During ngOnInit you configure the instantsearch with your environment variables and call .start() initialize it.


import { Component, OnInit } from '@angular/core';
import { environment } from '../../environments/environment';
import * as instantsearch from 'instantsearch.js'

selector: 'search-ui',
templateUrl: './search-ui.component.html',
styleUrls: ['./search-ui.component.scss']
export class SearchUiComponent implements OnInit {

search: any;

constructor() { }

ngOnInit() {
const options = environment.algolia; = instantsearch(options);;

You build out the Algolia search interface by adding widgets to it. Let’s start by adding a search field and the results (hits) to our component.
container: '#search-box'

// initialize hits widget
container: '#hits',


In the template, you just ID the elements you want replaced with the widgets - just like jQuery.

<div id="search-box">
<!-- SearchBox widget will appear here -->
<div id="hits">
<!-- Hits widget will appear here -->

Custom Templating

You can also add templating to certain widgets with mustache, interpolating data with double curly brackets. In the hits widget, I display the actor’s name and image rather than the raw JSON.
container: '#hits',
templates: {
empty: 'No results',
item: `<img src={{image_path}} width="50px">
Result {{objectID}}:
escapeHits: true

The analytics widget does not modify the actual UI, but you can access the results object, which is useful for analytics and logging.{
pushFunction: (query, state, results) => {

There are about 20 other widgets you can add to the search, check out the example below to see how I added pagination and stats.

Full Example