Skip to content

brucewar/egg-elasticsearch

Repository files navigation

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

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •