赞
踩
namespace app\common\model; use Elasticsearch\ClientBuilder; class EsModel { protected $client; // 连接es public function __construct() { try { $this->client = ClientBuilder::create() ->setHosts(['127.0.0.1:9200']) ->build(); } catch (\Exception $e) { // 输出连接错误信息 echo $e->getMessage(); exit; } } // 创建es索引 public function addIndex(){ // 定义索引的设置和映射 $indexParams = [ 'index' => 'product_info', 'body' => [ 'settings' => [ 'number_of_shards' => 5, 'number_of_replicas' => 1 ], 'mappings' => [ 'properties' => [ 'title_name' => [ 'type' => 'text', 'analyzer' => 'ik_smart' ], 'describe' => [ 'type' => 'text', 'analyzer' => 'ik_smart' ] ] ] ] ]; // 发送创建索引的请求 $this->client->indices()->create($indexParams); } // 批量插入数据 public function addEsData(){ $data = [ [ "index" => ['_id' => 1,] ], [ "title_name" => "一个新的文章", "describe" => "陈美凤日前出席假发品牌32周年活动,只见她顶著一头粉色的秀发,再配上深V领黑西服" ], ]; $params = [ 'index' => 'product_info', // 索引名称 'body' => $data, ]; $response = $this->client->bulk($params); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。