Nestjs中使用ElasticSearch操作数据
本人根据github
上提供的案例中文分词地址,带大家用页面方式来操作,如果你还没配置好环境,可以参考我之前写的文章连接地址
一、基本使用
1、启动
docker
容器2、浏览器中输入
http://localhost:9100/
访问地址3、创建一个
index
索引(类似创建一个数据库)4、插入数据
5、查询全部的数据
注意要写
_search
并且内容体中不能有内容体
6、根据
_id
修改数据内容体中你写什么就存入什么数据
7、根据条件模糊查询数据
二、在nestjs
中集成ElasticSearch
1、参考官网地址
2、创建一个
nestjs
项目3、安装依赖包
npm i --save @nestjs/elasticsearch @elastic/elasticsearch
4、在
App.module.ts
中引入ElasticsearchModule
模块,或者可以和官网一样的单独创建一个模块,然后在根模块中引入import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { ElasticsearchModule } from '@nestjs/elasticsearch';@Module({imports: [ElasticsearchModule.register({node: 'http://localhost:9200',})],controllers: [AppController],providers: [AppService], }) export class AppModule {}
5、可以直接在服务层中依赖注入
ElasticsearchService
然后对数据的增删改查,这里简单的使用,实际开发中要更加正规点,仅仅为了演示如何在nestjs
中操作import { Injectable } from '@nestjs/common'; import { ElasticsearchService } from '@nestjs/elasticsearch';@Injectable() export class AppService {constructor(private readonly elasticsearchService: ElasticsearchService) {}// 添加、修改、删除数据async bulk(params: any) {return await this.elasticsearchService.bulk(params);}// 查询数据async search(params: any) {return await this.elasticsearchService.search(params);} }
6、添加数据到
ElasticSearch
async bulk() {return await this.elasticsearchService.bulk({body: [// 指定的数据库为news, 指定的Id = 1{ index: { _index: 'news', _type: 'doc', _id: '1' } }, { content: '模拟数据插入' }]});}
7、删除数据(根据
id
删除数据)async bulk() {return await this.elasticsearchService.bulk({body: [{ delete: { _index: 'news', _type: 'doc', _id: 'UZAgiHgBS54NjNlgPg5j' } },]}); }
8、修改数据
async bulk() {return await this.elasticsearchService.bulk({body: [{ update: { _index: 'news', _type: 'doc', _id: 'UpAhiHgBS54NjNlgfA6i' } },// 仅仅是修改你改的字段,之前有的字段不会被删除{ doc: { content: '我是被修改的数据' } },]}); }
9、查询数据(模糊查询)
async search() {return await this.elasticsearchService.search({index: 'news', type: 'doc', body: {query: {match: {content: '中国', // 模糊查询,有点类型正则中的match}}}});}
10、分页查询数据
async search() {const pageSize = 10;const pageNumber = 1return await this.elasticsearchService.search({index: 'news', type: 'doc', body: {from: (pageNumber - 1) * pageSize, // 从哪里开始size: pageSize, // 查询条数query: {match: {content: '中国' // 搜索查询到的内容}}}}); }
11、查询数量
async count() {return await this.elasticsearchService.count({index: 'news',type: 'doc', body: {query: {match: {content: '中国'}}}}); }
12、参考API
Nestjs中使用ElasticSearch操作数据相关推荐
- 在 Java 应用程序中使用 Elasticsearch: 高性能 RESTful 搜索引擎和文档存储快速入门指南
如果您使用过 Apache Lucene 或 Apache Solr,就会知道它们的使用体验非常有趣.尤其在您需要扩展基于 Lucene 或 Solr 的解决方案时,您就会了解 Elasticsear ...
- elastic search java_在 Java 应用程序中使用 Elasticsearch
如果您使用过 Apache Lucene 或 Apache Solr,就会知道它们的使用体验非常有趣.尤其在您需要扩展基于 Lucene 或 Solr 的解决方案时,您就会了解 Elasticsear ...
- 如何在Go中实现Elasticsearch
Today, I am going to show you how to implement Elasticsearch in Go.But of course, before that I am g ...
- 218.94.78.76:20001/index.php,详解spring中使用Elasticsearch的实例教程
本篇文章主要介绍了详解spring中使用Elasticsearch的代码实现,具有一定的参考价值,有兴趣的可以了解一下 在使用Elasticsearch之前,先给大家聊一点干货. 1. ES和solr ...
- python snap7 简书_第14篇-Python中的Elasticsearch入门
我的Elasticsearch系列文章,逐渐更新中,欢迎关注 另外Elasticsearch入门,我强烈推荐ElasticSearch搭建手册给你,非常想尽的入门指南手册. 在本文中,我将讨论Elas ...
- cerebro管理工具中添加elasticsearch字段
cerebro管理工具中添加elasticsearch字段 查看文档mapping 7.*版本的ES查看或者操作需要再url之后增加?include_type_name=true 向已有的type中新 ...
- Nestjs中的守卫
NestJs中的守卫(guards)概念 文档:https://docs.nestjs.com/guards 守卫是一个使用 @Injectable()装饰器装饰的类,并且实现了CanActivate ...
- 【ES】CURL在windows中对ElasticSearch的一些简单的操作
上一节说了CURL在windows中的安装方式,现在就简单的记录下CURL在windows中对ElasticSearch的一些简单的操作 直接上操作命令 首先我们打开cmd命令行 输入curl -he ...
- 在kibana中查询elasticsearch数据的方法(lucene和kql语法)
kibana中查询elasticsearch数据的方法 1. Lucene查询语法 Kibana查询语言基于Lucene查询语法. 为了执行一个文本搜索,可以简单的输入一个文本字符串.例如,如果你想 ...
最新文章
- 读书笔记 effective c++ Item 5 了解c++默认生成并调用的函数
- 钢结构节点输出软件_BIM助力桥梁钢结构设计施工一体化建设
- 转载——CVE-2019-0807
- 微信小程序复制到剪切板及换行问题
- 什么是 NIO? NIO 和 BIO、AIO 之间的区别是什么?NIO主要用来解决什么问题?
- python web框架 - Django
- Swift - 动画效果的实现方法总结(附样例)
- python未知数的矩阵运算,机器学习的数学 之python矩阵运算
- Android开机动画的动态替换
- AI数学基础之:P、NP、NPC问题
- vbs整人代码,表白+提醒 两段代码就OK
- 做了一款DTU,网络通信模块化,代码开源,可以二次开发
- [渝粤教育] 西安建筑科技大学 环境规划与管理 参考 资料
- 【kafka】kafka windows Invalid UTF-8 middle byte 0xfe
- R语言 kNN 对鸢尾花进行分类
- 计算机论文中的技术路线,毕业论文开题报告中技术路线怎么写
- js超简单实现图片旋转
- 绝地求生大逃杀常用英语
- python时间序列分析包_python关于时间序列的分析
- 4年前端狗,面试被虐,如何翻身?