Socket
Socket
Sign inDemoInstall

algoliasearch

Package Overview
Dependencies
0
Maintainers
1
Versions
330
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    algoliasearch

Algolia Search API Client


Version published
Weekly downloads
1.2M
decreased by-23.33%
Maintainers
1
Created
Weekly downloads
 

Package description

What is algoliasearch?

The algoliasearch npm package is a JavaScript client for Algolia, a hosted search API that provides a fast and accurate search experience for websites and mobile applications. The package allows developers to integrate Algolia's search capabilities into their JavaScript applications, enabling features such as full-text search, faceting, filtering, and geolocation queries.

What are algoliasearch's main functionalities?

Search

Perform a search query on an Algolia index and retrieve the results.

const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('your_index_name');
index.search('query string').then(({ hits }) => {
  console.log(hits);
});

Indexing

Add or update a record in an Algolia index.

const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('your_index_name');
index.saveObject({ objectID: '1', name: 'Foo' }).then(() => {
  console.log('Object indexed');
});

Configure Index Settings

Configure settings of an Algolia index to define ranking, attributes for faceting, etc.

const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
const index = client.initIndex('your_index_name');
index.setSettings({
  searchableAttributes: ['name', 'description'],
  customRanking: ['desc(popularity)']
}).then(() => {
  console.log('Settings updated');
});

Manage Indices

List all indices in your Algolia application and manage them.

const algoliasearch = require('algoliasearch');
const client = algoliasearch('YourApplicationID', 'YourAdminAPIKey');
client.listIndices().then(({ items }) => {
  console.log(items);
});

Other packages similar to algoliasearch

Readme

Source

Algolia Search API Client for Javascript

This Javascript client let you easily use the Algolia Search API in a browser, it is compatible with most browsers:

  • Internet Explorer ≥ 8
  • Firefox ≥ 3.5
  • Google Chrome ≥ 3
  • Safari ≥ 4
  • Opera ≥ 12
  • Opera mobile ≥ 12
  • etc.

See this wikipedia page to have more details on supported browsers (we use XDomainRequest for IE8 and IE9).

Setup

To setup your project, follow these steps:

  1. Download the client and add a script include of algoliasearch.min.js
  2. Initialize the client with your ApplicationID and API-Key. You can find all of them on your Algolia account.
  3. When you use this API client for search on a website, we strongly recommand to use a key with an ACL restricted to "search". You can retrieve one with client.addUserKey(["search"]).
  <script src="algoliasearch.min.js"></script>
  <script>
    client = new AlgoliaSearch('ApplicationID', 'API-Key');
    ...

Quick Start

First, index some data. For example, you can use the command line client quick start to index the 500 contacts sample.

You can then update the example/autocomplete.html file with your ApplicationID, API-Key and index name to test the autocomplete feature. This version is based on typeahead.js version 0.9.3 with a small patch to allow usage of Algolia JS client.

You can also update the example/instantsearch.html file with your ApplicationID, API-Key and index name to test an instant-search example.

General Principle

All API calls will return the result in a callback that takes two arguments:

  1. sucess: a boolean that is set to false when an error was found.
  2. content: the object containing the answer (if an error was found, you can retrieve the error message in content.message)

