Full text, faceted, (almost) dependency free search engine in javascript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.circleci
dist
docs
src
tests
vendor
.babelrc
.gitignore
LICENSE
README.md
index.js
package-lock.json
package.json
yarn.lock

README.md

ItemsJS - search engine in javascript

Full text, faceted, dependency free search engine in javascript. Created to perform fast search on small json dataset (up to 1000 elements).

Demo

(by @darkrubyist)

See another demo examples

Features

  • faceted search
  • full text
  • pagination
  • no dependencies (only javascript)

Getting Started

NPM

npm install itemsjs
const itemsjs = require('itemsjs')(data, configuration);
const items = itemsjs.search();

Client side

or using from the client side:

npm install itemsjs
<!-- use the latest release -->
<script src="https://unpkg.com/itemsjs@latest/dist/itemsjs.min.js"></script>
<!-- or use the specify version -->
<script src="https://unpkg.com/itemsjs@1.0.37/dist/itemsjs.min.js"></script>
<!-- or use it locally -->
<script src="/node_modules/itemsjs/dist/itemsjs.js"></script>
itemsjs = itemsjs(data, configuration);
itemsjs.search()

Example

npm install itemsjs

# download json data
wget https://raw.githubusercontent.com/itemsapi/itemsapi-example-data/master/items/movies-processed.json -O data.json

Create search.js:

var data = require('./data.json');

var itemsjs = require('itemsjs')(data, {
  sortings: {
    name_asc: {
      field: 'name',
      order: 'asc'
    }
  },
  aggregations: {
    tags: {
      title: 'Tags',
      size: 10
    },
    actors: {
      title: 'Actors',
      size: 10
    },
    genres: {
      title: 'Genres',
      size: 10
    }
  },
  searchableFields: ['name', 'tags']
});

/**
 * get filtered list of movies 
 */
var movies = itemsjs.search({
  per_page: 1,
  sort: 'name_asc',
  // full text search
  // query: 'forrest gump',
  filters: {
    tags: ['1980s']
  }
})
console.log(JSON.stringify(movies, null, 2));

/**
 * get list of top tags 
 */
var top_tags = itemsjs.aggregation({
  name: 'tags',
  per_page: 10
})
console.log(JSON.stringify(top_tags, null, 2));

Test that with :

node search.js

API

var itemsjs = ItemsJS(data, [configuration])

data

The first data argument is an array of objects.

configuration

Responsible for defining global configuration. Look for full example here - configuration

  • aggregations filters configuration i.e. for tags, actors, colors, etc. Responsible for generating facets.

  • sortings you can configure different sortings like tags_asc, tags_desc with options and later use it with one key.

  • searchableFields an array of searchable fields.

itemsjs.search(options)

options

  • per_page amount of items per page.

  • page page number - used for pagination.

  • query used for full text search.

  • sort used for sorting. one of sortings key

  • filters filtering items based on specific aggregations i.e. {tags: ['drama' , 'historical']}

itemsjs.prefilter(items)

It is making items prefiltering before search or aggregation, i.e.:

itemsjs.prefilter(items) {
  return items.filter(item => {
    return item.price > 100;
  });
}

itemsjs.aggregation(options)

It returns full list of filters for specific aggregation

options

  • name aggregation name

  • per_page filters per page

  • page page number

  • query used for quering filters. It's not full text search

itemsjs.similar(id, options)

It returns similar items to item for given id

options

  • field field name for computing similarity (i.e. tags, actors, colors)

  • minimum what is the minimum intersection between field of based item and similar item to show them in the result

  • per_page filters per page

  • page page number

itemsjs.reindex(data)

It's used in case you need to reindex the whole data

data

An array of objects.

Credit

  • Lunr.js for providing full text search.