Table of Contents |
---|
Note |
---|
laravel 5.3 에 도입된 scout 패키지를 사용하여 full text search engine 연계하는 방법 |
사전 준비
...
방법에 대해 설명합니다. |
설치
scout elastic search 패키지 설치패키지를 설치합니다.
Code Block language bash title laravel 6.x $ composer require babenkoivan/scout-elasticsearch-driver
Code Block language bash title laravel 5.8 $ composer require babenkoivan/scout-elasticsearch-driver "^3.0"
Note 만약 ES 6을 사용한다면 3.12 버전은 Elastic 6.x 필요버전을 설치해 줍니다.
config/app.php 의 providers 에 아래 내용 추가 (No Package Discovery !) 프로바이더를 추가합니다. 개인적으로는 Laravel 의 Package Discovery 기능을 별로 좋아하지 않아서 직접 추가하는 것이며 등록해 주지 않아도 최신 버전의 라라벨에서는 정상 동작합니다.
Code Block 'providers' => [ Laravel\Scout\ScoutServiceProvider::class, ScoutElastic\ScoutElasticServiceProvider::class, ]
config에 설정 파일을 생성하기 위해 vendor publish 수행 수행합니다.
Code Block php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider" php artisan vendor:publish --provider="ScoutElastic\ScoutElasticServiceProvider"
- 컨피그 파일(config/scout_elastic.php) 설정 변경이 필요하면 수정합니다.
.env 에 다음 내용을 추가하고 설정추가합니다.
Code Block SCOUT_DRIVER=elasticsearch # This prefix may be useful if you have multiple| "tenants" or applications sharing the same search infrastructure. SCOUT_PREFIX='' ELASTICSEARCH_INDEX=laravel ##ELASTICSEARCH_HOST=localhost:9200 ## laravel scout host SCOUT_ELASTIC_HOST=http://my-elastic.sitelocalhost:80
Warning - nginx 를 붙여서 사용할 경우 SCOUT_ELASTIC_HOST 에 포트를 명시해야 함합니다.(기본 9200)
- URL 뒤에 trailing slash(Ex: my.site:80/) 를 붙이면 오작동하므로 주의주의하세요!
Queue 를 사용하여 인덱싱할 경우 config/scout.php 의 다음 항목을 true 로 설정설정합니다.
Code Block 'queue' => true,
Index configurator
모델별로 index 를 생성해 줘야 하며 다음 명령어로 인덱스 설정 클래스를 생성할 수 있습니다.
관례상 클래스 이름은 "모델명 + IndexConfigurator" 로 지으면 되며 아래는 Users 테이블용 index configurator 를 생성합니다.
Code Block |
---|
php artisan make:index-configurator MyIndexConfiguratorUserIndexConfigurator |
Code Block | ||||
---|---|---|---|---|
| ||||
<?php namespace App; use ScoutElastic\IndexConfigurator; use ScoutElastic\Migratable; class MyIndexConfiguratorUserIndexConfigurator extends IndexConfigurator { use Migratable; // It's not obligatory to determine name. By default it'll be a index 이름을 직접 설정하려면 $name 변수에 이름을 지정해 주면 됩니다. // 생략할 경우 `IndexConfigurator` 를 제외한 snaked class name without `IndexConfigurator` part이름으로 생성됩니다. protected $name = 'myuser_index'; // You can specify any settings you want, for example, analyzers. protected $settings = [ 'analysis' => [ 'analyzer' => [ 'korean' => [ 'type' => 'standard', 'stopwords' => '_korean_' ] ] ] ]; // ES field 매핑 protected $defaultMapping = [ 'properties' => [ "id"=> [ "type"=> "long" ], "name"=> [ "type"=> "text", "analyzer"=> "korean", "search_analyzer"=> "korean" ], "email"=> [ "type"=> "text", "analyzer"=> "korean", "search_analyzer"=> "korean" ], "created_at"=> [ "type"=> "date", "format"=> "yyyy-MM-dd HH:mm:ss" ], "updated_at"=> [ "type"=> "date", "format"=> "yyyy-MM-dd HH:mm:ss" ], ] // properties ]; } |
이제 인덱스를 생성해 줍니다. 만약 Mapping 이 달라지면 update-index 명령을 실행해야 합니다.
Code Block |
---|
$ art elastic:create-index "App\MyIndexConfiguratorUserIndexConfigurator" The my_index index was created! The my_index_write alias for the my_index index was created! |
...
localhost:9200/my_index 에 연결연결해서 인덱스가 생겼는지 확인해 봅니다.
Code Block |
---|
Searchable Data
...
Code Block |
---|
$ php artisan scout:import "App\User" |
Record 추가
Laravel\Scout\Searchable 트레이트를 추가한 모델은 save() 메서드 호출시 자동으로 인덱싱
Code Block |
---|
$order = new App\Order; // ... $order->save(); |
...