查询

根据索引、类型、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. go json null字段的转换

    最近试了试go中对json null字段进行转换,代码如下: struct 转 json: package main import ( "encoding/json" " ...

  2. 从MySQL5.7.6开始,安装MySQL提示“请键入 NET HELPMSG 3534 以获得更多的帮助”的解决办法

    今天安装MySQL提示如下错误: ----------------------------------------------------------------------------------- ...

  3. PCWIFI--无线网络共享软件

    前段时间由于需要共享笔记本无线网络给手机使用,在网上找了几个软件试了一下,没找到比较好用的,要么是收费的,要么有广告,要么附带一大堆其他功能,所以决定自己写一个小软件来实现该功能.软件相关介绍如下:  ...

  4. C#更改文件访问权限所有者(适用于各个Windows版本)

    前面也提到了,前段时间在做Online Judge系统,在正式上线前有几个比较老的版本,其中第一个版本使用ACL来控制权限以确保安全(但是这个版本完全建立在IIS上,所以这样做是没效果的),遇到了一些 ...

  5. Emberjs之Observer

    Observer Person.reopen({ fullNameChanged: Ember.observer('fullName', function() { // deal with the c ...

  6. Java虚拟机5:Java垃圾回收(GC)机制详解

    哪些内存需要回收? 哪些内存需要回收是垃圾回收机制第一个要考虑的问题,所谓“要回收的垃圾”无非就是那些不可能再被任何途径使用的对象.那么如何找到这些对象? 1.引用计数法 这个算法的实现是,给对象中添 ...

  7. require、module、exports dojo中的三个特殊模块标识

    查看dojo源码过程中,发现这三个模块名并不以dojo.dijit.dojox开头,在dojo加载器中属于特殊模块名. require 这是一个上下文智能的加载器. 我们通过dojoConfig配置了 ...

  8. Javascript事件机制兼容性解决方案

    本文的解决方案可以用于Javascript native对象和宿主对象(dom元素),通过以下的方式来绑定和触发事件: 或者 var input = document.getElementsByTag ...

  9. 免费打造自己的个人网站,免费域名、免费空间、FTP、数据库什么的,一个不能少,没钱,也可以这么任性

    作为一名程序猿,拥有自己的个人网站,是一件多么有逼格的事~~至于个人网站的好处嘛?那是多的说都说不完啊~~例如我们可以放自己的作品,展示自己的风采,放自己女神的照片(女神看到后会怎么样,自己想吧,哈哈 ...

  10. fsfds

    ccc fs -fsd fsdfsfs