项目作者: brucewar

项目描述 :
elasticsearch client for egg.js
高级语言: JavaScript
项目地址: git://github.com/brucewar/egg-elasticsearch.git
创建时间: 2017-06-23T09:47:36Z
项目社区:https://github.com/brucewar/egg-elasticsearch

开源协议:MIT License

下载


egg-es

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

Install

  1. $ npm i egg-es --save

Usage

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

Configuration

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

Refer to elasticsearch doc for more options;

Example

  1. // app/controller/post.js
  2. module.exports = app => {
  3. return class PostController extends app.Controller {
  4. async index(){
  5. const pageNum = this.ctx.params.page;
  6. const perPage = this.ctx.params.per_page;
  7. const userQuery = this.ctx.request.body.search_query;
  8. const userId = this.ctx.session.userId;
  9. const posts = await app.elasticsearch.search({
  10. index: 'posts',
  11. from: (pageNum - 1) * perPage,
  12. size: perPage,
  13. body: {
  14. query: {
  15. filtered: {
  16. query: {
  17. match: {
  18. _all: userQuery
  19. }
  20. },
  21. filter: {
  22. or: [
  23. {
  24. term: { privacy: 'public' }
  25. }, {
  26. term: { owner: userId }
  27. }
  28. ]
  29. }
  30. }
  31. }
  32. }
  33. });
  34. this.ctx.body = posts;
  35. }
  36. }
  37. };

Questions & Suggestions

Please open an issue here.

License

MIT