安装Elasticsearch-php

https://github.com/elastic/elasticsearch-php

使用composer安装:

在项目目录下,执行以下命令

composer require elasticsearch/elasticsearch

配置php.ini

配置php.ini的sys_temp_dir

;sys_temp_dir = "/tmp"
sys_temp_dir = "D:\phpstudy_pro\Extensions\tmp\tmp"

否则,使用过程中可能会出现以下报错

继续composer安装:

composer require tamayo/laravel-scout-elasticcomposer require laravel/scoutphp artisan vendor:publish

选择:Laravel\Scout\ScoutServiceProvider

修改驱动为 elasticsearch

'driver' => env('SCOUT_DRIVER', 'elasticsearch'),

创建索引用 Laravel 自带的 Artisan 命令行功能。

php artisan make:command ESOpenCommand

下面就可以借助插件,创建我们的 Index,直接看代码:

namespace App\Console\Commands;use Elasticsearch\ClientBuilder;
use Illuminate\Console\Command; public function handle(){$host = config('scout.elasticsearch.hosts');$index = config('scout.elasticsearch.index');$client = ClientBuilder::create()->setHosts($host)->build();if ($client->indices()->exists(['index' => $index])) {$this->warn("Index {$index} exists, deleting...");$client->indices()->delete(['index' => $index]);}$this->info("Creating index: {$index}");return $client->indices()->create([// 生成索引的名称'index' => $index,// 类型 body'body' => ['settings' => [// 分区数'number_of_shards' => 1,// 副本数'number_of_replicas' => 0],'mappings' => ['_source' => ['enabled' => true],// 字段 类似表字段,设置类型'properties' => ['id' => [// 相当于数据查询是的 = 张三你好,必须找到张三你好'type' => 'long'],'title' => ['type' => 'text',// 中文分词 张三你好 张三 你好 张三你好'analyzer' => 'ik_max_word','search_analyzer' => 'ik_smart'],'subtitle' => ['type' => 'text',// 中文分词 张三你好 张三 你好 张三你好'analyzer' => 'ik_max_word','search_analyzer' => 'ik_smart'],'content' => ['type' => 'text',// 中文分词 张三你好 张三 你好 张三你好'analyzer' => 'ik_max_word','search_analyzer' => 'ik_smart']],]]]);}

创建mapping

php artisan esoc:mapping

Laravel Model 使用

Laravel 框架已经为我们推荐使用 Scout 全文搜索,我们只需要在 Article Model 加上官方所说的内容即可,很简单,推荐大家看 Scout 使用文档:Scout 全文搜索《Laravel 6 中文文档》,下面直接上代码:

Scout 全文搜索 | 官方扩展包 |《Laravel 6 中文文档 6.x》| Laravel China 社区 (learnku.com)

Scout 提供了 Artisan 命令 import 用来导入所有已存在的记录到搜索索引中。

php artisan scout:import "App\Article"
<?phpnamespace App;use App\Tools\Markdowner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;class Article extends Model
{use Searchable;protected $connection = 'blog';protected $table = 'articles';use SoftDeletes;/*** The attributes that should be mutated to dates.** @var array*/protected $dates = ['published_at', 'created_at', 'deleted_at'];/*** The attributes that are mass assignable.** @var array*/protected $fillable = ['user_id','last_user_id','category_id','title','subtitle','slug','page_image','content','meta_description','is_draft','is_original','published_at','wechat_url',];protected $casts = ['content' => 'array'];/*** Set the content attribute.** @param $value*/public function setContentAttribute($value){$data = ['raw'  => $value,'html' => (new Markdowner)->convertMarkdownToHtml($value)];$this->attributes['content'] = json_encode($data);}/*** 获取模型的可搜索数据** @return array*/public function toSearchableArray(){$data = ['id' => $this->id,'title' => $this->title,'subtitle' => $this->subtitle,'content' => $this->content['html']];return $data;}public function searchableAs(){return '_doc';}
}

有了数据,我们可以测试看看能不能查询到数据。

还是一样的,创建一个命令:

php artisan make:command ElasearchCommand
class ElasearchCommand extends Command
{/*** The name and signature of the console command.** @var string*/protected $signature = 'command:search {query}';/*** The console command description.** @var string*/protected $description = 'Command description';/*** Create a new command instance.** @return void*/public function __construct(){parent::__construct();}/*** Execute the console command.** @return mixed*/public function handle(){$article = Article::search($this->argument('query'))->first();$this->info($article->title);}
}

IK 分词器安装

Releases · medcl/elasticsearch-analysis-ik · GitHub

总结:

Elasticsearch 安装;
Elasticsearch IK 分词器插件安装;
Elasticsearch 可视化工具 ElasticHQ 和 Kibana 的安装和简单使用;
Scout 的使用;
Elasticsearch 和 Scout 结合使用。

《Elasticsearch-PHP 中文文档》 | PHP 技术论坛

Laravel + Elasticsearch 实现中文搜索 | Laravel China 社区 (learnku.com)

终于有人把Elasticsearch原理讲透了! - 知乎 (zhihu.com)

Laravel之ES搜索_Dragon-CSDN博客

Laravel + Elasticsearch 实现中文搜索相关推荐

  1. 四、深入elasticsearch中文搜索

    @Author : By Runsen @Date : 2020/6/12 作者介绍:Runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,Python, Java和一系列数据分析软件.导致翘 ...

  2. 【Elasticsearch】如何使用 Elasticsearch 6.2 搜索中文、日语和韩语文本 - 第 3 部分:语言检测工具

    1.概述 翻译:https://www.elastic.co/cn/blog/how-to-search-ch-jp-kr-part-3 这是我有关中文.日语和韩语文本搜索的系列文章的第 3 部分.如 ...

  3. 【Elasticsearch】如何使用 Elasticsearch 6.2 搜索中文、日文和韩文文本 - 第 2 部分: 多字段

    1.概述 翻译:https://www.elastic.co/cn/blog/how-to-search-ch-jp-kr-part-2 如何使用 Elasticsearch 6.2 搜索中文.日文和 ...

  4. Elasticsearch:如何使用 Elasticsearch 6.2 搜索中文、日文和韩文文本 - 第 1 部分: 分析工具

    共有 92 个国家和地区的代表队参加了去年的平昌冬奥会. 共有 49 个国家和地区的代表队参加了去年的平昌冬季残奥会. Elastic 的员工遍布全球 34 个国家/地区. 在很多国家/地区内,很多人 ...

  5. php to es7,只需五步 集成新版 Elasticsearch7.9 中文搜索 到你的 Laravel7 项目

    只需五步骤: 启动 集成 ik 中文分词插件的 Elasticsearch7.9 Docker 镜像 Laravel7 配置 Scout 配置 Model 模型 导入数据 搜索 PHP进阶30K资料, ...

  6. Elasticsearch之中文分词器插件es-ik(博主推荐)

    前提 什么是倒排索引? Elasticsearch之分词器的作用 Elasticsearch之分词器的工作流程 Elasticsearch之停用词 Elasticsearch之中文分词器 Elasti ...

  7. 【Elasticsearch】Elasticsearch analyzer 中文 分词器

    1.概述 转载: https://blog.csdn.net/tzs_1041218129/article/details/77887767 分词器首先看文章:[Elasticsearch]Elast ...

  8. Laravel Elasticsearch

    Laravel Elasticsearch  启动Elasticsearch :进入Elasticsearch文件bin目录     命令行运行elasticsearch.bat 设置Elastics ...

  9. ElasticSearch:为中文分词器增加对英文的支持(让中文分词器可以处理中英文混合文档)(转)

    本文地址,需转载请注明出处: http://blog.csdn.net/hereiskxm/article/details/47441911 当我们使用中文分词器的时候,其实也希望它能够支持对于英文的 ...

最新文章

  1. vs安装一直在提取文件_Visual Studio 2019下载及安装教程
  2. Windows CE 6.0 安装顺序
  3. python的sys模块有什么用_python sys模块详解
  4. put url带参数_避免自己写的 url 被diss!建议看看这篇RESTful API简明教程!
  5. Istio 1.9 发布——重点改善 Istio 的 Day2 操作
  6. Android 屏幕适配攻略(一)
  7. c 语言自行实现字符串常用库函数_学习c语言的7本书——你知道吗?
  8. 【共读Primer】52.[6.3]返回类型和return语句--返回数组指针 Page205
  9. 【记录】搭建本地wordpress全过程
  10. unity 获得当前物体_unity 获取物体尺寸
  11. 【数学建模】基于matlab GUI理发店排队模拟系统【含Matlab源码 1116期】
  12. eclipse 汉化
  13. Jasperreports5.6支持PDF微软雅黑字体
  14. springboot关闭http登录验证
  15. BCNF范式、第四范式和第五范式
  16. 分享电脑中截图的五种方法(包括截长图)
  17. 115、闪点、燃点、自然点的概念
  18. 当矩阵的秩小于未知数的个数时,方程组有无数个解;当矩阵的秩等于未知数的个数时,方程组只有零解。...
  19. 面试侃集合 | LinkedBlockingQueue篇
  20. SK6805MICRO-2427RGB灯珠 2427RGB内置IC灯珠 适用显示领域、智能应用、蓝牙WiFi装饰

热门文章

  1. WinEdit 的algorithm2e包自定义一个带竖线的模块代码
  2. vue元素显示隐藏 v-if 和 v-show 指令
  3. CDH通过parcels安装组件,激活时卡住,取消激活
  4. 软件测试应该何时介入项目?
  5. contiki学习笔记(八)rtimer stimer 计时器库
  6. 论如何通过真值表来求逻辑函数
  7. 18岁的融创正在成为地产圈“潮牌”
  8. [WDS] Disconnected!问题解决
  9. 电容降压整流电源电路
  10. 个人逾期,失信黑名单