egg-es
TypeScript icon, indicating that this package has built-in type declarations

1.2.3 • Public • Published

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

Package Sidebar

Install

npm i egg-es

Weekly Downloads

45

Version

1.2.3

License

MIT

Unpacked Size

6.58 kB

Total Files

8

Last publish

Collaborators

  • brucewar