Skip to content
Sébastien Cottinet edited this page Jun 15, 2021 · 5 revisions

This module is a reverse-search engine.

Instead of indexing data and searching for them using filters, Koncorde does the opposite: it indexes search filters, and returns the corresponding ones when presented with data.

  • an arbitrary large number of filters can be registered and indexed;
  • whenever data are submitted to Koncorde, it returns the list of indexed filters matching them;
  • Koncorde's filter syntax supports a variety of different matchers, and ways to combine them.

Koncorde can be used in a variety of ways. For instance:

  • as a base of a notification system, where indexed filters are used as user subscriptions: Koncorde tells which JSON objects verify what subscriptions, making it easy to send events to listening users;
  • to verify if JSON objects comply to filters used as validation rules.

Example:

In the following example, we'll listen to objects containing a position property, describing a geopoint. We want that geopoint to be 500 meters around a pre-defined starting position.

This can be described by the following Koncorde filter:

{
    "geoDistance": {
        "position": {
            "lat": 43.6073913,
            "lon": 3.9109057
        },
        "distance": "500m"
    }
}

All you need to do now is to register this filter to the engine, and use it to test data:

import { Koncorde } from 'koncorde';

const engine = new Koncorde();

const filter = {
    geoDistance: {
        position: {
            lat: 43.6073913,
            lon: 3.9109057
        },
        distance: "500m"
    }
};

const filterId = engine.register(filter);

// Filter Identifiers are seeded. Given the same seed (to be provided to the
// constructor), then the filter IDs stay stable.
console.log(`Filter identifier: ${filterId}`);

// No match found, returns an empty array (distance is greater than 500m)
console.log(engine.test({ position: { lat: 43.6073913, lon: 5.7 } }));

// Point within the filter's scope: returns the list of matched filters
// Here we registered just one of them, so the array contains only 1 filter ID
console.log(engine.test({ position: { lat: 43.608, lon: 3.905 } }));

// No match found, returns an empty array 
// (the geopoint in the provided data is not stored in the tested field)
console.log(engine.test({ not_position: { lat: 43.608, lon: 3.905 } }));

Install

This library is compatible with Node.js version 12.x or higher. Both a C and a C++ compilers are needed to install its dependencies: Koncorde cannot be used in a browser.

Koncorde is compatible with either Javascript or Typescript projects.

To install:

npm install koncorde
Clone this wiki locally