Home

Awesome

search-indexer-algolia NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

base-search indexer to enable collecting and adding records to an algolia search index

Install

Install with npm:

$ npm install --save search-indexer-algolia

HEADS UP!

SECRET_KEY has been changed to API_KEY to reflect the naming conventions of the algoliasearch api. SECRET_KEY will still work but will be deprecrated and removed in the next major version.

Usage

var algolia = require('search-indexer-algolia');

var options = {
  APPLICATION_ID: '12345',
  API_KEY: 'xxxxx',
  index: 'my-index',
  collectFn: function(file, next) {
    // customize object being collected
    var obj = {
      objectID: file.key,
      key: file.key
    };
    return next(null, obj);
  }
};
app.search.indexer('algolia', algolia(options));

API

indexer

Creates a new algolia indexer that may be used in base-search.

Params

Example

var opts = {
  APPLICATION_ID: '12345',
  API_KEY: 'xxxxx'
};
app.search.indexer('algolia', algolia(opts));

.init

Initialize the algoliasearch client and index. The client will only be created the first time this method is called. This is called inside the collect and index methods but may be called manually before using.

Params

Example

var indexer = algolia(opts);
app.search.indexer('algolia', indexer.init(opts));

.collect

This method is called when collecting records to be indexed. It will be called for each file coming through the stream created by app.search.collect(). If options.collectFn is a function, the function will be called using the indexer instance as the context. This provides access to the algoliasearch index by using this.idx inside the function.

Params

Example

// set a custom `collectFn` when creating the indexer
var opts = {
  ...
  collectFn: function(file, next) {
    if (!file.data.foo) {
      // passing `false` says to not collect this file
      return next(null, false);
    }
    return next(null, {objectID: file.key, key: file.key, title: file.title});
  }
};
app.search.indexer('algolia', algolia(opts));

.index

This method is called when indexing the collected files. It will be passed an object of files collect through the collect method. If options.indexFn is a function, the function will be called using the indexer instance as the context. This provides access to the algoliasearch index by using this.idx inside the function.

Params

Example

// set a custom `indexFn` when creating the indexer
var opts = {
  ...
  indexFn: function(files, options, cb) {
    var arr = Object.keys(files)
      .map(function(key) {
        return files[key];
      });

    // add the array of objects to the algolia index
    this.idx.addObjects(arr, cb);
  }
};
app.search.indexer('algolia', algolia(opts));

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for advice on opening issues, pull requests, and coding standards.

Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test

Author

Brian Woodward

License

Copyright © 2018, Brian Woodward. Released under the MIT License.


This file was generated by verb-generate-readme, v0.7.0, on June 18, 2018.