本文共 16567 字,大约阅读时间需要 55 分钟。
环境:centos 6 php73 mysql56 ELASTIC7.71
1.安装elastic 使用华为云镜像更快哦
wget https://mirrors.huaweicloud.com/elasticsearch/7.7.1/elasticsearch-7.7.1-linux-x86_64.tar.gztar -zxvf elasticsearch-7.7.1-linux-x86_64.tar.gz
2.添加用户(用户启动elastic 不能用root所以) 去目录里面编辑配置 elastic.yml
groupadd elasticuseradd -g elastic elasticchown -R elastic.elastic /etc/elasticsearch-7.7.1vim config/elasticsearch.yml
配置如下 注意冒号后有空格
------------------------------------ Node ------------------------------------## Use a descriptive name for the node:#node.name: hxx-node-1## Add custom attributes to the node:##node.attr.rack: r1## ----------------------------------- Paths ------------------------------------## Path to directory where to store the data (separate multiple locations by comma):#path.data: /var/elastic/data## Path to log files:#path.logs: /var/elastic/logs## ----------------------------------- Memory -----------------------------------## Lock the memory on startup:##bootstrap.memory_lock: true## Make sure that the heap size is set to about half the memory available# on the system and that the owner of the process is allowed to use this# limit.## Elasticsearch performs poorly when the system is swapping the memory.## ---------------------------------- Network -----------------------------------## Set the bind address to a specific IP (IPv4 or IPv6):#network.host: 127.0.0.1## Set a custom port for HTTP:#http.port: 9200## For more information, consult the network module documentation.# on the system and that the owner of the process is allowed to use this# limit.## Elasticsearch performs poorly when the system is swapping the memory.## ---------------------------------- Network -----------------------------------## Set the bind address to a specific IP (IPv4 or IPv6):#network.host: 127.0.0.1## Set a custom port for HTTP:#http.port: 9200## For more information, consult the network module documentation.## --------------------------------- Discovery ----------------------------------## Pass an initial list of hosts to perform discovery when this node is started:# The default list of hosts is ["127.0.0.1", "[::1]"]##discovery.seed_hosts: ["host1", "host2"]## Bootstrap the cluster using an initial set of master-eligible nodes:#cluster.initial_master_nodes: ["hxx-node-1"]cluster.routing.allocation.disk.watermark.flood_stage: 99%cluster.routing.allocation.disk.threshold_enabled: false
3.安装ik分词库(用于中文) 下载对应的哦 7.7.1
不管你用何种方法 放到安装路径里 /etc/elasticsearch-7.7.1/plugins/ik
好了 切换到 elastic 然后运行elastic
su elastic./bin/elastic -d
可进行测试
[root@iZuf64idor3ej85kby45arZ elasticsearch-7.7.1]# curl 127.0.0.1:9200{ "name" : "hxx-node-1", "cluster_name" : "hxx", "cluster_uuid" : "-43vGhX0Ru2ocVqNO7VhyA", "version" : { "number" : "7.7.1", "build_flavor" : "default", "build_type" : "tar", "build_hash" : "ad56dce891c901a492bb1ee393f12dfff473a423", "build_date" : "2020-05-28T16:30:01.040088Z", "build_snapshot" : false, "lucene_version" : "8.5.1", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search"
二、PHP部分
1.composer 引入 elastic-php(使用phpstorm更方便)
"elasticsearch/elasticsearch": "~7.0",
2.编辑elastic控制器类(包括 创建 增删改查 批量给导入)Ela.php
(1)执行index 后面查询执行search
index = 'hcc_corporation'; //$this->type = 'lyric'; $this->client = ClientBuilder::create()->setHosts($host)->build(); } // 初始化 public function index() { // 只能创建一次 $this->delete_index(); $this->create_index(); //1.创建索引 $this->create_mappings(); //2.创建文档模板 /* foreach ($docs as $k => $v) { $this->add_doc($v['id'], $v); //3.添加文档 echo ''; print_r($this->get_doc($v['id'])); } exit();*/// echo '';// print_r($this->get_doc(512));// exit();// $re = $this->search_doc("我做无敌",0,2); //4.搜索结果//// echo '';// print_r($re);// exit(); echo ''; print_r("INIT OK"); exit(); } public function put_settings() { $params1 = [ 'index' => $this->index, 'body' => [ 'settings' => [ 'blocks' => [ 'read_only_allow_delete' => 'false' ] ], ] ]; $this->client->indices()->putSettings($params1); echo 'SUCCESS'; } // 创建索引 public function create_index() { // 只能创建一次 $params = [ 'index' => $this->index, 'body' => [ 'settings' => [ 'number_of_shards' => 3, 'number_of_replicas' => 2, 'blocks' => [ 'read_only_allow_delete' => 'false' ], /*'transient' => [ 'cluster' => [ 'routing' => [ 'allocation' => [ 'disk' => [ 'threshold_enabled' => 'true', 'watermark' => [ 'flood_stage' => '99%' ] ] ] ] ] ]*/ ], ] ]; try { $this->client->indices()->create($params); } catch (BadRequest400Exception $e) { $msg = $e->getMessage(); $msg = json_decode($msg, true); return $msg; } } // 删除索引 public function delete_index() { $params = ['index' => $this->index]; $index = $this->client->indices()->get($params); if ($index) { return $this->client->indices()->delete($params); } // } // 创建文档模板 public function create_mappings() { /*--------------------允许type的写法 老版本 已去除--------------------------*/ /*--------------------不允许type的写法--------------------------*/ // Set the index and type $params = [ 'index' => $this->index, 'body' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'id' => [ 'type' => 'integer', ], 'name' => [ 'type' => 'text', 'analyzer' => 'ik_smart' // 'analyzer' => 'keyword' ], /*-------------------------------------*/ /*'profile' => [ 'type' => 'text', 'analyzer' => 'ik_max_word' ], 'age' => [ 'type' => 'integer', ],*/ ] ] ]; $this->client->indices()->putMapping($params); /* echo ''; print_r('success'); exit();*/ } // 查看映射 public function get_mapping() { $params = [ 'index' => $this->index, ]; $re = $this->client->indices()->getMapping($params); echo ''; print_r($re); exit(); } // 添加一个文档(记录) public function add_doc($id, $doc) { $params = [ 'index' => $this->index, // 'type' => $this->type, 'id' => $id, 'body' => $doc ]; return $this->client->index($params); } // 判断文档(记录) public function exists_doc($id = 1) { $params = [ 'index' => $this->index, 'type' => $this->type, 'id' => $id ]; return $this->client->exists($params); } // 获取一条文档(记录) public function get_doc($id = 1) { $params = [ 'index' => $this->index, // 'type' => $this->type, 'id' => $id ]; try { $re = $this->client->get($params); } catch (Missing404Exception $e) { echo ''; print_r('未找到对应数据'); exit(); } echo ''; print_r($re); exit(); } // 更新一条文档() public function update_doc($id = 1) { // 可以灵活添加新字段,最好不要乱添加 $params = [ 'index' => $this->index, 'id' => $id, 'body' => [ 'doc' => [ 'name' => '大王' ] ] ]; return $this->client->update($params); } // 删除一条文档() public function delete_doc($id = 1) { $params = [ 'index' => $this->index, //'type' => $this->type, 'id' => $id ]; return $this->client->delete($params); } // 查询文档 (分页,排序,权重,过滤) public function search_doc($keywords = "", $from = 0, $size = 10, $order = ['id' => ['order' => 'desc']]) { /* echo ''; print_r($from); print_r($size); exit();*/ $keywords_arr = array_filter(explode(" ", $keywords)); $query = ''; $number = count($keywords_arr); if($number > 10){ return "ERROR"; } if ($number > 1) { $arr = []; foreach ($keywords_arr as $ka){ $arr[] = $ka; } $mathc_phrase = []; switch ($number){ case 2: $mathc_phrase = [ 'name'=>$arr[0], 'name'=>$arr[1] ]; break; case 3: $mathc_phrase = [ 'name'=>$arr[0], 'name'=>$arr[1], 'name'=>$arr[2] ]; break; case 4: $mathc_phrase = [ 'name'=>$arr[0], 'name'=>$arr[1], 'name'=>$arr[2], 'name'=>$arr[3], ]; break; case 5: $mathc_phrase = [ 'name'=>$arr[0], 'name'=>$arr[1], 'name'=>$arr[2], 'name'=>$arr[3], 'name'=>$arr[4], ]; break; case 6: $mathc_phrase = [ 'name'=>$arr[0], 'name'=>$arr[1], 'name'=>$arr[2], 'name'=>$arr[3], 'name'=>$arr[4], 'name'=>$arr[5], ]; break; case 7: $mathc_phrase = [ 'name'=>$arr[0], 'name'=>$arr[1], 'name'=>$arr[2], 'name'=>$arr[3], 'name'=>$arr[4], 'name'=>$arr[5], 'name'=>$arr[6], ]; break; case 8: $mathc_phrase = [ 'name'=>$arr[0], 'name'=>$arr[1], 'name'=>$arr[2], 'name'=>$arr[3], 'name'=>$arr[4], 'name'=>$arr[5], 'name'=>$arr[6], 'name'=>$arr[7], ]; break; case 9: $mathc_phrase = [ 'name'=>$arr[0], 'name'=>$arr[1], 'name'=>$arr[2], 'name'=>$arr[3], 'name'=>$arr[4], 'name'=>$arr[5], 'name'=>$arr[6], 'name'=>$arr[7], 'name'=>$arr[8], ]; break; case 10: $mathc_phrase = [ 'name'=>$arr[0], 'name'=>$arr[1], 'name'=>$arr[2], 'name'=>$arr[3], 'name'=>$arr[4], 'name'=>$arr[5], 'name'=>$arr[6], 'name'=>$arr[7], 'name'=>$arr[8], 'name'=>$arr[9], ]; break; } // $should = []; /*foreach ($keywords_arr as $k => $keyword) { // $query .= $keyword . " OR "; $mathc_phrase[] = ['name'=>$keyword]; }*/ // $query = substr($query,0,strlen($query)-3); $query_func = [ 'bool' => [ 'must' => [ //$should //$mathc_phrase /*'query_string' => [ 'default_field' => 'name', 'query' => $query ]*/ 'match_phrase'=>$mathc_phrase, /* 'match_phrase'=> [ 'name'=>'研究会', ]*/ ] ] ]; /*echo ''; print_r($query_func); exit();*/ } else { // $query = $keywords; $query_func = [ /*-----------------------------name 单字段单匹配---------------------------*/ 'bool' => [ 'should' => [ 'match_phrase' => [ 'name' => $keywords ] ] ] ]; } if ($keywords) { $params = [ 'index' => $this->index, // 'type' => $this->type, 'body' => [ 'query' => $query_func, 'sort' => [$order], 'from' => $from, 'size' => $size ] ]; } else { $params = [ 'index' => $this->index, // 'type' => $this->type, 'body' => [ /* 'query' => [ 'match_all'=>[] ],*/ 'sort' => [$order] , 'from' => $from, 'size' => $size ] ]; } try { $re = $this->client->search($params); } catch (\Exception $e) { echo ''; print_r($e->getMessage()); exit(); } return $re; } /** * 批量插入数据到索引 */ public function insertCorporation() { $corporations = Corporation::select(); foreach ($corporations as $corporation) { $corporation = $corporation->toArray(); $this->add_doc($corporation['id'], $corporation); } echo ''; print_r('完成了'); exit(); }}
实际案例:
转载地址:http://glyin.baihongyu.com/