1.安装 egg-es
npm i egg-es --save
2.建一个Controller 做资料新增
注意 my-index-005 这是ES内的index名称 , 假设为 my-index-005
const Controller = require('egg').Controller;
const elasticsearch = require('elasticsearch');
class EstestController extends Controller {
async add() {
var esClient = new elasticsearch.Client({
host: 127.0.0.1:9002,
log: 'info'
});
let result = await esClient.bulk({
body: [
{ index: { _index: 'my-index-005', _id: '10002245' } },
{ content: '测试写入内容' }
]
});
this.ctx.body = result;
}
}
module.exports = EstestController;
3.资料更新
async edit() {
let result = await esClient.bulk({
body: [
{ update: { _index: 'my-index-005', _id: '10002245' } },
{ doc: { content: '更新後的内容' } },
]
});
this.ctx.body = result;
}
4.搜寻
async query() {
let result = await esClient.search({
index: 'news',
body: {
query: {
match: {
content: '欲查询的内容'
}
}
}
});
console.log('result: ', JSON.stringify(result));
}
5.删除
async delete() {
let result = await esClient.bulk({
body: [
{ delete: { _index: 'my-index-005', _id: '10002245' } },
]
});
}
完成便可以到kibana确认
>>: [PoEAA] Data Source Architectural Pattern - Row Data Gateway
人的科技文明发展始终来自於人性 在现今的科技加速之下,所有的一切都将因为有了网路而有所不同,也因为在...
前言 以前很经常使用 GitHub 上的各种套件, 不管是大到页面,小到按钮,深到语法,都喜欢用自己...
Trello 作为专业的专案管理软件,在开源的世界中也会随之诞生一些类似操作的工具。今天要简介的 W...
因为心情太低落了,掉着泪看教学,我真的很认真的在学,但为什麽要被批评得一无是处。 也许在学技能的同时...
今日学习目标 常见的交叉验证方法 K-fold Leave one out cross valida...