| 在您的composer.json文件中包含elasticsearch-php |
| { |
| "require": { |
| "elasticsearch/elasticsearch": "~7.0" |
| } |
| } |
| |
| comoposer require elasticsearch/elasticsearch |
| 更新 |
| composer update |
| |
| private $client; |
| |
| |
| |
| |
| |
| public function __construct() |
| { |
| $params = array( |
| '127.0.0.1:9200' |
| ); |
| $this->client = ClientBuilder::create()->setHosts($params)->build(); |
| } |
| |
| |
| |
| |
| |
| public function exists_index($index_name = 'test_ik') |
| { |
| $params = [ |
| 'index' => $index_name |
| ]; |
| |
| try { |
| return $this->client->indices()->exists($params); |
| } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) { |
| $msg = $e->getMessage(); |
| $msg = json_decode($msg,true); |
| return $msg; |
| } |
| } |
| |
| |
| |
| |
| |
| public function create_index($index_name = 'test_ik') { |
| $params = [ |
| 'index' => $index_name, |
| 'body' => [ |
| 'settings' => [ |
| 'number_of_shards' => 5, |
| 'number_of_replicas' => 0 |
| ] |
| ] |
| ]; |
| |
| try { |
| return $this->client->indices()->create($params); |
| } catch (\Elasticsearch\Common\Exceptions\BadRequest400Exception $e) { |
| $msg = $e->getMessage(); |
| $msg = json_decode($msg,true); |
| return $msg; |
| } |
| } |
| |
| |
| |
| |
| |
| public function delete_index($index_name = 'test_ik') { |
| $params = ['index' => $index_name]; |
| $response = $this->client->indices()->delete($params); |
| return $response; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'goods') { |
| $params = [ |
| 'index' => $index_name, |
| 'type' => $type_name, |
| 'id' => $id, |
| 'body' => $doc |
| ]; |
| |
| $response = $this->client->index($params); |
| return $response; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { |
| $params = [ |
| 'index' => $index_name, |
| 'type' => $type_name, |
| 'id' => $id |
| ]; |
| |
| $response = $this->client->exists($params); |
| return $response; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { |
| $params = [ |
| 'index' => $index_name, |
| 'type' => $type_name, |
| 'id' => $id |
| ]; |
| |
| $response = $this->client->get($params); |
| return $response; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods', $body=[]) { |
| |
| $params = [ |
| 'index' => $index_name, |
| 'type' => $type_name, |
| 'id' => $id, |
| 'body' => $body |
| ]; |
| |
| $response = $this->client->update($params); |
| return $response; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'goods') { |
| $params = [ |
| 'index' => $index_name, |
| 'type' => $type_name, |
| 'id' => $id |
| ]; |
| |
| $response = $this->client->delete($params); |
| return $response; |
| } |
| * 搜索文档 (分页,排序,权重,过滤) |
| * @param string $index_name |
| * @param string $type_name |
| * @param array $body |
| public function search_doc($index_name = "test_ik",$type_name = "goods",$body=[]) { |
| $params = [ |
| 'index' => $index_name, |
| 'type' => $type_name, |
| 'body' => $body |
| ]; |
| |
| $results = $this->client->search($params); |
| return $results; |
| } |
| } |