Socket
Book a DemoInstallSign in
Socket

egg-es

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

egg-es

elasticsearch client

latest
Source
npmnpm
Version
1.2.4
Version published
Maintainers
1
Created
Source

egg-es

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-es --save

Usage

// {app_root}/config/plugin.js
exports.elasticsearch = {
  enable: true,
  package: 'egg-es',
};

Configuration

// {app_root}/config/config.default.js
exports.elasticsearch = {
  host: 'localhost:9200',
  apiVersion: '6.3'
};

Refer to elasticsearch doc for more options;

Example

// app/controller/post.js
module.exports = app => {
  return class PostController extends app.Controller {
    async index(){
      const pageNum = this.ctx.params.page;
      const perPage = this.ctx.params.per_page;
      const userQuery = this.ctx.request.body.search_query;
      const userId = this.ctx.session.userId;
      const posts = await app.elasticsearch.search({
        index: 'posts',
        from: (pageNum - 1) * perPage,
        size: perPage,
        body: {
          query: {
            filtered: {
              query: {
                match: {
                  _all: userQuery
                }
              },
              filter: {
                or: [
                  {
                    term: { privacy: 'public' }
                  }, {
                    term: { owner: userId }
                  }
                ]
              }
            }
          }
        }
      });
      this.ctx.body = posts;
    }
  }
};

Questions & Suggestions

Please open an issue here.

License

MIT

Keywords

egg

FAQs

Package last updated on 17 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts