es-curl 查询与更新
1,封装http方法
private function http($url, $data = NULL, $json = false)
{
unset($res,$curl,$errorno);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
if (!empty($data)) {
if ($json && is_array($data)) {
$data = json_encode($data);
} curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
if ($json) { //发送JSON数据
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json; charset=utf-8', 'Content-Length:' . strlen($data))
);
}
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
$errorno = curl_errno($curl); if ($errorno) {
return array('errorno' => false, 'errmsg' => $errorno);
}
curl_close($curl);
return json_decode($res, true);
}
2.查询调用
#索引
$index_name = 'apt_result_md5';
$ip = es的ip
$port = 端口(9200)
# 查询条件
$params['query']['bool']['must'][] = ['match'=>[$white_type=>$white_content]];
$params['size'] = 1000; $search = $this->http("http://".$ip:$port."/".$index_name."/_search",$params,true);
3.更新调用
# $index_name/$type/$id 索引/类型/id
$update['doc'][更新字段] = $arr;
$updateRes = $this->http("http://" . $ip:$port . "/" . $index_name ."/" .$type. "/".$id. "/_update", $update, true);
4.批量操作
/**
* @param $id 需要修改的id
* @param $arr 需要修改的内容
*/
public function onposDelEs($id,$arr){
try { $update = array(
'update' => array(
'_id' => $id,
'_retry_on_conflict' => 3,
));
$a .= json_encode($update) . "\n";
$update = array(
'doc' => array(
$white_type => $arr,
)
);
$a .= json_encode($update) . "\n";
}
} $updateRes = $this->http("http://" . $GLOBALS['CONFIG']['ES_HORSE']['DSN'] . ':' . $GLOBALS['CONFIG']['ES_HORSE']['PORT'] . "/" . $index_name . "/" . $type . "/_bulk", $a, false);
if(!$updateRes){
$this->ajaxReturn(array('code' => -1, 'msg' => 'update es 数据 失败'));
}
}
}
} catch (Exception $e) {
$this->ajaxReturn(array('code' => -1, 'msg' => $e->getMessage()));
}
}
5.其他拓展
行为 解释
create |
当文档不存在时创建 |
index |
创建新文档或替换已有文档。 |
update |
局部更新文档。 |
delete |
删除一个文档。 |

POST /_bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123", "_retry_on_conflict" : 3} }
{ "doc" : {"title" : "My updated blog post"} }

es-curl 查询与更新的更多相关文章
- ES 复合查询
ES在查询过程中比较多遇到符合查询,既需要多个字段过滤也需要特殊情况处理,本文简单介绍几种查询组合方便快捷查询ES. bool布尔查询有一个或者多个布尔子句组成 filter 只过滤符合条件的 ...
- es的查询、排序查询、分页查询、布尔查询、查询结果过滤、高亮查询、聚合函数、python操作es
今日内容概要 es的查询 Elasticsearch之排序查询 Elasticsearch之分页查询 Elasticsearch之布尔查询 Elasticsearch之查询结果过滤 Elasticse ...
- 使用TSQL查询和更新 JSON 数据
JSON是一个非常流行的,用于数据交换的文本数据(textual data)格式,主要用于Web和移动应用程序中.JSON 使用“键/值对”(Key:Value pair)存储数据,能够表示嵌套键值对 ...
- 简单的php Mysql类(查询 删除 更新)
php Mysql类一般都包括了几乎我们常用的数据库操作方法,这里只提供了查询 删除 更新三种操作,算不是很全只是一个简单的数据库查询类了. 代码如下 复制代码 class mysql { ...
- Sql Server Text 类型列 查询和更新
Text(ntext.image)类型为大数据字段,因为存储方式不同,也决定了其查询和更新不同于一般方法. 1.表定义: 2.查询: Like查询是可用的: select * from dbo.nod ...
- MongoDB 的创建、查询、更新、删除
MongoDB数据库中,创建.查询.更新.删除操作的对象是集合. 1.查看某个数据库中有哪些集合,在此之前需要使用数据库 C:\Windows\system32>mongo MongoDB sh ...
- java序列化对象 插入、查询、更新到数据库
java序列化对象 插入.查询.更新到数据库 : 实现代码例如以下: import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...
- 返璞归真 asp.net mvc (1) - 添加、查询、更新和删除的 Demo
原文:返璞归真 asp.net mvc (1) - 添加.查询.更新和删除的 Demo [索引页] [源码下载] 返璞归真 asp.net mvc (1) - 添加.查询.更新和删除的 Demo 作者 ...
- Microsoft SQL - 查询与更新
查询与更新(Query & Update) 转义引号 SQL语句中字符串只能使用单引号,如果需要转义,可用单引号转义单引号. 查询(Inquire) 以下公式中的c指代列名. 规则 1.查询语 ...
- 从零打造在线网盘系统之Hibernate查询与更新技术
欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...
随机推荐
- sudo问题汇总
1. 注释Defaults requiretty Defaults requiretty修改为 #Defaults requiretty, 表示不需要控制终端. 否则会出现sudo: sorry, y ...
- svm原理及opencv
转自http://www.cnblogs.com/justany/archive/2012/11/23/2784125.html
- VMWare windows找不到microsoft软件许可条款
提示如下错误: windows找不到microsoft软件许可条款.请确保安装源有效,然后重新启动安装. 解决方案: 把该虚拟机中的系统硬件配置中的软盘去掉. 程序员的基础教程:菜鸟程序员
- 循环结构之for循环
循环结构之for循环(一) 在很多编程语言中都有一种直接.简单的循环,它的一般形式为: 它的执行过程如下: 第一步:执行表达式1,对循环变量做初始化: 第二步:判断表达式2,若其值为真(非0),则执行 ...
- MQTT协议实现Eclipse Paho学习总结
MQTT协议实现Eclipse Paho学习总结 摘自:https://www.cnblogs.com/yfliufei/p/4383852.html 2015-04-01 14:57 by 辣椒酱, ...
- EXCEL 导入 R 的几种方法 R—readr和readxl包
导入Excel数据至R语言的几种方法 如有如下Excel数据源,如何将数据导入R语言呢?今天主要来介绍几种常见的方法: 一.使用剪贴板,然后使用read.table函数: 首先选择Excel中的数据源 ...
- 前端实用软件: Markdown工具之---Typora实用技巧(总结)
Typora是一款超简洁的markdown编辑器,具有如下特点: 完全免费,目前已支持中文 跨平台,支持windows,mac,linux 支持数学公式输入,图片插入 极其简洁,无多余功能 界面所见即 ...
- Web图片编辑控件发布-Xproer.ImageEditor
版权所有 2009-2014 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com 产品首页:http://www.ncmem.com/webplug/image-e ...
- Debug 时,执行语句
Display View The Display View displays the result of evaluating an expression in the context of the ...
- 设计模式11---组合模式(Composite Pattern)
一.组合模式定义 将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性.Compose objects into tree structures to re ...