├── .eslintignore ├── .npmrc ├── .eslintrc ├── test ├── fixtures │ └── apps │ │ └── elasticsearch-test │ │ ├── package.json │ │ ├── config │ │ └── config.unittest.js │ │ └── app │ │ └── router.js └── elasticsearch.test.js ├── .gitignore ├── app.js ├── agent.js ├── index.d.ts ├── config └── config.default.js ├── .travis.yml ├── appveyor.yml ├── .autod.conf.js ├── .github ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── nodejs.yml ├── lib └── elasticsearch.js ├── LICENSE ├── package.json └── README.md /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | registry=https://registry.npmjs.org -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-egg" 3 | } 4 | -------------------------------------------------------------------------------- /test/fixtures/apps/elasticsearch-test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "elasticsearch-test", 3 | "version": "0.0.1" 4 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | npm-debug.log 3 | node_modules/ 4 | coverage/ 5 | .idea/ 6 | run/ 7 | .DS_Store 8 | *.swp 9 | .history 10 | 11 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | if (app.config.elasticsearch.app) require('./lib/elasticsearch')(app); 5 | }; 6 | -------------------------------------------------------------------------------- /agent.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = agent => { 4 | if (agent.config.elasticsearch.agent) require('./lib/elasticsearch')(agent); 5 | }; 6 | -------------------------------------------------------------------------------- /test/fixtures/apps/elasticsearch-test/config/config.unittest.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.elasticsearch = { 6 | host: 'localhost:9200', 7 | }; 8 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import * as elasticsearch from 'elasticsearch'; 2 | 3 | declare module 'egg' { 4 | // extend app 5 | interface Application { 6 | elasticsearch: typeof elasticsearch.Client 7 | } 8 | } -------------------------------------------------------------------------------- /config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * egg-elasticsearch default config 5 | * @member Config#elasticsearch 6 | */ 7 | exports.elasticsearch = { 8 | apiVersion: '7.2', 9 | app: true, 10 | agent: false, 11 | }; 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | 2 | language: node_js 3 | node_js: 4 | - '6' 5 | - '8' 6 | before_install: 7 | - npm i npminstall -g 8 | install: 9 | - npminstall 10 | script: 11 | - npm run ci 12 | after_script: 13 | - npminstall codecov && codecov 14 | -------------------------------------------------------------------------------- /test/fixtures/apps/elasticsearch-test/app/router.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | app.get('/', function* () { 5 | const ret = yield app.elasticsearch.search({ 6 | q: 'pants', 7 | }); 8 | this.body = ret; 9 | }); 10 | }; 11 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: '6' 4 | - nodejs_version: '8' 5 | 6 | install: 7 | - ps: Install-Product node $env:nodejs_version 8 | - npm i npminstall && node_modules\.bin\npminstall 9 | 10 | test_script: 11 | - node --version 12 | - npm --version 13 | - npm run test 14 | 15 | build: off 16 | -------------------------------------------------------------------------------- /.autod.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | write: true, 5 | prefix: '^', 6 | plugin: 'autod-egg', 7 | test: [ 8 | 'test', 9 | 'benchmark', 10 | ], 11 | devdep: [ 12 | 'egg', 13 | 'egg-ci', 14 | 'egg-bin', 15 | 'autod', 16 | 'autod-egg', 17 | 'eslint', 18 | 'eslint-config-egg', 19 | 'supertest', 20 | 'webstorm-disable-index', 21 | ], 22 | exclude: [ 23 | './test/fixtures', 24 | './docs', 25 | './coverage', 26 | ], 27 | }; 28 | -------------------------------------------------------------------------------- /test/elasticsearch.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const request = require('supertest'); 4 | const mm = require('egg-mock'); 5 | 6 | describe('test/elasticsearch.test.js', () => { 7 | let app; 8 | before(() => { 9 | app = mm.app({ 10 | baseDir: 'apps/elasticsearch-test', 11 | }); 12 | return app.ready(); 13 | }); 14 | 15 | after(() => app.close()); 16 | afterEach(mm.restore); 17 | 18 | it('should GET /', () => { 19 | return request(app.callback()) 20 | .get('/') 21 | .expect(200); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 10 | 11 | ##### Checklist 12 | 13 | 14 | - [ ] `npm test` passes 15 | - [ ] tests and/or benchmarks are included 16 | - [ ] documentation is changed or added 17 | - [ ] commit message follows commit guidelines 18 | 19 | ##### Affected core subsystem(s) 20 | 21 | 22 | 23 | ##### Description of change 24 | 25 | -------------------------------------------------------------------------------- /lib/elasticsearch.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const elasticsearch = require('elasticsearch'); 5 | 6 | module.exports = app => { 7 | const config = app.config.elasticsearch; 8 | assert(config.host || config.hosts, '[egg-elasticsearch] \'host\' or \'hosts\' is required on config'); 9 | 10 | app.coreLogger.info('[egg-elasticsearch] connecting elasticsearch server'); 11 | 12 | const client = new elasticsearch.Client(config); 13 | 14 | Object.defineProperty(app, 'elasticsearch', { 15 | value: client, 16 | writable: false, 17 | configurable: false, 18 | }); 19 | 20 | app.beforeStart(async () => { 21 | try { 22 | await client.ping({ requestTimeout: 30000 }); 23 | app.coreLogger.info('[egg-elasticsearch] elasticsearch connects successfully'); 24 | } catch (err) { 25 | app.coreLogger.error('[egg-elasticsearch] elasticsearch cluster is down with error: ' + err); 26 | } 27 | }); 28 | }; 29 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Alibaba Group Holding Limited and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: Node.js CI 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | schedule: 12 | - cron: '0 2 * * *' 13 | 14 | jobs: 15 | build: 16 | runs-on: ${{ matrix.os }} 17 | 18 | strategy: 19 | fail-fast: false 20 | matrix: 21 | node-version: [6, 8] 22 | os: [ubuntu-latest, windows-latest, macos-latest] 23 | 24 | steps: 25 | - name: Checkout Git Source 26 | uses: actions/checkout@v2 27 | 28 | - name: Use Node.js ${{ matrix.node-version }} 29 | uses: actions/setup-node@v1 30 | with: 31 | node-version: ${{ matrix.node-version }} 32 | 33 | - name: Install Dependencies 34 | run: npm i -g npminstall && npminstall 35 | 36 | - name: Continuous Integration 37 | run: npm run ci 38 | 39 | - name: Code Coverage 40 | uses: codecov/codecov-action@v1 41 | with: 42 | token: ${{ secrets.CODECOV_TOKEN }} 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egg-es", 3 | "version": "1.2.4", 4 | "description": "elasticsearch client", 5 | "eggPlugin": { 6 | "name": "elasticsearch" 7 | }, 8 | "keywords": [ 9 | "egg", 10 | "eggPlugin", 11 | "egg-plugin", 12 | "elasticsearch" 13 | ], 14 | "dependencies": { 15 | "elasticsearch": "^16.7.1" 16 | }, 17 | "devDependencies": { 18 | "autod": "^3.0.1", 19 | "autod-egg": "^1.1.0", 20 | "egg": "^2.10.0", 21 | "egg-bin": "^4.8.1", 22 | "egg-ci": "^1.8.0", 23 | "egg-mock": "^3.19.2", 24 | "eslint": "^5.3.0", 25 | "eslint-config-egg": "^7.0.0", 26 | "supertest": "^3.1.0", 27 | "webstorm-disable-index": "^1.2.0" 28 | }, 29 | "engines": { 30 | "node": ">=6.0.0" 31 | }, 32 | "scripts": { 33 | "test": "npm run lint -- --fix && npm run pkgfiles && npm run test-local", 34 | "test-local": "egg-bin test", 35 | "cov": "egg-bin cov", 36 | "lint": "eslint .", 37 | "ci": "npm run pkgfiles --check && npm run lint && npm run cov", 38 | "pkgfiles": "egg-bin pkgfiles", 39 | "autod": "autod" 40 | }, 41 | "files": [ 42 | "config", 43 | "agent.js", 44 | "lib", 45 | "app.js", 46 | "index.d.ts" 47 | ], 48 | "ci": { 49 | "version": "6, 8" 50 | }, 51 | "repository": { 52 | "type": "git", 53 | "url": "https://github.com/brucewar/egg-elasticsearch" 54 | }, 55 | "bugs": { 56 | "url": "https://github.com/brucewar/egg-elasticsearch/issues" 57 | }, 58 | "homepage": "https://github.com/brucewar/egg-elasticsearch#readme", 59 | "author": "brucewar ", 60 | "license": "MIT" 61 | } 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # egg-es 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![build status][travis-image]][travis-url] 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![David deps][david-image]][david-url] 7 | [![Known Vulnerabilities][snyk-image]][snyk-url] 8 | [![npm download][download-image]][download-url] 9 | 10 | [npm-image]: https://img.shields.io/npm/v/egg-es.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/egg-es 12 | [travis-image]: https://img.shields.io/travis/eggjs/egg-es.svg?style=flat-square 13 | [travis-url]: https://travis-ci.org/eggjs/egg-es 14 | [codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-es.svg?style=flat-square 15 | [codecov-url]: https://codecov.io/github/eggjs/egg-es?branch=master 16 | [david-image]: https://img.shields.io/david/eggjs/egg-es.svg?style=flat-square 17 | [david-url]: https://david-dm.org/eggjs/egg-es 18 | [snyk-image]: https://snyk.io/test/npm/egg-es/badge.svg?style=flat-square 19 | [snyk-url]: https://snyk.io/test/npm/egg-es 20 | [download-image]: https://img.shields.io/npm/dm/egg-es.svg?style=flat-square 21 | [download-url]: https://npmjs.org/package/egg-es 22 | 23 | 26 | 27 | ## Install 28 | 29 | ```bash 30 | $ npm i egg-es --save 31 | ``` 32 | 33 | ## Usage 34 | 35 | ```js 36 | // {app_root}/config/plugin.js 37 | exports.elasticsearch = { 38 | enable: true, 39 | package: 'egg-es', 40 | }; 41 | ``` 42 | 43 | ## Configuration 44 | 45 | ```js 46 | // {app_root}/config/config.default.js 47 | exports.elasticsearch = { 48 | host: 'localhost:9200', 49 | apiVersion: '6.3' 50 | }; 51 | ``` 52 | 53 | Refer to [elasticsearch doc](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html) for more options; 54 | 55 | ## Example 56 | 57 | 58 | ```js 59 | // app/controller/post.js 60 | module.exports = app => { 61 | return class PostController extends app.Controller { 62 | async index(){ 63 | const pageNum = this.ctx.params.page; 64 | const perPage = this.ctx.params.per_page; 65 | const userQuery = this.ctx.request.body.search_query; 66 | const userId = this.ctx.session.userId; 67 | const posts = await app.elasticsearch.search({ 68 | index: 'posts', 69 | from: (pageNum - 1) * perPage, 70 | size: perPage, 71 | body: { 72 | query: { 73 | filtered: { 74 | query: { 75 | match: { 76 | _all: userQuery 77 | } 78 | }, 79 | filter: { 80 | or: [ 81 | { 82 | term: { privacy: 'public' } 83 | }, { 84 | term: { owner: userId } 85 | } 86 | ] 87 | } 88 | } 89 | } 90 | } 91 | }); 92 | this.ctx.body = posts; 93 | } 94 | } 95 | }; 96 | ``` 97 | 98 | ## Questions & Suggestions 99 | 100 | Please open an issue [here](https://github.com/eggjs/egg/issues). 101 | 102 | ## License 103 | 104 | [MIT](LICENSE) 105 | --------------------------------------------------------------------------------