/strapi-plugin-elastic

The elasticsearch plugin allows you to easily synchronize your database with elasticsearch

Primary LanguageJavaScriptMIT LicenseMIT

Project logo

plugin

GitHub issues GitHub forks GitHub stars GitHub license


This plugin has not been tested on mongodb


The purpose of developing this plugin is to use the elastic search engine in Strapi to help the application development process


📝 Table of Contents

Prerequisites


Install Elasticsearch - https://www.elastic.co/downloads/elasticsearch

Install plugin

  • Go to the project path

    • cd PROJECT/plugins
  • Clone the project

    • git clone https://github.com/marefati110/strapi-plugin-elasticsearch.git
  • Install dependencies

    • yarn install

🏁 Getting Started

  • Go to PROJECT/config/middleware.js and add "elastic" to end of load.before
  • enable elastic middleware in setting

code4
final result should be like the image


How plugin works?

After the first run of the project, it creates a config file at PROJECT/config/elasticsearch.js

config file should look like the image

code3

By default, syncing occurs in two ways

The answer of any request that makes a change in the model is stored in Elasticsearch this is especially true for the Strap panel admin

Or in response to any request, search for the pk of model the model in which the change was made, and after retrieving the information from the database, stores it in the elasticsearch

In the following, solutions are prepared for more complex scenarios.

After installing the plugin and running it, it creates an config file in the PROJECT/config/elasticsearch.js

In the connections section, the settings related to the connection to the elasticsearch are listed, there is also a help link

In the setting section, there are the initial settings related to the elastic plugin.

In the models section for all models in the Project/api/** path there is a model being built and you can change the initial settings


🎈 Usage

Scenario 1

For example, we want to make changes to the article model and then see the changes in the Elasticsearch.

The first step is to activate in the settings related to this model

After saving and restarting the plugin, it creates an index for this model in the elasticsearch.

Note that the name selected for the index can be changed in the settings of the model.

At the end of the settings should be as follows

{
  model: 'article',
  pk: 'id',
  plugin: null, // changed to true
  enable: true,
  index: 'article',
  relations: [],
  conditions: {},
  supportAdminPanel: true,
  fillByResponse: true,
  migration: false,
  urls: [],
},

Now in the strapi admin panel, by making an creating , deleting or updating , you can see the changes in Elasticsearch.

Scenario 2

In this scenario, we want to make a change in the model using the rest api and see the result in Elasticsearch.

After sending a post request to /articles, changes will be applied and we will receive a response to this

{
  "id": 1,
  "title": "title",
  "content": "content"
}

and model config should change to

{
  model: 'article',
  pk: 'id',
  plugin: null,
  enable: true,
  index: 'article',
  relations: [],
  conditions: {},
  supportAdminPanel: true,
  fillByResponse: true, // default value
  migration: false,
  urls: ['/articles'], //changed
},

If the fillByResponse settings are enabled for the model, the same data will be stored in Elasticsearch, otherwise the data will be retrieved from the database using pk and stored in Elasticsearch.

Scenario 3

This scenario is quite similar to the previous scenario with these differences being the response

{
  "metaData": null,
  "data": {
    "articleID": 1,
    "title": "title",
    "content": "content"
  }
}

By default, the plugin looks for pk in the response or ctx.body.id

We can rewrite these settings for a specific url

config model should change to

{
  model: 'article',
  pk: 'id',
  plugin: null,
  enable: true,
  index: 'article',
  relations: [],
  conditions: {},
  supportAdminPanel: true,
  fillByResponse: true,
  migration: false,
  urls: [
    {
      '/articles':{
        pk: 'data.articleID',  // over write
        relations: [],  // over write
        conditions: {}, // over write
      }
    }
  ],
},

Scenario 4

In this scenario, no pk may be found in the request response

{
  "success": true
}

In this case, the synchronization operation can be performed on the controller

there is some functions for help

const articleData = { title: 'title', content: 'content' };
const article = await strapi.query('article').create(articleData);

strapi.elastic.createOrUpdate('article', { data: article, id: article.id });
// or
strapi.elastic.migrateById('article', { id: article.id }); // execute new query

and for delete data

const articleId = 1;
const article = await strapi.query('article').delete(articleData);

strapi.elastic.destroy('article', { id: articleID });

Functions

Command Description example
strapi.elastic official elasticsearch package example
strapi.elastic.createOrUpdate Create to update data example
strapi.elastic.findOne Find specific data by id example
strapi.elastic.destroy delete data example
strapi.elastic.migrateById migrate data example
strapi.elastic.migrateModel migrate specific data example
strapi.elastic.models migrate all enable models example
strapi.log log data to elasticsearch example

Api

Url Method Description body
/migrate-models POST Migrate all enable Models
/migrate-Model POST Migrate specific model {model:'MODEL_NAME'}

Examples

elastic

For use official Elasticsearch package we can use strapi.elastic, and can access builtin function elasticsearch reference api

const count = strapi.elastic.count({ index: 'article' }); // https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_count

const article = strapi.elastic.get({ index: 'article', id: 1 }); // https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_get

CreateOrUpdate

const result = strapi.elastic.createOrUpdate('article', {
  id: 1,
  data: { title: 'title', content: 'content' },
});

findOne

const result = strapi.elastic.findOne('article', { id: 1 });

destroy

const result_one = strapi.elastic.destroy('article', { id: 1 });
// or
const result_two = strapi.elastic.destroy('article', { id_in: [1, 2, 3] });

migrateById

const result_one = strapi.elastic.migrateById('article', { id: 1 });

const result_two = strapi.elastic.migrateById('article', { id_in: [1, 2, 3] });

migrateModel

const result = strapi.elastic.migrateModel('article', {
  conditions, // optional
});

migrateModels

const result = strapi.elastic.migrateModels({
  conditions, // optional (the conditions apply on all models)
});

Logging

strapi use Pino to logging but can store logs or send it to elasticsearch

at now wen can send logs to elasticsearch by strapi.elastic.log there is no difference between strapi.elastic.log with strapi.log to call functions.

strapi.log.info('log message in console');
strapi.elastic.log.info('log message console and store it to elasticsearch');

strapi.log.debug('log message');
strapi.elastic.log.debug('log message console and store it to elasticsearch');

strapi.log.warn('log message');
strapi.elastic.log.warn('log message console and store it to elasticsearch');

strapi.log.error('log message');
strapi.elastic.log.error('log message console and store it to elasticsearch');

strapi.log.fatal('log message');
strapi.elastic.log.fatal('log message console and store it to elasticsearch');

Also there is some more options

// just send log to elastic and avoid to display in console
strapi.elastic.log.info('some message', { setting: { show: false } });

// just display  relations, // optional ni console and avoid to save it to elastic search
strapi.elastic.log.info('some message', { setting: { saveToElastic: false } });

// send more data to elasticsearch
const logData = { description: 'description' };
strapi.elastic.log.info('some message', logData);

By default strapi.log send some metaData to elasticsearch such as free memory, cpu load avg, current time, hostname ,...

Tricks

to avoid config plugin for all model or write a lot of code we can create cron job for migration

module.exports = {
  '*/10 * * * *': () => {
    const t = new Date()
    strapi.elastic.migrateModels({ conditions: { updated_at_gt: t.setDate(t.getMinute() - 10); } });
  },
};

✍️ Authors