Elasticsearch Javascript API增删改查
查询
根据索引、类型、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增删改查的更多相关文章
- javaScript实现增删改查
自己写的一个html+javaScript实现增删改查小实例.下面是js代码1. [代码][JavaScript]代码 //1.创建受捐单位数组var arrOrgData = [ { & ...
- elasticsearch实例讲解增删改查
1.首先弄明白四个概念 elasticsearch 关系型数据库 index 数据库 type 表 document 行 field 字段 如果刚一开始理解比较困难,那你就在心中默念100遍,10遍也 ...
- 04-springboot整合elasticsearch初识-简单增删改查及复杂排序,分页,聚合操作
前面大概了解了一下elasticsearch的数据存储和数据的查询.现在学习一下,es的复杂操作. 官网相关文档地址:https://www.elastic.co/guide/en/e ...
- [py]django强悍的数据库接口(QuerySet API)-增删改查
django强悍的数据库接口(QuerySet API) 4种方法插入数据 获取某个对象 filter过滤符合条件的对象 filter过滤排除某条件的对象- 支持链式多重查询 没找到排序的 - 4种方 ...
- Es学习第三课, ElasticSearch基本的增删改查
前面两课我们了解了ES的基本概念并且学会了安装ES,这节课我们就来讲讲ES基本的增删改查:ES主要对外界提供的是REST风格的API,我们通过客户端操作ES本质上就是API的调用.在第一课我们就讲了索 ...
- elasticsearch索引的增删改查入门
为了方便直观我们使用Head插件提供的接口进行演示,实际上内部调用的RESTful接口. RESTful接口URL的格式: http://localhost:9200/<index>/&l ...
- Zookeeper api增删改查节点
Exists - 检查Znode的存在 ZooKeeper类提供了 exists 方法来检查znode的存在.如果指定的znode存在,则返回一个znode的元数据.exists方法的签名如下: ex ...
- SpringBoot 集成Elasticsearch进行简单增删改查
一.引入的pom文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- VUE2.0增删改查附编辑添加model(弹框)组件共用
Vue实战篇(增删改查附编辑添加model(弹框)组件共用) 前言 最近一直在学习Vue,发现一份crud不错的源码 预览链接 https://taylorchen709.github.io/vue- ...
随机推荐
- C# Retry重试操作解决方案(附源码)
一.前言 (1)对于Thread的Abort方法,如果线程当前正在执行的是一段非托管代码,那么CLR就不会抛出ThreadAbortException,只有当代码继续回到CLR中时,才会引发Threa ...
- 如何把.cs文件编译成DLL文件
开始--程序--Microsoft Visual Studio.NET 2013--Visual Studio.NET工具,点击其中的"VS2013 开发人员命令提示",就会进入M ...
- MySQL7:视图
什么是视图 数据库中的视图是一个虚拟表.视图是从一个或者多个表中导出的表,视图的行为与表非常相似,在视图中用户可以使用SELECT语句查询数据,以及使用INSERT.UPDATE和DELETE修改记录 ...
- 【读书笔记】Programming Entity Framework CodeFirst -- 初步认识
以下是书<Programming Entity Framework Code First>的学习整理,主要是一个整体梳理. 一.模型属性映射约定 1.通过 System.Component ...
- C#Light 再推荐,顺便介绍WP8 功能展示项目
由于在项目中验证了C#Light脚本,C#Light的健壮和稳定程度已经得到了很大的提升. 现在可以更好的把C#Light介绍给大家使用,同时也有更多的自信,告诉大家这是一个已经具有商业价值的类库. ...
- Sqlserver 如何获取每组中的第一条记录
在日常生活方面,我们经常需要记录一些操作,类似于日志的操作,最后的记录才是有效数据,而且可能它们属于不同的方面.功能下面,从数据库的术语来说,就是查找出每组中的一条数据. 例子 我们要从上面获得的有效 ...
- 细说.NET中的多线程 (五 使用信号量进行同步)
上一节主要介绍了使用锁进行同步,本节主要介绍使用信号量进行同步 使用EventWaitHandle信号量进行同步 EventWaitHandle主要用于实现信号灯机制.信号灯主要用于通知等待的线程.主 ...
- 跨站请求伪造CSRF
CSRF是Cross Site Request Forgery的缩写,乍一看和XSS差不多的样子,但是其原理正好相反,XSS是利用合法用户获取其信息,而CSRF是伪造成合法用户发起请求. 在XSS危害 ...
- Ajax跨域访问XML数据的另一种方式——使用YQL查询语句
XML数据默认是不能在客户端通过Ajax跨域请求读取的,一般的做法是在服务器上写一个简单的代理程序,将远程XML的数据先读到本地服务器,然后客户端再从本地服务器通过Ajax来请求.由于我们不能对数据源 ...
- 建立 svn 服务端
上一篇文章 (SVN 使用)是针对于客户端,本文是说明如何在本地设置服务端 1,建立服务端站点 svnadmin create /Users/hour/Desktop/svn 2,终端进入svn 里的 ...