插件:https://www.npmjs.com/package/egg-es
安装:$cnpm i egg-es --save
配置插件
//{app_root}/config/plugin.js
exports.elasticsearch = {
enable: true,
package: 'egg-es',
};
配置 Elasticsearch 连接地址
//{app_root}/config/config.default.js
// 注意这里的版本,此插件支持:7.0, _default, 6.7, 6.6, 6.5, 6.4, 6.3, 6.2, 6.1, 6.0, 5.6, 2.4, 1.7, 0.90, 6.8, 7.1, 7.x, master
// 因此不能使用7.7的版本
exports.elasticsearch = {
host: 'localhost:9200',
apiVersion: '7.x'
};
加入测试路由
router.get('/search', controller.web.search.index);
编写相应的测试控制器
'use strict';
const Controller = require('egg').Controller;
class SearchController extends Controller {
// 在增加商品的时候,需要把数据同步到es,搜索的时候接入es, 这样搜索会更快
// 下面的news索引是之前建好的,可以结合elasticsearch-heard来测试
async index() {
// 测试的时候,逐个代码展开测试 如下:
//增加数据
// await this.add();
//修改数据
// await this.edit();
// 搜索数据
// await this.query();
// 分页搜索数据
// await this.pager();
// 统计数据
// await this.count();
// 删除数据
// await this.delete();
}
async add() {
let addResult = await this.app.elasticsearch.bulk({
body: [
{ index: { _index: 'news', _id: '111111111111111111' } },
{ content: 'es-add-test' }
]
});
console.log('add: ', addResult);
this.ctx.body = 'add test';
}
async edit() {
let editResult = await this.app.elasticsearch.bulk({
body: [
{ update: { _index: 'news', _id: '111111111111111111' } },
{ doc: { content: '使用ARM芯片的Mac将可能在2020年推出666' } },
]
});
console.log('editResult: ', editResult);
this.ctx.body = 'update test';
}
async query() {
// 相关文档:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/7.x/search_examples.html
let result = await this.app.elasticsearch.search({
index: 'news',
body: {
query: {
match: {
content: '666'
}
}
}
});
console.log('queryResult: ', JSON.stringify(result));
this.ctx.body = 'query test';
}
async pager() {
let page = 1;
let pageSize = 2;
let result = await this.app.elasticsearch.search({
index: 'news',
from: (page - 1) * pageSize, //skip
size: pageSize,
body: {
query: {
match: {
content: '666'
}
}
}
});
console.log('pagerResult: ', JSON.stringify(result));
this.ctx.body = 'pager test';
}
async count() {
let result = await this.app.elasticsearch.count({
index: 'news',
type: 'doc',
body: {
query: {
match: {
content: '666'
}
}
}
});
console.log(result);
this.ctx.body = 'count test';
}
async delete() {
let result = await this.app.elasticsearch.bulk({
body: [
{ delete: { _index: 'news', _id: '111111111111111111' } },
]
});
console.log('deleteResult: ', result);
this.ctx.body = 'delete test';
}
}
module.exports = SearchController;