栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Laravel + Elasticsearch 实现中文搜索

Laravel + Elasticsearch 实现中文搜索

安装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_proExtensionstmptmp"

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

 

 继续composer安装:

composer require tamayo/laravel-scout-elastic

composer require laravel/scout

php artisan vendor:publish

选择:LaravelScoutScoutServiceProvider

 

 修改驱动为 elasticsearch:

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

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

php artisan make:command ESOpenCommand

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

namespace AppConsoleCommands;

use ElasticsearchClientBuilder;
use IlluminateConsoleCommand; 



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 "AppArticle"
 'array'
    ];

    
    public function setContentAttribute($value)
    {
        $data = [
            'raw'  => $value,
            'html' => (new Markdowner)->convertMarkdownToHtml($value)
        ];

        $this->attributes['content'] = json_encode($data);
    }

    
    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
{
    
    protected $signature = 'command:search {query}';

    
    protected $description = 'Command description';

    
    public function __construct()
    {
        parent::__construct();
    }

    
    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 结合使用。

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/753248.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号