Laravel 基于 Scout 配置实现 Elasticsearch (二)- 配置以及使用

导语

上篇文章搭建了 Elasticsearch 容器以及添加了测试数据,这篇来配置以及使用。

安装扩展包以及配置

  1. composer require tamayo/laravel-scout-elastic
  2. config/app.phpproviders 添加 Laravel\Scout\ScoutServiceProvider::classScoutEngines\Elasticsearch\ElasticsearchProvider::class,
  3. 发布配置文件 php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
  4. config/scout.php 添加
'elasticsearch' => [
    'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
    'hosts' => [
        env('ELASTICSEARCH_HOST', 'http://localhost'),
    ],
],
  1. 编辑 .env 文件,所以添加如下配置项
SCOUT_DRIVER=elasticsearch
ELASTICSEARCH_INDEX=laravel_index
ELASTICSEARCH_HOST=elasticsearch #因为环境是 laradock

修改模型配置

修改 app/Models/FakeArticle.php 文件如下

only('author', 'title', 'content');
    }
}

设置 Elasticsearch 分词策略

这一步是花费时间最多的地方,查的资料要么是过时的,要么根本不能运行。最终根据这篇文章修改而来。
关于 ik 分词以及 ik_max_wordik_smart 的区别,不在这里赘述了,可以看下这篇文章。

新建文件 php artisan make:command InitEs,编辑如下

createTemplate($client);
        $this->createIndex($client);
    }

    /**
     * 创建模板 see https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html
     * @param Client $client
     */
    private function createTemplate(Client $client)
    {
        $url = config('scout.elasticsearch.hosts')[0] . ':9200/_template/template_1';
        $client->put($url, [
            'json' => [
                'template' => config('scout.elasticsearch.index'),
                'settings' => [
                    'number_of_shards' => 1,
                ],
                'mappings' => [
                    '_default_' => [
                        'dynamic_templates' => [ // 动态映射模板
                            [
                                'string_fields' => [ // 字段映射模板的名称,一般为"类型_fields"的命名方式
                                    'match' => '*', // 匹配的字段名为所有
                                    'match_mapping_type' => 'string', // 限制匹配的字段类型,只能是 string 类型
                                    'mapping' => [ // 字段的处理方式
                                        'type' => 'text', // 字段类型限定为 string
                                        'analyzer' => 'ik_max_word', // 字段采用的分析器名,默认值为 standard 分析器
                                        'fields' => [
                                            'raw' => [
                                                'type' => 'keyword',
                                                'ignore_above' => 256, // 字段是索引时忽略长度超过定义值的字段。
                                            ]
                                        ],
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ]);
    }

    /**
     * 创建索引
     * @param Client $client
     */
    private function createIndex(Client $client)
    {
        $url = config('scout.elasticsearch.hosts')[0] . ':9200/' . config('scout.elasticsearch.index');

        $client->put($url, [
            'json' => [
                'settings' => [
                    'refresh_interval' => '5s',
                    'number_of_shards' => 1, // 分片为
                    'number_of_replicas' => 0, // 副本数
                ],
                'mappings' => [
                    '_default_' => [
                        '_all' => [
                            'enabled' => false, // 是否开启所有字段的检索
                        ],
                    ],
                ],
            ],
        ]);
    }
}

使用

  1. php artisan init:es
  2. php artisan scout:import "App\Models\FakeArticle"

  1. 搜索的时候使用 FakeArticle::search('搜索词')->get();

结语

测试后没有问题,可以正常搜索。更多的方法参考这里。


参考资料:Elastic Driver for Laravel Scout、Laravel Scout + Elasticsearch + ik 分词

你可能感兴趣的:(laravel,elasticsearch)