ZendSearch Lucene に基づく Eloquent モデルの全文検索用の Laravel 5.5 パッケージ。
このパッケージをcomposer.jsonに要求し、composer updateを実行します。
{
"require" : {
"nqxcode/laravel-lucene-search" : " 2.4.* "
}
}
Composer を更新した後、 config/app.php
のプロバイダー配列に ServiceProvider を追加します。
' providers ' => [
Nqxcode LuceneSearch ServiceProvider ::class,
],
ファサードを使用して検索したい場合は、これをconfig/app.php
のファサードに追加します。
' aliases ' => [
' Search ' => Nqxcode LuceneSearch Facade ::class,
],
次のコマンドを実行して、構成ファイルをプロジェクトに公開します。
php artisan vendor:publish --provider= " NqxcodeLuceneSearchServiceProvider "
公開された構成ファイルに、インデックスを作成する必要があるモデルの説明を追加します。例:
' index ' => [
// ...
namespace FirstModel ::class => [
' fields ' => [
' name ' , ' full_description ' , // fields for indexing
]
],
namespace SecondModel ::class => [
' fields ' => [
' name ' , ' short_description ' , // fields for indexing
]
],
namespace ModelWithCustomPrimaryKey ::class => [
// You can also define your primary key (if you use something else than "id")
' primary_key ' => ' my_custom_field_name ' ,
' fields ' => [
' username ' , ' short_description ' , // fields for indexing
]
],
// ...
],
オプションのフィールド(動的フィールド) の値にインデックスを付けることもできます。オプションのフィールドのインデックス作成を有効にするには:
' optional_attributes ' => true
// or
' optional_attributes ' => [
' accessor ' => ' custom_name ' // with specifying of accessor name
]
field-name => field-value
のリストを返す特別な accessorを追加します。デフォルトでは、 getOptionalAttributesAttribute
アクセサーが使用されます。 config で指定されたアクセサー名がある場合、 getCustomNameAttribute
アクセサーが使用されます。例:
設定ファイル内:
namespace FirstModel ::class => [
' fields ' => [
' name ' , ' full_description ' , // fixed fields for indexing
],
' optional_attributes ' => true // enable indexing for dynamic fields
],
モデルに次のアクセサーを追加します。
public function getOptionalAttributesAttribute ()
{
return [
' optional_attribute1 ' => ' value1 ' ,
' optional_attribute2 ' => ' value2 ' ,
];
}
「Apache Lucene - スコアリング」の詳細を参照してください。
これは、Apache Lucene の用語でのドキュメント レベルの向上です。デフォルトでは、すべてのモデルのブースト値は1です。この動作を変更するには、次の例のように必要なモデルのブーストをカスタマイズします。
' boost ' => true
// or
' boost ' => [
' accessor ' => ' custom_name ' // with specifying of accessor name
]
モデルに次のアクセサーを追加します。
public function getBoostAttribute ()
{
return 0.5 ; // customize boost value for model
}
getBoostAttribute
アクセサーが使用されます。 config で指定されたアクセサー名がある場合、 getCustomNameAttribute
アクセサーが使用されます。例:
設定ファイル内:
namespace FirstModel ::class => [
' fields ' => [
' name ' , ' full_description ' ,
],
' boost ' => true // enable boosting for model
],
モデルに次のアクセサーを追加します。
public function getBoostAttribute ()
{
return 0.5 ; // customize boost value for model
}
これは、Apache Lucene の用語でいうドキュメントのフィールド レベルのブーストです。デフォルトでは、各フィールドのブーストは1に設定されます。この動作を変更するには、次の例のように必要なフィールドに boost を設定します。
設定ファイル内:
namespace FirstModel ::class => [
' fields ' => [
' name ' , // field with default boost
' full_description ' => [ ' boost ' => 0.2 ], // customize boost value
],
],
または/およびモデル アクセサー内:
public function getOptionalAttributesAttribute ()
{
return [
' optional_attribute1 ' => ' value1 ' , // field with default boost
' optional_attribute2 ' => [ ' boost ' => 0.5 , ' value ' => ' value2 ' ], // customize boost value
];
}
デフォルトでは、次のフィルターが検索に使用されます。
このフィルターは削除したり、他のフィルターと置き換えたりすることができます。
' analyzer ' => [
' filters ' => [
// Default stemming filter.
Nqxcode Stemming TokenFilterEnRu ::class,
],
// List of paths to files with stopwords.
' stopwords ' => Nqxcode LuceneSearch Analyzer Stopwords Files :: get (),
],
検索インデックスを構築するには、次のコマンドを実行します。
php artisan search:rebuild --verbose
検索インデックスをクリアするには、次のコマンドを実行します。
php artisan search:clear
検索結果内のモデルをフィルタリングするために、各モデルのクラスはSearchableInterface
を実装できます。例えば:
use Illuminate Database Eloquent Model ;
use Nqxcode LuceneSearch Model SearchableInterface ;
class Dummy extends Model implements SearchableInterface
{
// ...
/**
* Get id list for all searchable models.
*/
public static function searchableIds ()
{
return self :: wherePublish ( true )-> pluck ( ' id ' );
}
// ...
}
必要なイベントの登録 (保存/更新/削除) には、ターゲット モデルでuse NqxcodeLuceneSearchModelSearchTrait
。
use Illuminate Database Eloquent Model ;
use Nqxcode LuceneSearch Model SearchableInterface ;
use Nqxcode LuceneSearch Model SearchTrait ;
class Dummy extends Model implements SearchableInterface
{
use SearchTrait ;
// ...
}
インデックス作成のトリガーを回避したい場合は、必要な操作をモデルのwithoutSyncingToSearch()
メソッドでラップします。
Product :: withoutSyncingToSearch ( function () {
// mass update position for product, e.g.
foreach ( Product :: all () as $ i => $ product ) {
$ product -> update ([ ' position ' => $ i )]);
}
});
いくつかの方法でクエリを作成します。
デフォルトでは、フレーズ全体の検索を実行するクエリが作成されます。
$ query = Search :: query ( ' clock ' ); // search by all fields.
// or
$ query = Search :: where ( ' name ' , ' clock ' ); // search by 'name' field.
// or
$ query = Search :: query ( ' clock ' ) // search by all fields with
-> where ( ' short_description ' , ' analog ' ); // filter by 'short_description' field.
// or
$ query = Product :: search ( ' clock ' ); // search only in `Product` model by all fields in case when `Product` use `SearchableTrait`.
query
およびwhere
メソッドでは、次のオプションを設定できます。
いずれかのフィールドに「composite one twophrase」のようなフレーズが含まれるすべてのモデルを検索します。
$ query = Search :: query ( ' composite phrase ' , ' * ' , [ ' proximity ' => 2 ]);
クエリ内の各単語で検索します。
$ query = Search :: query ( ' composite phrase ' , ' * ' , [ ' phrase ' => false ]);
$ query = Search :: rawQuery ( ' short_description:"analog" ' );
// or
$ rawQuery = QueryParser :: parse ( ' short_description:"analog" ' );
$ query = Search :: rawQuery ( $ rawQuery );
構築されたクエリでは、次のアクションが利用可能です。
$ models = $ query -> get ();
$ count = $ query -> count ();
$ models = $ query -> limit ( 5 , 10 )-> get (); // Limit = 5 and offset = 10
$ paginator = $ query -> paginate ( 50 );
一致の強調表示は、 utf-8でエンコードされた任意の html フラグメントで使用でき、最後に実行されたリクエストに対してのみ実行されます。
Search :: find ( ' nearly all words must be highlighted ' )-> get ();
$ highlighted = Search :: highlight ( ' all words ' );
// highlighted html:
// '<span class="highlight">all</span> <span class="highlight">words</span>'
MIT ライセンスに基づいてライセンスされたパッケージ。