查询

根据索引、类型、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. [转载]sql语句练习50题

    Student(Sid,Sname,Sage,Ssex) 学生表 Course(Cid,Cname,Tid) 课程表 SC(Sid,Cid,score) 成绩表 Teacher(Tid,Tname) ...

  2. 【转】各种语言中的urlencode方法

    URLENCODE和URLDECODE是比较常用的URL参数转换方法,为以后使用方便,自己归类一下.   原文地址:http://blog.sina.com.cn/s/blog_3f195d25010 ...

  3. JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (1):NetBeans 寫 Servlet (转帖)

    JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (1):NetBeans 寫 Servlet 功能:這支程式的主要功能是將 javafx 與 swi ...

  4. 网易云信,发送验证码短信C#版代码

    网易云信发送短信代码(C# 版)....需要注意SHA1 String有转换小写!!!! using System; using System.Collections.Generic; using S ...

  5. Mvc利用淘宝Kissy uploader实现图片批量上传附带瀑布流的照片墙

    前言 KISSY 是由阿里集团前端工程师们发起创建的一个开源 JS 框架.它具备模块化.高扩展性.组件齐全,接口一致.自主开发.适合多种应用场景等特性.本人在一次项目中层使用这个uploader组件. ...

  6. android知识杂记(三)

    记录项目中的android零碎知识点,用以备忘. 1.android 自定义权限 app可以自定义属于自己的权限: <permission android:description="s ...

  7. guava之Joiner 和 Splitter

    最近在给客户准备一个Guava的分享,所以会陆续的更新关于Guava更多的细节分享.本文将记录Guava中得字符串处理Joiner(连接)和Splitter(分割)处理. Joiner 首先我们来看看 ...

  8. 使用FiddlerCore来测试WebAPI

    大家在调试Web相关的API时,经常会用Fiddler来查看相关的请求,以及返回结果.当然你也可以尝试修改或者重复你的请求信息.本文主要介绍如何使用代码来实现fiddler的功能. Fiddler C ...

  9. python2.7和python3共存

    python2.7和python3共存 原本装了python,玩nodejs的时候需要node-gyp来编译依赖,无赖这货需要python2.5<v<3.0,那就弄两个版本吧 转载自 ht ...

  10. Oracle 11g系列:函数与存储过程

    1.函数 Oracle中的函数分为两类:系统函数和自定义行数.对于自定义函数,函数的传入参数可以没有,如果有,一定要明确其数据类型.函数传入参数不能在函数内部进行修改.函数必须有返回值,并且返回值必须 ...