2025年Elasticsear 简单操作(CURD) PHP类

Elasticsear 简单操作(CURD) PHP类本示例 elasticsearc 版本 7 1 之前的版本请勿参考 如有不理解的方法可以参考 Elasticsearc 基本操作 高级查询 php Created by PhpStorm User 18110 Date 2019 6 18 Time 13 53

大家好,我是讯享网,很高兴认识大家。

本示例elasticsearch版本7.1,之前的版本请勿参考 

如有不理解的方法可以参考:

Elasticsearch 基本操作+高级查询

<?php / * Created by PhpStorm. * User: 18110 * Date: 2019/6/18 * Time: 13:53 */ //除query方法以外其他方法基本和PHP框架model层用法一样 class MyElasticSearch { private $sendUrl = 'http://localhost:9200/'; private $sendParams = null; / * 创建索引 * @param $indexName 索引名 * @param array $fields 需要创建的字段 * @param int $shards //分片数 * @param int $replicas //备份数 * @return bool //索引名 */ public function createIndex($indexName ,array $fields , $shards = 5 , $replicas = 1){ $this->sendUrl .= $indexName; $this->sendParams = [ 'settings' => [ 'number_of_shards' => $shards, 'number_of_replicas' => $replicas ], 'mappings' => [ 'properties' => $fields ] ]; $response = $this->sendRequest('PUT'); if (isset($response['acknowledged']) && $response['acknowledged']){ return $response['index']; }else{ return false; } } / * 给指定索引添加字段 * @param $indexName 索引名 * @param array $params 要添加的字段 * @return bool */ public function addIndexField($indexName , array $params){ $this->sendUrl .= $indexName . '/_mapping'; $this->sendParams = [ 'properties' => $params ]; $response = $this->sendRequest('PUT'); if (isset($response['acknowledged']) && $response['acknowledged']){ return true; }else{ return false; } } / * 给指定索引添加数据 * @param $indexName 索引名 * @param array $params 要添加的数据 * @return bool _id */ public function addIndexData($indexName , array $params , $id = null){ $id = $id ? '/' . $id : ''; $this->sendUrl .= $indexName . '/_doc' . $id; $this->sendParams = $params; $response = $this->sendRequest('POST'); if(isset($response['result']) && ($response['result'] == 'created')){ return $response['_id']; }else{ return false; } } / * 修改指定索引数据 * @param $indexName 索引名 * @param $id 要修改的条件 * @param array $params 要改成的数据 * @return bool _id */ public function updateIndexData($indexName , $id , array $params){ $this->sendUrl .= $indexName . '/_doc/' . $id . '/_update'; $this->sendParams = [ 'doc' => $params ]; $response = $this->sendRequest('POST'); if (isset($response['result']) && ($response['result'] == 'updated')){ return $response['_id']; }else{ return false; } } / * 删除指定索引数据 * @param $indexName 索引名 * @param $id 删除条件 * @return bool _id */ public function deleteIndexData($indexName , $id){ $this->sendUrl .= $indexName .'/_doc/'.$id; $response = $this->sendRequest('DELETE'); if (isset($response['result']) && ($response['result'] == 'deleted')){ return $response['_id']; }else{ return false; } } / * 查询单条数据 * @param $indexName 索引名 * @param $id 查询条件 * @return array 过滤后的有用数组 */ public function findIndexOne($indexName , $id){ $this->sendUrl .= $indexName.'/_doc/'.$id; $response = $this->sendRequest('GET'); if ($response['found']){ $data['id'] = $response['_id']; foreach ($response['_source'] as $key => $value){ $data[$key] = $value; } return $data; }else{ return []; } } / * 执行自定义条件 * @param $indexName * @param array $where * @return mixed|string */ public function query($indexName ,array $where){ $this->sendUrl .= $indexName.'/_search'; $this->sendParams = $where; return $this->sendRequest('POST'); } / * 发送请求 * @param string $type * @return mixed|string */ private function sendRequest($type = 'GET'){ $ch = curl_init($this->sendUrl); $json_data = json_encode($this->sendParams); curl_setopt($ch, CURLOPT_URL, $this->sendUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); switch ($type){ case "GET" : curl_setopt($ch, CURLOPT_HTTPGET, true); break; case "POST": curl_setopt($ch, CURLOPT_POST,true); curl_setopt($ch, CURLOPT_POSTFIELDS,$json_data); break; case "PUT" : curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS,$json_data); break; case "PATCH": curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); break; case "DELETE": curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_POSTFIELDS,$json_data); break; default: curl_setopt($ch, CURLOPT_HTTPGET, true); } curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); $response = curl_exec($ch); if ($response === FALSE) { echo '发送请求失败!'; } curl_close($ch); return json_decode($response , true); } }

讯享网

不要问我为什么写个这么low的类,项目PHP版本老,用不了的es7.1的demo,这个类对于PHP版本没要求,开启curl扩展就行。


讯享网

小讯
上一篇 2025-04-02 15:50
下一篇 2025-03-23 14:04

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/37863.html