查询

根据索引、类型、id进行查询:

client.get({ index:'myindex', type:'mytype', id:1
},function(error, response){// ...});

根据某个查询条件,查询某个索引的所有数据

client.search({ index:'myindex', q:'title:test'
},function(error, response){// ...});

复杂一点的查询:

client.search({
     index:'myindex',
     body:{
         query:{
             match:{
                 title:'test' } },
         facets:{
             tags:{
                 terms:{
                     field:'tags' } } } }
},function(error, response){// ...});

新增

新增时,需要指定索引,类型,和id,还有保存的内容:

client.create({ index:'myindex', type:'mytype', id:'1', body:{ title:'Test 1', tags:['y','z'], published:true, published_at:'2013-01-01', counter:1 }
},function(error, response){// ...});

删除

按照索引,类型和id删除:

client.delete({ index:'myindex', type:'mytype', id:'1'
},function(error, response){// ...});

修改

修改操作通常使用update方法:

client.update({ index:'myindex', type:'mytype', id:'1', body:{ // put the partial document under the `doc` key doc:{ title:'Updated' } }
},function(error, response){// ...})

一次性执行多个操作

ESClient也支持一次性执行多个操作:

client.mget({
     body:{
         docs:[ {
            _index:'indexA', _type:'typeA', _id:'1' },{
            _index:'indexB', _type:'typeB', _id:'1' },{
            _index:'indexC', _type:'typeC', _id:'1' }] }
},function(error, response){// ...});

也支持下面的风格:

client.mget({ index:'myindex', type:'mytype', body:{ ids:[1,2,3]}
},function(error, response){// ...});

类似的也可以同时执行多个查询:

client.msearch({
     body:[ // match all query, on all indices and types {}, { query:{ match_all:{}}}, // query_string query, on index/mytype {
        _index:'myindex',
        _type:'mytype' },{
        query:{
            query_string:{ query:'"Test 1"'} } }]
});

扩展

通过上面基本API的使用,基本可以了解js端对ESclient的操作。当然也可以使用下面的变成风格调用方法:

es[method](params)
它类似
es.method(params,回调方法)

在kibana中的_doc_send_to_es.js,使用了如下的封装:

function (method, validateVersion, body, ignore) {// debugger;var doc = this;// straight assignment will causes undefined valuesvar params = _.pick(this._state, ['id', 'type', 'index']);params.body = body;params.ignore = ignore || [409];if (validateVersion && params.id) {params.version = doc._getVersion();}// debugger;return es[method](params).then(function (resp) {// debugger;if (resp.status === 409) throw new errors.VersionConflict(resp);doc._storeVersion(resp._version);doc.id(resp._id);var docFetchProm;if (method !== 'index') {docFetchProm = doc.fetch();} else {// we already know what the response will bedocFetchProm = Promise.resolve({_id: resp._id,_index: params.index,_source: body,_type: params.type,_version: doc._getVersion(),found: true});}// notify pending request for this same document that we have updatesdocFetchProm.then(function (fetchResp) {// use the key to compair sourcesvar key = doc._versionKey();// clear the queue and filter out the removed items, pushing the// unmatched ones back in.var respondTo = requestQueue.splice(0).filter(function (req) {var isDoc = req.source._getType() === 'doc';var keyMatches = isDoc && req.source._versionKey() === key;debugger;// put some request back into the queueif (!keyMatches) {requestQueue.push(req);return false;}return true;});return courierFetch.fakeFetchThese(respondTo, respondTo.map(function () {return _.cloneDeep(fetchResp);}));});return resp._id;}).catch(function (err) {// cast the errorthrow new errors.RequestFailure(err);});};

因此使用时,又变成了:

xxx.call(this, 'create', false, body, []);

一层一层封装了很多,但是只要慢慢屡清除,就知道怎么使用了。

本文转自博客园xingoo的博客,原文链接:Elasticsearch Javascript API增删改查,如需转载请自行联系原博主。

Elasticsearch Javascript API增删改查相关推荐

  1. javascript实现增删改查

    增删改查 增删改查对于学前端的来说,可以说是必修课,在很多的网站中都会遇见这种功能,那么今天,我们就来看看如何使用js来实现正删改查. 首先,我们先模拟一个后台数据,如下: var data = [{ ...

  2. JavaScript 对象增删改查 + 遍历对象+内置函数 + 随机对象

    red red 1.什么是对象? 对象是JavaScript 里的一种数据类型:可以理解为是一种无序的数据集合:用来描述某个事物,例如描述一个人信息 2.对象怎么声明? let 对象名 = {} 例如 ...

  3. API 增删改查命名规范

    常用的增删改查命名规约 阿里命名规范 Service/Dao 层命名规约 获取单个对象的方法用get做前缀. 获取多个对象的方法用list做前缀. 获取统计值的方法用count做前缀. 返回布尔值的方 ...

  4. ElasticSearch 6.x 增删改查操作汇总 及 python调用ES中文检索实例

    文章目录 Error汇总 1. 由于Elasticsearch可以输入且执行脚本,为了系统安全,不允许使用root启动 2. 外部无法访问 3. 解决 max virtual memory areas ...

  5. 【ElasticSearch】使用Docker安装ElasticSearch、基本增删改查使用

    一.Elasticsearch Elasticsearch 是一个分布式.可扩展.实时的搜索与数据分析引擎. 官方中文文档:Elasticsearch 权威指南 二.安装 1.拉取镜像 docker ...

  6. 纯javascript实现增删改查

    效果图: html: <!DOCTYPE html> <html><head><meta charset="UTF-8"><t ...

  7. .net Api 接口调用 增删改查

    .net Api项目搭建内容以前已经说过了,文章链接,这篇文章在已经搭建好项目基础上简单说下如何建立API增删改查接口. 在Models中建立一个实体类: namespace ApiTest.Mode ...

  8. VUE2.0增删改查附编辑添加model(弹框)组件共用

    为什么80%的码农都做不了架构师?>>> Vue实战篇(增删改查附编辑添加model(弹框)组件共用) 前言 最近一直在学习Vue,发现一份crud不错的源码 预览链接 https: ...

  9. elasticsearch的增删改查

    增删改查是数据库的基础操作方法.ES 虽然不是数据库,但是很多场合下,都被 人们当做一个文档型 NoSQL 数据库在使用,原因自然是因为在接口和分布式架构 层面的相似性.虽然在 Elastic Sta ...

最新文章

  1. 业务系统性能优化——缓存
  2. [原创]软件产品的质量
  3. 数据库设计中的范式形式
  4. java前沿技术_Java语言就业前景如何?这4个就业方向都很吃香
  5. ListCtrl添加右键菜单(ListCtrl类里编辑,给ListCtrl 发送NM_RCLICK消息)
  6. 基于TCP原理,采用Socket通信技术,实现聊天室
  7. 【python】Python的基本数据类型之数据转换
  8. Web安全实践(6)web应用剖析之信息提炼
  9. (渝粤教育)网络教育远程教育统考计算机应用基础复习题
  10. linux在拥有/etc/xdg的自启动方式
  11. 408数据结构考研笔记!超级详细!23最新考纲
  12. matlab的foramt
  13. UDIMM、RDIMM和LRDIMM
  14. 《疯狂Java讲义》学习笔记 第六章 面向对象(下)
  15. 从图(Graph)到图卷积(Graph Convolution):漫谈图神经网络模型 (二)
  16. 电影与幸福感期末答案和平时测试答案
  17. 【算法学习笔记】6:SAT问题的一些经典求解策略
  18. 从SQLserver中导出表数据到Access
  19. Unity基础知识学习七,帧同步源码学习
  20. 分享雅虎前端优化军规

热门文章

  1. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验二十九:LCD模块
  2. C/C++获得当前系统时间
  3. 【超级大转载】常用的D3D变换相关函数
  4. ashx是什么文件,如何创建
  5. spring 声明式事务
  6. 每个设计师需知的40个设计素材站
  7. Linux文件存储结构,包括目录项、inode、数据块
  8. 解析codepage和charset的含义及其应用
  9. Windows Server 2003 网络互访新特性
  10. [转载]IT知识体系结构图