• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
钗的php生涯
博客园    首页    新随笔    联系   管理    订阅  订阅
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");

 

posted on 2019-04-26 13:57  php钗  阅读(364)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3