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 연계하는 방법에 대해 설명합니다.

...

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() 메서드 호출시 자동으로 인덱싱됩니다.

...