本人根据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操作数据相关推荐

  1. 在 Java 应用程序中使用 Elasticsearch: 高性能 RESTful 搜索引擎和文档存储快速入门指南

    如果您使用过 Apache Lucene 或 Apache Solr,就会知道它们的使用体验非常有趣.尤其在您需要扩展基于 Lucene 或 Solr 的解决方案时,您就会了解 Elasticsear ...

  2. elastic search java_在 Java 应用程序中使用 Elasticsearch

    如果您使用过 Apache Lucene 或 Apache Solr,就会知道它们的使用体验非常有趣.尤其在您需要扩展基于 Lucene 或 Solr 的解决方案时,您就会了解 Elasticsear ...

  3. 如何在Go中实现Elasticsearch

    Today, I am going to show you how to implement Elasticsearch in Go.But of course, before that I am g ...

  4. 218.94.78.76:20001/index.php,详解spring中使用Elasticsearch的实例教程

    本篇文章主要介绍了详解spring中使用Elasticsearch的代码实现,具有一定的参考价值,有兴趣的可以了解一下 在使用Elasticsearch之前,先给大家聊一点干货. 1. ES和solr ...

  5. python snap7 简书_第14篇-Python中的Elasticsearch入门

    我的Elasticsearch系列文章,逐渐更新中,欢迎关注 另外Elasticsearch入门,我强烈推荐ElasticSearch搭建手册给你,非常想尽的入门指南手册. 在本文中,我将讨论Elas ...

  6. cerebro管理工具中添加elasticsearch字段

    cerebro管理工具中添加elasticsearch字段 查看文档mapping 7.*版本的ES查看或者操作需要再url之后增加?include_type_name=true 向已有的type中新 ...

  7. Nestjs中的守卫

    NestJs中的守卫(guards)概念 文档:https://docs.nestjs.com/guards 守卫是一个使用 @Injectable()装饰器装饰的类,并且实现了CanActivate ...

  8. 【ES】CURL在windows中对ElasticSearch的一些简单的操作

    上一节说了CURL在windows中的安装方式,现在就简单的记录下CURL在windows中对ElasticSearch的一些简单的操作 直接上操作命令 首先我们打开cmd命令行 输入curl -he ...

  9. 在kibana中查询elasticsearch数据的方法(lucene和kql语法)

    kibana中查询elasticsearch数据的方法 1.  Lucene查询语法 Kibana查询语言基于Lucene查询语法. 为了执行一个文本搜索,可以简单的输入一个文本字符串.例如,如果你想 ...

最新文章

  1. 读书笔记 effective c++ Item 5 了解c++默认生成并调用的函数
  2. 钢结构节点输出软件_BIM助力桥梁钢结构设计施工一体化建设
  3. 转载——CVE-2019-0807
  4. 微信小程序复制到剪切板及换行问题
  5. 什么是 NIO? NIO 和 BIO、AIO 之间的区别是什么?NIO主要用来解决什么问题?
  6. python web框架 - Django
  7. Swift - 动画效果的实现方法总结(附样例)
  8. python未知数的矩阵运算,机器学习的数学 之python矩阵运算
  9. Android开机动画的动态替换
  10. AI数学基础之:P、NP、NPC问题
  11. vbs整人代码,表白+提醒 两段代码就OK
  12. 做了一款DTU,网络通信模块化,代码开源,可以二次开发
  13. [渝粤教育] 西安建筑科技大学 环境规划与管理 参考 资料
  14. 【kafka】kafka windows Invalid UTF-8 middle byte 0xfe
  15. R语言 kNN 对鸢尾花进行分类
  16. 计算机论文中的技术路线,毕业论文开题报告中技术路线怎么写
  17. js超简单实现图片旋转
  18. 绝地求生大逃杀常用英语
  19. python时间序列分析包_python关于时间序列的分析
  20. 4年前端狗,面试被虐,如何翻身?

热门文章

  1. python做正态分布的例子_Python求正态分布曲线下面积实例
  2. UI自动化测试工具探索:Airtest
  3. python五子棋小游戏程序源码
  4. 华为交换机vlan间arp代理
  5. 【工业大数据】工业大数据真正要做的是智能分析和智能决策
  6. python熊猫弹幕_Python爬取pandaTV弹幕
  7. 跨域MPLS Option C1
  8. 汽车悬挂系统的现代控制分析(现代控制理论课程小论文)
  9. 全国计算机一级office2010,全国计算机等级考试一级MS Office2010教程
  10. vue中使用element-tiptap