Skip to content

Latest commit

 

History

History
180 lines (127 loc) · 7.22 KB

README.md

File metadata and controls

180 lines (127 loc) · 7.22 KB

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

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

Params

  • options {Object}: Options for setting up the algoliasearch client and index.
  • options.APPLICATION_ID {String}: algolia application id
  • options.API_KEY {String}: algolia api key used for adding records to an index
  • options.index {String}: algolia index to add records to
  • options.collectFn {Function}: Custom collect function to use for creating records from file objects. See collect method for more information.
  • options.indexFn {Function}: Custom index function used to add records to the algolia index. See index method for more information.
  • returns {Object}: Indexer instance to pass to base-search.

Example

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

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

  • options {Object}: Options for setting up the algoliasearch client and index.
  • options.APPLICATION_ID {String}: algolia application id
  • options.API_KEY {String}: algolia api key used for adding records to an index
  • options.index {String}: algolia index to add records to
  • options.collectFn {Function}: Custom collect function to use for creating records from file objects. See collect method for more information.
  • options.indexFn {Function}: Custom index function used to add records to the algolia index. See index method for more information.
  • returns {Object}: Indexer instance to pass to base-search.

Example

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

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

  • file {Object}: File object passed in from base-search
  • next {Function}: Callback function to return the customized record or false to indicate the file should be skipped.

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));

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

  • files {Object}: Files object passed in from base-search.
  • options {Object}: Options object passed in from base-search.
  • cb {Function}: Callback function passed in from [base-seaerch][] used to indicate that indexing has finished.

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.