查询

根据索引、类型、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 values
var 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 be
docFetchProm = 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 updates
docFetchProm.then(function (fetchResp) {
// use the key to compair sources
var 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 queue
if (!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 error
throw new errors.RequestFailure(err);
});
};

因此使用时,又变成了:

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

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

Elasticsearch Javascript API增删改查的更多相关文章

  1. javaScript实现增删改查

    自己写的一个html+javaScript实现增删改查小实例.下面是js代码​1. [代码][JavaScript]代码   //1.创建受捐单位数组var arrOrgData = [    { & ...

  2. elasticsearch实例讲解增删改查

    1.首先弄明白四个概念 elasticsearch 关系型数据库 index 数据库 type 表 document 行 field 字段 如果刚一开始理解比较困难,那你就在心中默念100遍,10遍也 ...

  3. 04-springboot整合elasticsearch初识-简单增删改查及复杂排序,分页,聚合操作

        前面大概了解了一下elasticsearch的数据存储和数据的查询.现在学习一下,es的复杂操作.     官网相关文档地址:https://www.elastic.co/guide/en/e ...

  4. [py]django强悍的数据库接口(QuerySet API)-增删改查

    django强悍的数据库接口(QuerySet API) 4种方法插入数据 获取某个对象 filter过滤符合条件的对象 filter过滤排除某条件的对象- 支持链式多重查询 没找到排序的 - 4种方 ...

  5. Es学习第三课, ElasticSearch基本的增删改查

    前面两课我们了解了ES的基本概念并且学会了安装ES,这节课我们就来讲讲ES基本的增删改查:ES主要对外界提供的是REST风格的API,我们通过客户端操作ES本质上就是API的调用.在第一课我们就讲了索 ...

  6. elasticsearch索引的增删改查入门

    为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口. RESTful接口URL的格式: http://localhost:9200/<index>/&l ...

  7. Zookeeper api增删改查节点

    Exists - 检查Znode的存在 ZooKeeper类提供了 exists 方法来检查znode的存在.如果指定的znode存在,则返回一个znode的元数据.exists方法的签名如下: ex ...

  8. SpringBoot 集成Elasticsearch进行简单增删改查

    一.引入的pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...

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

    Vue实战篇(增删改查附编辑添加model(弹框)组件共用) 前言 最近一直在学习Vue,发现一份crud不错的源码 预览链接 https://taylorchen709.github.io/vue- ...

随机推荐

  1. 灰色预测原理及JAVA实现

    最近在做项目时,用户不想使用平均值来判断当前数据状态,想用其他的方式来分析数据的变化状态,在查找了一些资料后,想使用灰色预测来进行数据的预测.下面的内容是从网上综合下来的,java代码也做了一点改动, ...

  2. MicroPHP 2.2.0 发布

    ver 2.2.0: 增加了:     1.$this->cache为最新的phpfastcache2.1,缓存功能更加强大,而且编写自己的缓存类非常容易.     2.自定义缓存类说明:   ...

  3. phpmyadmin Wrong permissions on configuration file, should not be world writable!

    巴拉巴拉,实际场景是这样,因为有需要,所以想用django 做个rest服务给其他平台提供服务,发现以前正常的页面都无法运行,奇怪发现有一个页面提示连接不上mysql 难道mysql挂了,打开phpm ...

  4. 使用ABP时报错“UPDATE 语句与 FOREIGN KEY SAME TABLE 约束"FK_dbo.AbpUsers_dbo.AbpUsers_LastModifierUserId"冲突”的解决办法

    ABP理论学习总目录 一步一步使用ABP框架搭建正式项目系列教程 ABP之Module-Zero学习目录 本篇目录 问题 原因 解决办法 问题 问题的是在下面这种情况下出现的: 我在使用CodeFir ...

  5. js中的深复制和浅复制

    在实际情况中经常会遇到对对象复制的问题.比如在处理项目中的一笔多结构的数据存储或者调用,这个时候你就要对对象(json)进行操作,而不同的操作根据不同的需求来定义.其中最常见最普遍的是对对象的复制,重 ...

  6. 使用SQL生成DateTime.Ticks

    在项目中我需要使用到一个随机数(Random Number),该随机数将作为 Hashtable 中的 Key 用于唯一索引数据,所以需要保持单机唯一性. 同时该随机数还需要具备可排序性以便对数据进行 ...

  7. Javascrpt无刷新文件上传

    最近工作中遇到上传文件问题,主要需求是一步点击上传,兼容ie8+,当时用的dojox/form/uploader控件,这两天扒了一下源码,明白了原理拿出来分享一下. 总体思路如下: 1.对于支持XML ...

  8. Wix 安装部署教程(十三) -- 多语言安装包

    这几天摸索WIX的多语言安装包(这里是Wix的setup 工程,不是Bundle),终于走通了,感谢网友uni的指点.WIX的多语言安装包能够根据系统环境自动切换界面语言,你也可以通过命令指定语言.下 ...

  9. MSDN Kinect for Windows SDK中文版论坛开放了

    昨天MSDN上K4W开发相关的中文版论坛正式开放了,地址 http://social.msdn.microsoft.com/Forums/zh-CN/home?forum=k4wdevelopment ...

  10. python 实现web框架simfish

    python 实现web框架simfish 本文主要记录本人利用python实现web框架simfish的过程.源码github地址:simfish WSGI HTTP Server wsgi模块提供 ...