node.js + @elastic/elasticsearch 操作elasticsearch数据库

本文介绍了如何在Node.js项目中使用Koa2框架和@elastic/elasticsearch库连接Elasticsearch8.11.1,包括设置认证、基本操作如创建、批量创建、删除、更新和查询文档,以及示例代码和查询语法说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我这边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-

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值