环境:centos 6 php73 mysql56  ELASTIC7.71

1.安装elastic  使用华为云镜像更快哦 https://mirrors.huaweicloud.com/elasticsearch/

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 elastic
useradd -g elastic elastic
chown -R elastic.elastic /etc/elasticsearch-7.7.1
vim 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

https://github.com/medcl/elasticsearch-analysis-ik/releases

不管你用何种方法  放到安装路径里  /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

<?phpnamespace app\index\controller;use app\admin\model\Corporation;
use app\index\model\SystemUploadfile;
use app\common\controller\IndexController;
use app\common\service\MenuService;
use EasyAdmin\upload\Uploadfile;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use Elasticsearch\Common\Exceptions\ElasticsearchException;
use Elasticsearch\Common\Exceptions\Missing404Exception;
use think\db\Query;
use think\facade\Cache;class Ela extends IndexController
{private $client;// 构造函数public function __construct(){//$host = ['120.26.205.129:9200'];$host = ['127.0.0.1:9200'];$this->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 '<pre>';print_r($this->get_doc($v['id']));}exit();*/
//        echo '<pre>';
//        print_r($this->get_doc(512));
//        exit();//        $re = $this->search_doc("我做无敌",0,2); //4.搜索结果
//
//        echo '<pre>';
//        print_r($re);
//        exit();echo '<pre>';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 '<pre>';print_r('success');exit();*/}// 查看映射public function get_mapping(){$params = ['index' => $this->index,];$re = $this->client->indices()->getMapping($params);echo '<pre>';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 '<pre>';print_r('未找到对应数据');exit();}echo '<pre>';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 '<pre>';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 '<pre>';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 '<pre>';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 '<pre>';print_r('完成了');exit();}}

  实际案例:http://www.huixx.cn/search/corporation.html?keyword=

ELASTIC-PHP + IK分词器 + THINKPHP6 初次使用 (关键词查询)相关推荐

  1. Ik分词器(自定义分词-mysql)

    引言:ik分词器的分词范围不够广泛.某些特定行业的专业用语分词能力就不够了,此时就需要自定义分词,与停顿词. 1.下载ik分词器源码 git地址:https://github.com/medcl/el ...

  2. elastic ik分词搜索_php环境下使用elasticSearch+ik分词器进行全文搜索

    php中文网最新课程 每日17点准时技术干货分享 首先需要说明的一点是,如果需要启用ik分词器,那么分词器的版本必须与es版本一致,即6.3.0的分词器需要同样6.3.0版本的es支持. 安装java ...

  3. 2021年大数据ELK(八):Elasticsearch安装IK分词器插件

    全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 系列历史文章 安装IK分词器 一.下载Elasticsearch IK分词器 ...

  4. ik分词器 mysql php_php环境下使用elasticSearch+ik分词器进行全文搜索

    首先需要说明的一点是,如果需要启用ik分词器,那么分词器的版本必须与es版本一致,即6.3.0的分词器需要同样6.3.0版本的es支持. 安装java win-64bit的安装包需要去java英文官网 ...

  5. Docker 安装 ES 7.7.0 及 Head、Kibana、IK分词器、Logstash、Filebeat 插件

    目录 环境信息 ES安装 ElasticSearch-Head安装 IK分词器安装 环境信息 Docker version 1.13.1, build 4ef4b30/1.13.1 CentOS Li ...

  6. Elasticsearch(二) ik分词器的安装 以及 自定义分词

    ik分词器作为elasticsearch的一个插件,则是安装在es插件中. ik分词器的安装 1,创建ik分词目录上传与es相同版本的ik分词器插件,不同版本可能es启动 报错 在elasticsea ...

  7. Elasticsearch+elasticsearch-head的安装+Kibana环境搭建+ik分词器安装

    一.安装JDK1.8 二.安装ES 三个节点:master.slave01.slave02 1.这里下载的是elasticsearch-6.3.1.rpm版本包 https://www.elastic ...

  8. 服务器安装配置elasticsearch,kibana,IK分词器和拼音分词器,集群搭建教程

    docker安装配置elasticsearch,kibana和IK分词器 elasticsearch文章系列 前置安装docker 创建docker网络 安装Elasticsearch 运行elast ...

  9. ElasticSearch03_Mapping字段映射、常用类型、数据迁移、ik分词器、自定义分词器

    文章目录 ①. Mapping字段映射概述 ②. 常用类型如下 - text.keyword ③. 映射中对时间类型详解 ④. ES的keyword的属性ignore_above ⑤. 映射的查看.创 ...

  10. 商城项目18_esMapping字段映射、常用类型、数据迁移、ik分词器、自定义分词器

    文章目录 ①. Mapping字段映射概述 ②. 常用类型如下 - text.keyword ③. 映射中对时间类型详解 ④. ES的keyword的属性ignore_above ⑤. 映射的查看.创 ...

最新文章

  1. MYSQL 数据库迁移 ***
  2. C++ 对数组sizeof 和对数组元素sizeof
  3. 【深度学习】从零开始 Mask RCNN 实战:基于 Win10 + Anaconda 的 Mask RCNN 环境搭建
  4. Linux系统管理初步(七)系统服务管理、chkconfig与systemd 编辑中
  5. 要让Fiddler能够监控加密过后的HTTPS请求,需要执行哪些步骤?
  6. win10 x64+anaconda+labelme标注数据
  7. c#输入三个数选出最大的_C#习题,3、 输入三个数字,输出他们的最大值。(if) | 学步园...
  8. php什么集成框架比较好,php哪个框架比较好?
  9. input的一些使用方法
  10. 自定义Dialog宽度占满屏幕
  11. Silverlight/Windows8/WPF/WP7/HTML5周学习导读(8月5日-8月12日)
  12. 【全网最全面C语言教程】C语言从入门到精通
  13. 走近冰球运动·体育项目
  14. html 字体围绕图片效果
  15. XP pro下安装Windows XP Tablet PC 2005组件教程
  16. 《中英文在自然语言处理上的十大差异点》学习总结
  17. 常见的接口测试 开源网站
  18. Golang的广东11选5出售协程调度机制与GOMAXPROCS性能调优
  19. ArrayList集合学生管理系统,java笔试基础题
  20. 精通CSS(5.6.3-end)PixyFairypureCSSToolHintscale

热门文章

  1. 不使用CAD转换工具,你能转换CAD文件格式吗?
  2. PLC中如何区分源型漏型
  3. 内外网隔离 双网隔离DoraOS云终端双桌面云办公应用
  4. 移动端(html5)富文本编辑器,vue移动端中使用vue-html5-editor富文本编辑器详解
  5. 分享一个学习充电的电子书下载网站(目前可以免费下载电子书)
  6. Eclipse搭建Android开发环境并运行Android项目 (详细)
  7. 中国最新7座超大城市、14座特大城市完整榜单
  8. 生成QQ/MSN/旺旺/SKYPE等在线状态图标
  9. php 银行卡识别,PHP实现根据银行卡号判断银行_php技巧
  10. 规则引擎drools教程一