$ npm i egg-es --save
// {app_root}/config/plugin.js
exports.elasticsearch = {
enable: true,
package: 'egg-es',
};
// {app_root}/config/config.default.js
exports.elasticsearch = {
host: 'localhost:9200',
apiVersion: '6.3'
};
Refer to elasticsearch doc for more options;
// 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;
}
}
};
Please open an issue here.