浅谈PHP Elasticsearch的简单使用技巧
发布时间:2022-07-19 14:46:50 所属栏目:PHP教程 来源:互联网
导读:PHP中使用Elasticsearch composer require elasticsearch/elasticsearch 会自动加载合适的版本!我的php是5.6的,它会自动加载5.3的elasticsearch版本! Using version ^5.3 for elasticsearch/elasticsearch ./composer.json has been updated Loading comp
PHP中使用Elasticsearch composer require elasticsearch/elasticsearch 会自动加载合适的版本!我的php是5.6的,它会自动加载5.3的elasticsearch版本! Using version ^5.3 for elasticsearch/elasticsearch ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 4 installs, 0 updates, 0 removals - Installing react/promise (v2.7.0): Downloading (100%) - Installing guzzlehttp/streams (3.0.0): Downloading (100%) - Installing guzzlehttp/ringphp (1.1.0): Downloading (100%) - Installing elasticsearch/elasticsearch (v5.3.2): Downloading (100%) Writing lock file Generating autoload files 简单使用 <?php class MyElasticSearch { private $es; // 构造函数 public function __construct() { include('../vendor/autoload.php'); $params = array( '127.0.0.1:9200' ); $this->es = ElasticsearchClientBuilder::create()->setHosts($params)->build(); } public function search() { $params = [ 'index' => 'megacorp', 'type' => 'employee', 'body' => [ 'query' => [ 'constant_score' => [ //非评分模式执行 'filter' => [ //过滤器,不会计算相关度,速度快 'term' => [ //精确查找,不支持多个条件 'about' => '谭' ] ] ] ] ] ]; <?php require "./MyElasticSearch.php"; $es = new MyElasticSearch(); $es->search(); 执行结果 Array ( [took] => 2 [timed_out] => [_shards] => Array ( [total] => 5 [successful] => 5 [skipped] => 0 [failed] => 0 ) [hits] => Array ( [total] => 1 [max_score] => 1 [hits] => Array ( [0] => Array ( [_index] => megacorp [_type] => employee [_id] => 3 [_score] => 1 [_source] => Array ( [first_name] => 李 [last_name] => 四 [age] => 24 [about] => 一个PHP程序员,热爱编程,谭康很帅,充满激情。 [interests] => Array ( [0] => 英雄联盟 ) ) ) ) require '../vendor/autoload.php'; use ElasticsearchClientBuilder; class MyElasticSearch { private $client; // 构造函数 public function __construct() { $params = array( '127.0.0.1:9200' ); $this->client = ClientBuilder::create()->setHosts($params)->build(); } // 创建索引 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 (ElasticsearchCommonExceptionsBadRequest400Exception $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 create_mappings($type_name = 'goods',$index_name = 'test_ik') { $params = [ 'index' => $index_name, 'type' => $type_name, 'body' => [ $type_name => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'id' => [ 'type' => 'integer', // 整型 'index' => 'not_analyzed', ], 'price' => [ 'type' => 'integer' ] ] ] ] ]; $response = $this->client->indices()->putMapping($params); return $response; } // 查看映射 public function get_mapping($type_name = 'goods',$index_name = 'test_ik') { $params = [ 'index' => $index_name, 'type' => $type_name ]; $response = $this->client->indices()->getMapping($params); return $response; } ]; $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') { // 可以灵活添加新字段,最好不要乱添加 $params = [ 'index' => $index_name, 'type' => $type_name, 'id' => $id, 'body' => [ 'doc' => [ 'title' => '苹果手机iPhoneX' ] ] ]; $response = $this->client->update($params); return $response; } (编辑:应用网_丽江站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |