SCOUT AND 검색(searchBoolean)

Admin 2020.05.05 23:58 조회 수 : 38

라라벨 SCOUT 검색 설치

<!--// 1. Install SCOUT -->
# composer require laravel/scout
# php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"


<!--// 2. Add the Laravel\Scout\Searchable trait to the model -->
use Laravel\Scout\Searchable;
class 모델명 extends Model
{
    use Searchable;
    public $asYouType = true;
    /**
     * Get the indexable data array for the model.
     *
     * @return array
     */
    public function toSearchableArray()
    {
        $array = $this->toArray();
        // ID, NAME등 조건 검색일 경우
        $array = collect($this->toArray())->only(['id', 'name'])->toArray();
        return $array;
    }

    public function getScoutKey()
    {
        return $this->id;
    }
}


<!--// 3. Modify "config/scout.php" -->
'driver' => env('SCOUT_DRIVER', 'tntsearch'),
'queue' => env('SCOUT_QUEUE', true),
'tntsearch' => [
  'storage'  => env('TNTSEARCH_STORAGE', storage_path()), //place where the index files will be stored
  'fuzziness' => env('TNTSEARCH_FUZZINESS', false),
  'fuzzy' => [
    'prefix_length' => 2,
    'max_expansions' => 50,
    'distance' => 2
  ],
  'asYouType' => false,
  'searchBoolean' => env('TNTSEARCH_BOOLEAN', false), // AND 검색일 경우 true
],


<!--// 4. Modify "php.ini" extension -->
extension=pdo_sqlite
extension=sqlite3


<!--// 5. install tntsearch -->
#composer require teamtnt/laravel-scout-tntsearch-driver


<!--// 6. Modify "config/app.php" -->
'providers' => [
    // ...
    Laravel\Scout\ScoutServiceProvider::class,
    TeamTNT\Scout\TNTSearchScoutServiceProvider::class,
],


<!--// 7. Add SCOUT_DRIVER=tntsearch to your .env file -->
SCOUT_DRIVER=tntsearch


<!--// 8. add the following line to your .gitignore file. -->
/storage/*.index


<!--// 9. Then, create index in terminal -->
#php artisan scout:import App\모델명


<!--// 10. 컨트롤러에 검색 구문 추가 -->
$child = new ChildProfile;
$child = ChildProfile::search($request->search)->orderBy('id', 'asc');
$childrens = $child->paginate(16);