我这边node.js 使用的是 koa2,elasticsearch是8.11.1版本
官网:https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/getting-started-js.html
一、@elastic/elasticsearch 连接 elasticsearch数据库
如果elasticsearch没有设置账号秘密 则auth就不需要了
const { Client } = require('@elastic/elasticsearch');
this.elastic = new Client({
node: 'http://localhost:9200',
auth: {
username: 'xm',
password: '123'
}
});
// 另一个写法
const elastic = new Client({
node: 'https://username:password@localhost:9200'
})
二、基础操作
1、创建
1)创建一条信息
this.elastic.index({
index: 'testes',
id: '20240425-01',
body: {
name: 'xiaoming',
age: 22
}
})
创建一条信息,id不填 系统会自动补全
注意:index 只能是小写,不能大写,否则会报错,可以 ‘test-