Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Sonic driver for Laravel Scout

License

NotificationsYou must be signed in to change notification settings

james2doyle/laravel-scout-sonic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Search Eloquent Models usingSonic indexes.

  1. Implementation
  2. Installation
  3. Usage

Implementation

When implementing thetoSearchableArray method, you need to provide an array that will be coerced into a string (the engine just joins with a' ') as Sonic can only index strings. So you need to provide a "stringified" (index string) version of your model. The defaulttoArray works but it is probably too much noise for reasonable usage.

Here is an example of the string I used when I was developing this Engine:

publicfunctiontoSearchableArray(){returnarray_filter([$this->display_name,$this->first_name,$this->last_name]);}

For me, this builds a nice string for search that can match on a "name". In my application, the concept of "name" is either the User display name or first/last name.


If the locale is known, you can also create thegetSonicLocale() method on your model, which returns the locale. It will then get passed to the SonicPUSH calls:

// an ISO 639-3 locale code eg. eng for English (if set, the locale must be a valid ISO 639-3 code; if set to none, lexing will be disabled; if not set, the locale will be guessed from text)publicfunctiongetSonicLocale() {return'none';}

Installation

If you haven't already you shouldinstall Laravel Scout toyour project and apply theLaravel\Scout\Searchable trait to any Eloquent models you would like to make searchable.

Install this package viaComposer

composer require james2doyle/laravel-scout-sonic

Note: if you have Laravel >= 5.5 you can skip this step because of Package Auto-Discovery.

Next add the ServiceProvider to the Package Service Providers inconfig/app.php

/* * Package Service Providers... */james2doyle\SonicScout\Providers\SonicScoutServiceProvider::class,

Append the default configuration toconfig/scout.php

/*|--------------------------------------------------------------------------| Sonic Configuration|--------------------------------------------------------------------------|| Here you may configure your Sonic settings.|*/'sonic' => ['address' =>\env('SONIC_ADDRESS','localhost'),'port' =>\env('SONIC_PORT',1491),'password' =>\env('SONIC_PASSWORD'),'connection_timeout' =>\env('SONIC_CONNECTION_TIMEOUT',10),'read_timeout' =>\env('SONIC_READ_TIMEOUT',5)],

SetSCOUT_DRIVER=sonic in your.env file

In addition there is no need to use thephp artisan scout:import command.

Usage

Simply call thesearch() method on yourSearchable models:

$users = App\User::search('bro')->get();

Simple constraints can be applied using thewhere() builder method:

$users = App\User::search('bro')->where('active', 1)->get();

Note: Sonic does not support the concept of "where", so the where is applied at the collection level not the query!

Pagination

Sonic cannot support real pagination because Sonic does not return proper paging or total information. It simply returns all the results for a given query.

There is a naive implementation of pagination in place but it probably isn't perfect as it doesn't take into account the "where" filter.

For more usage information see theLaravel Scout Documentation.

About

Sonic driver for Laravel Scout

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors3

  •  
  •  
  •  

Languages


[8]ページ先頭

©2009-2025 Movatter.jp