npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zrpaplicacoes/strapi-plugin-elasticsearch

v1.4.6

Published

This package help you easily send your data to elasticsearch.

Downloads

144

Readme

latest test: v4.4.7

📝 Table of Contents

Prerequisites

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

Install plugin

  • Go to the project path

    • cd PROJECT/src/plugins
  • Clone the project

    • git submodule add https://github.com/cillaeslopes/strapi-elastic ./elastic
  • Install dependencies

    • yarn install

🏁 Getting Started

Strapi setup

In order to activate plugin, you need to add the following settings to PROJECT/config/plugins.js

module.exports = ({env}) => ({
  ...
    elastic: {
  enabled: true,
  resolve: "./src/plugins/elastic",
},
...
})
;

And you will need to activate the plugin middleware in PROJECT/config/middlewares.js

module.exports = [
  ...
    "plugin::elasticsearch.elasticMiddleware",
  ...
];

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

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/src/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",
    content
:
  "article",
    index
:
  "articles",
    enabled
:
  true,
    migration
:
  true,
    pk
:
  "id",
    relations
:
  [],
    conditions
:
  {
  }
,
  fillByResponse: false,
    supportAdminPanel
:
  true,
    urls
:
  ["/articles"],
}

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",
    content
:
  "article",
    index
:
  "articles",
    enabled
:
  true,
    migration
:
  true,
    pk
:
  "id",
    relations
:
  [],
    conditions
:
  {
  }
,
  fillByResponse: true, // changed
    supportAdminPanel
:
  true,
    urls
:
  ["/articles"],
}
,

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.


# Functions <a name="functions"></a>

| Command                         | Description                    |           example            |
| :------------------------------ | :----------------------------- | :--------------------------: |
| `strapi.$es`                | official elasticsearch package |     [example](#elastic)      |
| `strapi.$es.createOrUpdate` | Create to update data          | [example](#create_or_update) |
| `strapi.$es.findOne`        | Find specific data by id       |     [example](#findOne)      |
| `strapi.$es.destroy`        | delete data                    |     [example](#destroy)      |
| `strapi.$es.migrateById`    | migrate data                   |   [example](#migrateById)    |
| `strapi.$es.migrateModel`   | migrate specific data          |   [example](#migrateModel)   |
| `strapi.$es.models`         | migrate all enabled models     |      [example](#models)      |
| `strapi.log`                    | log data to elasticsearch      |     [example](#logging)      |

# Api <a name="api"></a>

| Url            | Method | Description            | body                   |
| :------------- | :----: | :--------------------- | ---------------------- |
| /migrate-Model |  POST  | Migrate specific model | `{model:'MODEL_NAME'}` |

# Examples <a name="example"></a>

### elastic

For use official Elasticsearch package we can use `strapi.$es`, and can access builtin function
[elasticsearch reference api](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html)

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

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

CreateOrUpdate

const result = strapi.$es.createOrUpdate("article", {
  id: 1,
  data: {title: "title", content: "content"},
});

findOne

const result = strapi.$es.findOne("article", {id: 1});

destroy

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

migrateById

const result_one = strapi.$es.migrateById("article", {id: 1});

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

migrateModel

const result = strapi.$es.migrateModel("article", {
  conditions, // optional
});

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.$es.log there is no difference between strapi.$es.log with strapi.log to call functions.

strapi.log.info("log message in console");
strapi.$es.log.info("log message console and store it to elasticsearch");

strapi.log.debug("log message");
strapi.$es.log.debug("log message console and store it to elasticsearch");

strapi.log.warn("log message");
strapi.$es.log.warn("log message console and store it to elasticsearch");

strapi.log.error("log message");
strapi.$es.log.error("log message console and store it to elasticsearch");

strapi.log.fatal("log message");
strapi.$es.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.$es.log.info("some message", {setting: {show: false}});

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

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

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

✍️ Authors