Elasticsearch Javascript API增删改查
时间:2022-04-22
本文章向大家介绍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, []);
一层一层封装了很多,但是只要慢慢屡清除,就知道怎么使用了。
- Python爬虫(十九)_动态HTML介绍
- Python爬虫(二十)_动态爬取影评信息
- Python爬虫(二十一)_Selenium与PhantomJS
- Python爬虫(十三)_案例:使用XPath的爬虫
- Python爬虫(十四)_BeautifulSoup4 解析器
- Python爬虫(十五)_案例:使用bs4的爬虫
- Python爬虫(十六)_JSON模块与JsonPath
- 多类好米齐交易:域名776.cn近10万元结拍
- Python爬虫(八)_Requests的使用
- Python爬虫(十一)_案例:使用正则表达式的爬虫
- Python爬虫(十二)_XPath与lxml类库
- 区块链域名热度不减 健康类英文米近六位交易
- Python爬虫(九)_非结构化数据与结构化数据
- Python爬虫(十)_正则表达式
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- python selenium 关于将网页打包为静态网页(mhtml)下载。
- Redis:哨兵
- Python中设置指定窗口为前台活动窗口(最顶层窗口)win32gui
- 关于pandas.eval使用的一些问题。
- Flutter基础widgets教程-Slider篇
- 带你认识Pytest(三)
- Additive Powers-of-Two (APoT) Quantization:硬件友好的非均匀量化方法
- Flutter基础widgets教程-SnackBar篇
- Redash 二开 - 后端环境搭建
- Flutter基础widgets教程-Stepper篇
- Flutter基础widgets教程-Switch篇
- 13.深入k8s:Pod 水平自动扩缩HPA及其源码分析
- python 协程新版
- 14.深入k8s:kube-proxy ipvs及其源码分析
- netty