diff --git a/Elasticsearch/Lite.php b/Elasticsearch/Lite.php new file mode 100755 index 0000000000000000000000000000000000000000..7ab68546a74a41319968160c55876b4b4b8ab064 --- /dev/null +++ b/Elasticsearch/Lite.php @@ -0,0 +1,145 @@ + + */ +class Elasticsearch_Lite +{ + private $host; + private $port; + private $indexName; //索引名,类似于数据库中的数据库名 + private $tableName; //索引的表名,类似于数据库中的数据库的表名 + + function __construct($host = "", $port = "", $indexName = "", $tableName = "") + { + $config = DI()->config->get('app.DB_CONFIG_ELASTICSEARCH'); + $this->host = $host ? $host : $config ['DB_HOST']; + $this->port = $port ? $port : $config ['DB_PORT']; + $this->indexName = $indexName ? $indexName : $config ['DB_INDEX']; + $this->tableName = $tableName ? $tableName : $config ['DB_TABLE']; + } + + /** + * 创建索引 + */ + public function createIndex() + { + $indexUrl = 'http://' . $this->host . ':' . $this->port . '/' . $this->indexName; + return $this->curl($indexUrl); + } + + /** + * 切换索引 + */ + public function switchIndex($indexName) + { + $this->indexName = $indexName; + return $this; + } + + /** + * 切换索引 + */ + public function switchTable($tableName) + { + $this->tableName = $tableName; + return $this; + } + + /** + * 创建字段 + * @param array $mapping + * @return array + */ + public function createMapping($mapping) + { + $url = 'http://' . $this->host . ':' . $this->port . '/' . $this->indexName . '/' . $this->tableName . '/_mapping'; + print_r($mapping); + $json_mapping = json_encode($mapping); + return $this->curl($url, $json_mapping); + } + + /** + * 添加数据 + * @param array $data + * @return array + */ + + public function add($id, $data) + { + $url = 'http://' . $this->host . ':' . $this->port . '/' . $this->indexName . '/' . $this->tableName . '/' . $id; + $json_data = json_encode($data); + return $this->curl($url, $json_data); + } + + /** + * 查找数据 + * @param array $data + * @return array + */ + public function search($data) + { + $url = 'http://' . $this->host . ':' . $this->port . '/' . $this->indexName . '/' . $this->tableName . '/_search'; + $json_data = json_encode($data); + return $this->curl($url, $json_data, "GET"); + } + + /** + * 删除数据 + * @param array $data + * @return array + */ + public function delete($data) + { + $url = 'http://' . $this->host . ':' . $this->port . '/' . $this->indexName . '/' . $this->tableName . '/' . $data; + return $this->curl($url, " ", "DELETE"); + } + + /** + * 查看 + * @param array $id + * @return array + */ + public function view($id) + { + $url = 'http://' . $this->host . ':' . $this->port . '/' . $this->indexName . '/' . $this->tableName . '/' . $id; + return $this->curl($url, " ", "GET"); + } + + /** + * 跟新数据 + * @param array $id + * @param array $data + * @return array + */ + public function update($id, $data) + { + $url = 'http://' . $this->host . ':' . $this->port . '/' . $this->indexName . '/' . $this->tableName . '/' . $id . '/_update'; + $json_data = json_encode($data); + return $this->curl($url, $json_data, "POST"); + } + + /** + * @param string $url + * @param string $postDate json格式 + * @return array + */ + private function curl($url, $postDate = "", $method = "PUT") + { + $ci = curl_init(); + curl_setopt($ci, CURLOPT_URL, $url); + curl_setopt($ci, CURLOPT_PORT, $this->port); + curl_setopt($ci, CURLOPT_TIMEOUT, 200); + curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ci, CURLOPT_FORBID_REUSE, 0); + curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); + if ($postDate) { + curl_setopt($ci, CURLOPT_POSTFIELDS, $postDate); + } + $response = curl_exec($ci); + return json_decode($response); + } + +} \ No newline at end of file diff --git a/Elasticsearch/README.md b/Elasticsearch/README.md new file mode 100644 index 0000000000000000000000000000000000000000..3d3b816d53d28f788abbea8e041fe7bbb6ce1854 --- /dev/null +++ b/Elasticsearch/README.md @@ -0,0 +1,76 @@ +#基于PhalApi的ELASTICSEARCH操作类 +[elasticsearch官方api文档](http://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html) + +### 1.安装和配置 + +#### 1.1 扩展包下载 +从 PhalApi-Library 扩展库中下载获取 Elasticsearch 扩展包,如使用: + +git clone https://git.oschina.net/dogstar/PhalApi-Library.git +然后把 Elasticsearch 目录复制到 ./PhalApi/Library/ 下,即: + +cp ./PhalApi-Library/Elasticsearch/ ./PhalApi/Library/ -R +到处安装完毕!接下是插件的配置。 + +#### 1.2 扩展包配置 +我们需要在 ./Config/app.php 配置文件中追加以下配置: +##### 1.2.1 服务配置 +``` + /** + * Elasticsearch服务相关配置 + */ + 'DB_CONFIG_ELASTICSEARCH' => array( + //对应的文件路径 + 'DB_HOST' => 'your DB_HOST',//es服务ip + 'DB_PORT' => 'your DB_PORT',//es服务端口 + 'DB_INDEX' => 'your DB_INDEX', //默认index 通过switchIndex 切换 + 'DB_TABLE' => 'your DB_TABLE',//默认table 通过switchTable 切换 + ), +``` + + +### 2.入门使用 +#### 2.1 入口注册 +``` +$loader->addDirs('Library'); + +//其他代码... + +//初始化(使用配置) +DI()->es = new Elasticsearch_Lite(); + + +//初始化(传参) +DI()->es = new Elasticsearch_Lite('192.168.14.110','9200','account','user'); + +``` +![入口注册](https://ws1.sinaimg.cn/large/006tKfTcgy1fqpu4ac1hej315g0pijw1.jpg) + +### 3.示例 +先简单写个查询示例: +``` + $data = [ + "query" => [ + "match" => [ + "userPhone" => "13800138000" + ] + ] + ]; + + return DI()->es->search($data); +``` +![示例](https://ws4.sinaimg.cn/large/006tKfTcgy1fqpu2a8gwxj30sg0pitbh.jpg) + +### 4. 实例可用方法 + +| method | 说明 | +| ----------- | ----------- | +| createIndex | 创建索引 | +| createMapping | 创建字段 | +| switchIndex | 切换索引 | +| switchTable | 切换table | +| add | 添加数据 | +| search | 查找数据 | +| delete | 删除数据 | +| view | 查看 | + diff --git a/Elasticsearch/config/app.php b/Elasticsearch/config/app.php new file mode 100755 index 0000000000000000000000000000000000000000..5f7bde847c7c1c87746881bd1ad0dd4f05781f82 --- /dev/null +++ b/Elasticsearch/config/app.php @@ -0,0 +1,13 @@ + [ + 'DB_HOST' => 'your DB_HOST',//es服务ip + 'DB_PORT' => 'your DB_PORT',//es服务端口 + 'DB_INDEX' => 'your DB_INDEX', //默认index 通过switchIndex 切换 + 'DB_TABLE' => 'your DB_TABLE',//默认table 通过switchTable 切换 + ] +]; + +