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

2024-04-25 18:04

本文主要是介绍node.js + @elastic/elasticsearch 操作elasticsearch数据库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

我这边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-es’命名
在这里插入图片描述

2)批量创建

await this.elastic.indices.create({index: 'tweets',operations: {mappings: {properties: {id: { type: 'integer' },text: { type: 'text' },user: { type: 'keyword' },time: { type: 'date' }}}}
}, { ignore: [400] })
const dataset = [{id: 1,text: 'If I fall, don\'t bring me back.',user: 'jon',time: new Date()},{id: 2,text: 'Winter is coming',user: 'ned',time: new Date()},{id: 3,text: 'A Lannister always pays his debts.',user: 'tyrion',time: new Date()},{id: 4,text: 'I am the blood of the dragon.',user: 'daenerys',time: new Date()},{id: 5, // change this value to a string to see the bulk response with errorstext: 'A girl is Arya Stark of Winterfell. And I\'m going home.',user: 'arya',time: new Date()}
]const operations = dataset.flatMap(doc => [{ index: { _index: 'tweets' } }, doc])const bulkResponse = await this.elastic.bulk({ refresh: true, operations })
const count = await this.elastic.count({ index: 'tweets' })
console.log(count)

等同于

await this.elastic.bulk({refresh: true,operations: [// operation to perform{ index: { _index: 'game-of-thrones' } },// the document to index{character: 'Ned Stark1',quote: 'Winter is coming1.'},{ index: { _index: 'game-of-thrones' } },{character: 'Daenerys Targaryen2',quote: 'I am the blood of the dragon2.'},{ index: { _index: 'game-of-thrones' } },{character: 'Tyrion Lannister3',quote: 'A mind needs books like a sword needs a whetstone3.'}]
});

根据官网来看,bulk其实就是批量操作,这里也可以 update、delete 等等
在这里插入图片描述

2、删除
1)删除单个

this.elastic.delete({index: 'testes',id: '20240425-01'
});

2)按条件删除,会删除符合条件的所有数据

this.elastic.deleteByQuery({index: 'tweets',query: {match: {user: 'tyrion'}}
})

在这里插入图片描述

3、更新操作

await this.elastic.update({index: 'tweets',id: 'QK5KE48BTvD5VO_sox9p',doc: {text: '111',user: '222'}
});
const document = await this.elastic.get({index: 'tweets',id: 'QK5KE48BTvD5VO_sox9p'
});

在这里插入图片描述

在这里插入图片描述

4、查询操作

1)单个查询,根据 index 和 id精准查询

const document = await this.elastic.get({index: 'tweets',id: 'QK5KE48BTvD5VO_sox9p'
});

2)查询所有,查询 index 为 testes 的所有数值

this.elastic.search({index: 'testes'
})

3)search 混合查询

GET /cartest/_search
{"size": 10,"from": 0,"query":{"bool":{"must":[{"match":{"say": "33333"}},{"regexp": {"name": ".*云.*" }},{"range": {"num": {"gte": 120,"lte": 200}}}]}}
}

在这里插入图片描述

具体查询大家可以看我这个文章:query 中的内容大致都是一致的

https://blog.csdn.net/weixin_44384273/article/details/137920183?spm=1001.2014.3001.5501

这篇关于node.js + @elastic/elasticsearch 操作elasticsearch数据库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/935399

相关文章

RedHat运维-Linux文本操作基础-AWK进阶

你不用整理,跟着敲一遍,有个印象,然后把它保存到本地,以后要用再去看,如果有了新东西,你自个再添加。这是我参考牛客上的shell编程专项题,只不过换成了问答的方式而已。不用背,就算是我自己亲自敲,我现在好多也记不住。 1. 输出nowcoder.txt文件第5行的内容 2. 输出nowcoder.txt文件第6行的内容 3. 输出nowcoder.txt文件第7行的内容 4. 输出nowcode

关于如何更好管理好数据库的一点思考

本文尝试从数据库设计理论、ER图简介、性能优化、避免过度设计及权限管理方面进行思考阐述。 一、数据库范式 以下通过详细的示例说明数据库范式的概念,将逐步规范化一个例子,逐级说明每个范式的要求和变换过程。 示例:学生课程登记系统 初始表格如下: 学生ID学生姓名课程ID课程名称教师教师办公室1张三101数学王老师101室2李四102英语李老师102室3王五101数学王老师101室4赵六103物理陈

数据库期末复习知识点

A卷 1. 选择题(30') 2. 判断范式(10') 判断到第三范式 3. 程序填空(20') 4. 分析填空(15') 5. 写SQL(25') 5'一题 恶性 B卷 1. 单选(30') 2. 填空 (20') 3. 程序填空(20') 4. 写SQL(30') 知识点 第一章 数据库管理系统(DBMS)  主要功能 数据定义功能 (DDL, 数据定义语

给数据库的表添加字段

周五有一个需求是这样的: 原来数据库有一个表B,现在需要添加一个字段C,我把代码中增删改查部分进行了修改, 比如insert中也添入了字段C。 但没有考虑到一个问题,数据库的兼容性。因为之前的版本已经投入使用了,再升级的话,需要进行兼容处理,当时脑子都蒙了,转不过来,后来同事解决了这个问题。 现在想想,思路就是,把数据库的表结构存入文件中,如xxx.sql 实时更新该文件: CREAT

js+css二级导航

效果 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con

SQL Server中,查询数据库中有多少个表,以及数据库其余类型数据统计查询

sqlserver查询数据库中有多少个表 sql server 数表:select count(1) from sysobjects where xtype='U'数视图:select count(1) from sysobjects where xtype='V'数存储过程select count(1) from sysobjects where xtype='P' SE

SQL Server中,always on服务器的相关操作

在SQL Server中,建立了always on服务,可用于数据库的同步备份,当数据库出现问题后,always on服务会自动切换主从服务器。 例如192.168.1.10为主服务器,12为从服务器,当主服务器出现问题后,always on自动将主服务器切换为12,保证数据库正常访问。 对于always on服务器有如下操作: 1、切换主从服务器:假如需要手动切换主从服务器时(如果两个服务

SQL Server中,添加数据库到AlwaysOn高可用性组条件

1、将数据添加到AlwaysOn高可用性组,需要满足以下条件: 2、更多具体AlwaysOn设置,参考:https://msdn.microsoft.com/zh-cn/library/windows/apps/ff878487(v=sql.120).aspx 注:上述资源来自MSDN。

SQL Server中,用Restore DataBase把数据库还原到指定的路径

restore database 数据库名 from disk='备份文件路径' with move '数据库文件名' to '数据库文件放置路径', move '日志文件名' to '日志文件存放置路径' Go 如: restore database EaseWe from disk='H:\EaseWe.bak' with move 'Ease

探索Elastic Search:强大的开源搜索引擎,详解及使用

🎬 鸽芷咕:个人主页  🔥 个人专栏: 《C++干货基地》《粉丝福利》 ⛺️生活的理想,就是为了理想的生活! 引入 全文搜索属于最常见的需求,开源的 Elasticsearch (以下简称 Elastic)是目前全文搜索引擎的首选,相信大家多多少少的都听说过它。它可以快速地储存、搜索和分析海量数据。就连维基百科、Stack Overflow、