Curl实现ElasticSearch的增删改查

一、添加数据(laravel必须安装Curl扩展)

$data = [
'username'=>"张三",
'sex'=>"女",
'age'=>“12”,
'habby'=>"看书"
'create_time'=>time()
];
$response = Curl::to("http://localhost:9200/users/adduser")//http://localhost:9200/_index/_type
->withData(json_encode($data))
->withContentType("application/json")
->post();
$res=json_decode($response,true);
$data['sid']=$res['_id'];
$this->result['code']=200;
$this->result['message']="ok";
$this->result['data']=$response;
return $response;

二、数据删除

$response = Curl::to("http://127.0.0.1:9200/user/adduser/-v08VGoBKruRPXlHAPOO")//http://localhost:9200/_index/_type/_id(添加数据生成随机id,最好不要写成死值)
->withContentType("application/json")
->delete();
return $response;

三、数据修改

$data = [
'username'=>"张三",
'sex'=>"女",
'age'=>“13”,
'habby'=>"看书"
'create_time'=>time()
];

$response = Curl::to("http://localhost:9200/user/adduser/-v08VGoBKruRPXlHAPOO")   //http://localhost:9200/_index/_type/_id(添加数据生成随机id,最好不要写成死值)
->withData(json_encode($data))
->withContentType("application/json")
->post();
return $response;

四、数据查询

$response = Curl::to("http://127.0.0.1:9200/user/adduser/_search")  //http://localhost:9200/_index/_type/_search
->withContentType("application/json")
->post();
return $response;

五、数据分页,高亮显示

public function page($username,$value,$page){
$params = [
'query' => [
'match_phrase' => [
"$username" => "$value",
]
],
"size"=>3,
"from"=>$page,
'highlight'=>[
"pre_tags" => ["<font color='red'>"],
"post_tags"=>["</font>"],
'fields'=>[
"$username"=>new \stdClass()
]
]
];

$res=Curl::to("http://localhost:9200/user/adduser/_search")
->withData(json_encode($params))
->withContentType('application/json')
->post();
return $res;
}

 

laravel框架实现,路由如下:Route::any("User/page/{username}/{value}/{page}","UserController@page");

 

转载于:https://www.cnblogs.com/chaihtml/p/10773898.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值