ZendSearch Lucene을 기반으로 하는 Eloquent 모델에 대한 전체 텍스트 검색을 위한 Laravel 5.5 패키지입니다.
작곡가.json에 이 패키지를 요구하고 작곡가 업데이트를 실행하십시오.
{
"require" : {
"nqxcode/laravel-lucene-search" : " 2.4.* "
}
}
작곡가를 업데이트한 후 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
목록을 반환하는 특수 접근자를 추가합니다. 기본적으로 getOptionalAttributesAttribute
접근자가 사용됩니다. 구성에 지정된 접근자 이름의 경우 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
접근자가 사용됩니다. 구성에 지정된 접근자 이름의 경우 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 로 설정됩니다. 이 동작을 변경하려면 다음 예와 같이 필수 필드에 부스트를 설정하세요.
구성 파일에서:
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
메소드의 경우 다음 옵션을 설정할 수 있습니다.
필드에 '하나 둘 구문 복합'과 같은 구문이 포함된 모든 모델을 찾습니다.
$ 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 라이선스에 따라 라이선스가 부여된 패키지입니다.