Javascript

Getting Started

Install

Frontend

You can either use a package manager like npm or include a <script> tag.

Node.js / React Native / Browserify / webpack

We are browserifyable and webpack friendly.

npm install algoliasearch --save

TypeScript typings

For Typescript typings, we provide the definition file via typings

npm install --save @types/algoliasearch

Bower

bower install algoliasearch -S

<script> tag using CDNS

jsDelivr is a global CDN delivery for JavaScript libraries.

To include the latest releases and all upcoming features and patches, use this:

<script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script>
Other CDNS

We recommend using jsDelivr, but algoliasearch is also available at:

  • CDNJS
  • unpkg: https://unpkg.com/algoliasearch@3/dist/algoliasearch.min.js

Search only/lite client

We have a lightweight build available that can only do searches. Use it when filesize is important to you or if you like to include only what you need.

Find it on jsDelivr:

<script src="https://cdn.jsdelivr.net/algoliasearch/3/algoliasearchLite.min.js"></script>

Init index - initIndex

To initialize the client, you need your Application ID and API Key. You can find both of them on your Algolia account.

// var algoliasearch = require('algoliasearch');
// var algoliasearch = require('algoliasearch/reactnative');
// var algoliasearch = require('algoliasearch/lite');
// or just use algoliasearch if you are using a <script> tag
// if you are using AMD module loader, algoliasearch will not be defined in window,
// but in the AMD modules of the page

var client = algoliasearch('applicationID', 'apiKey');
var index = client.initIndex('indexName');
index.search('something', function searchDone(err, content) {
  console.log(err, content);
});

Quick Start

In 30 seconds, this quick start tutorial will show you how to index and search objects.

Without any prior configuration, you can start indexing 500 contacts in the contacts index using the following code:

var index = client.initIndex('contacts');
var contactsJSON = require('./contacts.json');

index.addObjects(contactsJSON, function(err, content) {
  if (err) {
    console.error(err);
  }
});

You can now search for contacts using firstname, lastname, company, etc. (even with typos):

// firstname
index.search('jimmie', function(err, content) {
  console.log(content.hits);
});

// firstname with typo
index.search('jimie', function(err, content) {
  console.log(content.hits);
});

// a company
index.search('california paint', function(err, content) {
  console.log(content.hits);
});

// a firstname & company
index.search('jimmie paint', function(err, content) {
  console.log(content.hits);
});

Settings can be customized to tune the search behavior. For example, you can add a custom sort by number of followers to the already great built-in relevance:

index.setSettings({
  'customRanking': ['desc(followers)']
}, function(err, content) {
  console.log(content);
});

You can also configure the list of attributes you want to index by order of importance (first = most important):

index.setSettings({
  'searchableAttributes': [
    'lastname',
    'firstname',
    'company',
    'email',
    'city',
    'address'
  ]
}, function(err, content) {
  console.log(content);
});

Since the engine is designed to suggest results as you type, you’ll generally search by prefix. In this case the order of attributes is very important to decide which hit is the best:

index.search('or', function(err, content) {
  console.log(content.hits);
});

index.search('jim', function(err, content) {
  console.log(content.hits);
});