OpenPaaS Documentation logo OpenPaaS Documentation

Table of contents

Overview

The OpenPaaS Core provides a search module which relies on Elasticsearch. This page will provides all the informations for a developer to use the core module in order to index and search data.

Indexing data

The core elasticsearch module provides a listeners module which must be used to register a listener. This listener will be in charge of handling everything needed to transform data from OpenPaaS to Elasticsearch, indexing data to the right place, etc…

Registering a listener in the search module is as easy as giving a valid object. As an example, let’s say that we want to index messages coming from a chat module:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const listeners = dependencies('elasticsearch').listeners;

listeners.addListener({
  events: {
    add: 'message:create',
    update: 'message:update',
    remove: 'message:delete'
  },
  denormalize: (message) => {
    // do something with input data (coming from topic defined above)
    // then return the data to be indexed
    return {
      text: message.message,
      date: message.timestamps.created_at
    };
  },
  getId: (message) => message._id.toString(),
  type: 'message',
  index: 'message.idx'
});

Note that calling addListener will return a set of functions you can also use to make calls to Elasticsearch yourself:

1
2
3
4
5
6
const { listeners } = dependencies('elasticsearch');

const searchHandler = listeners.addListener(x);

//searchHandler.indexData(data, callback)
//searchHandler.removeFromIndex(data, callback)

Once the listener is registered, indexing data is as easy as:

1
2
3
4
5
6
7
8
9
const { local } = dependencies('pubsub');
const message = {
  message: 'This is a wonderful message',
  timestamps: {
    created_at: new Date()
  }
};

local('message:create').publish(message);

The listener will be called automatically, and message will be indexed into Elasticsearch.

Search data

Making calls to Elasticsearch must be achieved from the elasticsearch module with the searchDocuments function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
const elasticsearch = dependencies('elasticsearch');
const query = {}; // some ES query https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html

elasticsearch.searchDocuments({
  index: 'message.idx',
  type: 'message',
  from: 0,
  size: 10,
  body: query
}, (err, result) => {
  if (err) {
    return console.log('I failed to search messages', err);
  }

  console.log('Search results', result.hits.hits);
});

This will search messages in the message.idx index in the Elasticsearch service and return the search results.