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 errors
    text: '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

相关推荐
努力成为一个程序猿.5 分钟前
【Flink】FlinkSQL-动态表和持续查询概念
大数据·数据库·flink
毕设十刻41 分钟前
基于Vue的学分预警系统98k51(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
cdming1 小时前
Node.js 解释环境变量的定义、作用及在Node.js中的重要性,区分开发、测试、生产环境配置需求。
node.js
liliangcsdn2 小时前
如何利用约束提示优化LLM在问题转sql的一致性
数据库·sql
Java爱好狂.2 小时前
分布式ID|从源码角度深度解析美团Leaf双Buffer优化方案
java·数据库·分布式·分布式id·es·java面试·java程序员
Elastic 中国社区官方博客2 小时前
通过混合搜索重排序提升多语言嵌入模型的相关性
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
倔强的石头1063 小时前
KingbaseES:从兼容到超越,详解超越MySQL的权限隔离与安全增强
数据库·mysql·安全·金仓数据库
小鸡毛程序员3 小时前
我在CSDN学MYSQL之----数据库基本概念和基本知识(下)
数据库·mysql
liliangcsdn4 小时前
如何使用elasticdump进行elasticsearch数据还原
大数据·elasticsearch·搜索引擎
程序定小飞4 小时前
基于springboot的web的音乐网站开发与设计
java·前端·数据库·vue.js·spring boot·后端·spring