Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Note

laravel 5.3 에 도입된 scout 패키지를 사용하여 full text search engine 연계하는 방법에 대해 설명합니다.

...

  1. scout elastic search 패키지를 설치합니다.

    Code Block
    languagebash
    titlelaravel 6.x
    $ composer require babenkoivan/scout-elasticsearch-driver
    Code Block
    languagebash
    titlelaravel 5.8
    $ composer require babenkoivan/scout-elasticsearch-driver "^3.0"
    Note

    만약 ES 6을 사용한다면 3.12 버전을 설치해 줍니다.

  2. config/app.php 의 providers 에 프로바이더를 추가합니다. 개인적으로는 Laravel 의 Package Discovery 기능을 별로 좋아하지 않아서 직접 추가해 주는데 등록해 주지 않아도 최신 버전의 라라벨에서는 정상 동작합니다.

    Code Block
    'providers' => [
        Laravel\Scout\ScoutServiceProvider::class,
        ScoutElastic\ScoutElasticServiceProvider::class,
    ]
  3. config에 설정 파일을 생성하기 위해 vendor publish 수행합니다.

    Code Block
    php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
    php artisan vendor:publish --provider="ScoutElastic\ScoutElasticServiceProvider"
  4. 컨피그 파일(config/scout_elastic.php) 설정 변경이 필요하면 수정합니다.
  5. .env 에 다음 내용을 추가합니다.

    Code Block
    SCOUT_DRIVER=elasticsearchelastic
    
    ## laravel scout host
    SCOUT_ELASTIC_HOST=localhost:80
    Warning
    • nginx 를 붙여서 사용할 경우 SCOUT_ELASTIC_HOST 에 포트를 명시해야 합니다.(기본 9200)
    • URL 뒤에 trailing slash(Ex: my.site:80/) 를 붙이면 오작동하므로 주의하세요!
  6. Queue 를 사용하여 인덱싱할 경우 config/scout.php 의 다음 항목을 true 로 설정합니다.

    Code Block
    'queue' => true,

...

Code Block
languagephp
titleUserIndexConfigurator
<?php

namespace App;

use ScoutElastic\IndexConfigurator;
use ScoutElastic\Migratable;

class UserIndexConfigurator extends IndexConfigurator
{
    use Migratable;

    // index 이름을 직접 설정하려면 $name 변수에 이름을 지정해 주면 됩니다.
    // 생략할 경우 `IndexConfigurator` 를 제외한 snaked class 이름으로 생성됩니다.
    protected $name = 'user_index';

    // You can specify any settings you want, for example, analyzers.
     protected $settings = [
        'analysis' => [
            "tokenizer" => [
                "nori_user_dict" => [
                    "type" => "nori_tokenizer",
                    "decompound_mode" => "mixed",
                    "user_dictionary" => "userdict_ko.txt"
                ]
            ],
            'analyzer' => [
                'my_analyzer' => [
                    'type' => 'custom',
                    "tokenizer" => "nori_user_dict"
                ]
            ]
        ]
    ];

    // ES field 매핑
    protected $defaultMapping = [
        'properties' => [
            "id"=> [
                "type"=> "long"
            ],
            "name"=> [
                "type"=> "text",
                "analyzer"=> "my_analyzer",
                "search_analyzer"=> "my_analyzer"
            ],
            "email"=> [
                "type"=> "text",
                "analyzer"=> "my_analyzer",
                "search_analyzer"=> "my_analyzer"
            ],
            "created_at"=> [
                "type"=> "date",
                "format"=> "yyyy-MM-dd HH:mm:ss"
            ],
            "updated_at"=> [
                "type"=> "date",
                "format"=> "yyyy-MM-dd HH:mm:ss"
            ],
        ] // properties
    ];
}

...

Code Block
$ php artisan scout:import "App\MyModel"


Record 추가

Searchable 트레이트를 추가한 모델은  save() 메서드 호출시 자동으로 인덱싱됩니다.

...