To perform a search, you just need to initialize the index and perform a call to the search function.
You can use the following optional arguments:

  • attributes: a string that contains the names of attributes to retrieve separated by a comma.
    By default all attributes are retrieved.
  • attributesToHighlight: a string that contains the names of attributes to highlight separated by a comma.
    By default indexed attributes are highlighted. Numerical attributes cannot be highlighted. A matchLevel is returned for each highlighted attribute and can contain: "full" if all the query terms were found in the attribute, "partial" if only some of the query terms were found, or "none" if none of the query terms were found.
  • attributesToSnippet: a string that contains the names of attributes to snippet alongside the number of words to return (syntax is 'attributeName:nbWords'). Attributes are separated by a comma (Example: "attributesToSnippet=name:10,content:10").
    By default no snippet is computed.
  • minWordSizefor1Typo: the minimum number of characters in a query word to accept one typo in this word.
    Defaults to 3.
  • minWordSizefor2Typos: the minimum number of characters in a query word to accept two typos in this word.
    Defaults to 7.
  • getRankingInfo: if set to 1, the result hits will contain ranking information in _rankingInfo attribute.
  • page: (pagination parameter) page to retrieve (zero base).
    Defaults to 0.
  • hitsPerPage: (pagination parameter) number of hits per page.
    Defaults to 10.
  • aroundLatLng: search for entries around a given latitude/longitude (specified as two floats separated by a comma).
    For example aroundLatLng=47.316669,5.016670).
    You can specify the maximum distance in meters with the aroundRadius parameter (in meters) and the precision for ranking with aroundPrecision (for example if you set aroundPrecision=100, two objects that are distant of less than 100m will be considered as identical for "geo" ranking parameter).
    At indexing, you should specify geoloc of an object with the _geoloc attribute (in the form {"_geoloc":{"lat":48.853409, "lng":2.348800}})
  • insideBoundingBox: search entries inside a given area defined by the two extreme points of a rectangle (defined by 4 floats: p1Lat,p1Lng,p2Lat,p2Lng).
    For example insideBoundingBox=47.3165,4.9665,47.3424,5.0201).
    At indexing, you should specify geoloc of an object with the _geoloc attribute (in the form {"_geoloc":{"lat":48.853409, "lng":2.348800}})
  • queryType: select how the query words are interpreted:
  • prefixAll: all query words are interpreted as prefixes,
  • prefixLast: only the last word is interpreted as a prefix (default behavior),
  • prefixNone: no query word is interpreted as a prefix. This option is not recommended.
  • numerics: specify the list of numeric filters you want to apply separated by a comma. The syntax of one filter is attributeName followed by operand followed by value. Supported operands are <, <=, =, > and >=. You can have multiple conditions on one attribute like for example numerics=price>100,price<1000.
  • tags: filter the query by a set of tags. You can AND tags by separating them by commas. To OR tags, you must add parentheses. For example, tags=tag1,(tag2,tag3) means tag1 AND (tag2 OR tag3).
    At indexing, tags should be added in the _tags attribute of objects (for example {"_tags":["tag1","tag2"]} )
index = client.initIndex('contacts');
index.search('query string', function(success, content) {
    for (var h in content.hits) {
        console.log('Hit(' + content.hits[h].objectID + '): ' + content.hits[h].toString());
    }
});

index.search('query string', function(success, content) {
    for (var h in content.hits) {
        console.log('Hit(' + content.hits[h].objectID + '): ' + content.hits[h].toString());
    }
}, {'attributes': 'firstname,lastname', 'hitsPerPage': 50});

The server response will look like:

{
  "hits": [
    {
      "firstname": "Jimmie",
      "lastname": "Barninger",
      "company": "California Paint & Wlpaper Str",
      "address": "Box #-4038",
      "city": "Modesto",
      "county": "Stanislaus",
      "state": "CA",
      "zip": "95352",
      "phone": "209-525-7568",
      "fax": "209-525-4389",
      "email": "jimmie@barninger.com",
      "web": "http://www.jimmiebarninger.com",
      "followers": 3947,
      "objectID": "433",
      "_highlightResult": {
        "firstname": {
          "value": "<em>Jimmie</em>",
          "matchLevel": "partial"
        },
        "lastname": {
          "value": "Barninger",
          "matchLevel": "none"
        },
        "company": {
          "value": "California <em>Paint</em> & Wlpaper Str",
          "matchLevel": "partial"
        },
        "address": {
          "value": "Box #-4038",
          "matchLevel": "none"
        },
        "city": {
          "value": "Modesto",
          "matchLevel": "none"
        },
        "email": {
          "value": "<em>jimmie</em>@barninger.com",
          "matchLevel": "partial"
        }
      }
    }
  ],
  "page": 0,
  "nbHits": 1,
  "nbPages": 1,
  "hitsPerPage": 20,
  "processingTimeMS": 1,
  "query": "jimmie paint",
  "params": "query=jimmie+paint&"
}

Update the index

The javascript client is dedicated to web apps searching directly from the browser. In some use-cases, it can however be interesting to perform updates to the index directly in javascript, for example in an HTML5 mobile app. Therefore, just as for other languages, the javascript client is able to add, update or delete objects, or to modify index settings.

For more details about updating an index from javascript, have a look at the algoliasearch.js source file to see details about each function.

Note: If you use the javascript client to update the index, you need to specify https as the protocol in the client initialization:

  <script src="algoliasearch.min.js"></script>
  <script>
    client = new AlgoliaSearch('ApplicationID', 'API-Key', 'https');
    ...

Keywords

FAQs

Last updated on 03 Nov 2013

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc