laravelsolr
Version 1.0.0
Apache Solr とのシームレスな統合のための Laravel パッケージ。コア管理用の使いやすいコマンドと Solr 操作用の滑らかなインターフェイスを提供します。
composer require haiderjabbar/laravelsolr:dev-main
サービスプロバイダーをconfig/app.php
に追加します。
' providers ' => [
// ...
HaiderJabbar LaravelSolr LaravelSolrServiceProvider ::class,
];
config/solr.php 構成ファイルを次のように公開する必要があります。
php artisan vendor:publish --provider= " HaiderJabbarLaravelSolrLaravelSolrServiceProvider "
# Create a new Solr core and migration file
php artisan solr:create-core coreName
# Update a Solr core with a new name
php artisan solr:update-core
# Delete a Solr core and its migration file
php artisan solr:delete-core coreName
# Create new Solr fields with optional parent and fields
php artisan solr:create-fields coreName
# Update existing Solr core fields
php artisan solr:update-fields
# Delete fields from a Solr core
php artisan solr:delete-fields
use HaiderJabbar LaravelSolr Models SolrModel ;
$ coreName = ' your_core_name ' ;
$ data = [
' id ' => ' unique_id ' ,
' name ' => ' document_name ' ,
// ... other fields
];
$ result = SolrModel :: addDocument ( $ coreName , $ data );
$ result = SolrModel :: updateDocument ( $ coreName , $ data );
$ result = SolrModel :: deleteDocumentById ( $ coreName , $ id );
// Add child documents to a parent
$ result = SolrModel :: addChildToParent ( $ coreName , $ parentId , " child " , $ childData );
use HaiderJabbar LaravelSolr Services SolrQueryBuilder ;
use Illuminate Http Request ;
use HaiderJabbar LaravelSolr Models SolrModel ;
class SolrController extends Controller
{
public function addDocument ( Request $ request )
{
$ coreName = ' testCore ' ;
$ data = [
' id ' => 1 ,
' name ' => ' name '
];
$ result = SolrModel :: addDocument ( $ coreName , $ data );
return $ result
? response ()-> json ([ ' message ' => ' Document added successfully ' ])
: response ()-> json ([ ' message ' => ' Failed to add document ' ], 500 );
}
public function addChildDocument ( Request $ request , $ parentId )
{
$ coreName = ' testCore ' ;
$ childData = $ request -> input ( ' childData ' );
$ result = SolrModel :: addChildToParent ( $ coreName , $ parentId , " child " , $ childData );
if ( $ result ) {
return response ()-> json ([ ' message ' => ' Child documents added successfully ' ]);
}
return response ()-> json ([ ' message ' => ' Failed to add child documents ' ], 500 );
}
}
laravelsolr は、 SolrQueryBuilder
クラスを通じて Solr クエリを構築するための強力で流暢なインターフェイスを提供します。
use HaiderJabbar LaravelSolr Services SolrQueryBuilder ;
$ builder = new SolrQueryBuilder ( $ coreName );
// Basic search
$ builder -> search ( ' field ' , ' = ' , ' value ' , $ boost );
// OR search condition
$ builder -> orSearch ( ' field ' , ' value ' , $ boost );
// Where clause
$ builder -> where ( ' field ' , ' = ' , ' value ' , $ priority );
// OR where clause
$ builder -> orWhere ( ' field ' , ' = ' , ' value ' , $ priority );
// WHERE IN clause
$ builder -> whereIn ( ' field ' , [ ' value1 ' , ' value2 ' ]);
// Filter (fq parameter)
$ builder -> filter ( ' field ' , ' = ' , ' value ' );
利用可能な演算子:
=
完全一致!=
等しくない<
以下>
より大きい<=
以下>=
以上like
in
値の配列 $ builder
-> sort ( ' field asc ' ) // Add sorting
-> start ( 0 ) // Starting offset (pagination)
-> rows ( 10 ); // Number of rows to return
$ builder -> fl ([ ' field1 ' , ' field2 ' , ' score ' ]); // Select specific fields to return
// Get parent with child
$ builder
-> whereParent ( ' id ' , ' = ' , ' parent_id ' , 20 )
-> returnBothParentAndChild ()
-> get ();
// Get only children
$ builder
-> whereParent ( ' id ' , ' = ' , ' parent_id ' , 20 )
-> returnOnlyChild ()
-> fl ([ ' * ' , ' score ' ])
-> get ();
// Get parent only
$ builder
-> whereParent ( ' id ' , ' = ' , ' parent_id ' , 20 )
-> returnOnlyParent ()
-> get ();
// Query where parent and child
$ builder
-> whereParent ( ' id ' , ' = ' , ' parent_id ' , 20 )
-> whereChild ( ' childId ' , ' = ' , ' child_id ' , 20 )
-> get ();
// Simple join
$ builder -> crossCollectionJoin (
' otherCore ' , // From index
' fromField ' , // Field in from index
' toField ' , // Field in current index
' field:value ' // Optional query
);
// Complex join with conditions
$ builder -> whereJoin ( ' otherCore ' , ' fromField ' , ' toField ' , function ( $ q ) {
$ q -> where ( ' field1 ' , ' = ' , ' value1 ' )
-> where ( ' field2 ' , ' = ' , ' value2 ' );
});
// Cross collection join example
$ result = $ builder
-> whereParent ( ' id ' , ' = ' , ' parent_id ' , 20 )
-> whereJoin ( ' otherCore ' , ' id ' , ' id ' , function ( $ q ) {
$ q -> where ( " id " , " = " , " value1 " , 20 )
-> where ( " id " , " = " , " value2 " , 20 );
})
-> returnBothParentAndChild ()
-> getWithJoinedDocuments ( " otherCore " , " id " , " id " );
// Basic faceted search
$ builder
-> facet ( true )
-> facetFields ([ ' field1 ' , ' field2 ' ])
-> get ();
// Complex faceted search
$ builder
-> whereParent ( ' id ' , ' = ' , ' parent_id ' , 20 )
-> whereChild ( ' childId ' , ' = ' , ' child_id ' , 20 )
-> facetFields ([ ' id ' ])
-> facet ( true )
-> fl ([ ' * ' , ' score ' ])
-> get ();
// Basic query execution
$ results = $ builder -> get ();
// Get results with facets
$ results = $ builder -> getWithFacets ();
// Get results with joined documents
$ results = $ builder -> getWithJoinedDocuments ( ' otherCore ' , ' fromId ' , ' toId ' );
$ builder = new SolrQueryBuilder ( $ coreName );
$ results = $ builder
-> whereParent ( ' category ' , ' = ' , ' electronics ' , 20 )
-> whereChild ( ' price ' , ' >= ' , 100 )
-> facet ( true )
-> facetFields ([ ' brand ' , ' color ' ])
-> sort ( ' price desc ' )
-> start ( 0 )
-> rows ( 20 )
-> fl ([ ' id ' , ' name ' , ' price ' , ' score ' ])
-> returnBothParentAndChild ()
-> get ();
.env
ファイルに Solr URL を設定してください。
SOLR_URL = http://localhost:8983/solr
laravelsolr/
├── src/
│ ├── Console/Commands/
│ │ ├── CreateSolrCore.php
│ │ ├── CreateSolrFields.php
│ │ ├── DeleteSolrCore.php
│ │ ├── DeleteSolrFields.php
│ │ ├── UpdateSolrCore.php
│ │ └── UpdateSolrFields.php
│ ├── Models/
│ │ └── SolrModel.php
│ ├── Schema/
│ │ └── SolrSchemaBuilder.php
│ ├── Services/
│ │ ├── CoreSolrService.php
│ │ ├── FieldsSolrService.php
│ │ └── SolrQueryBuilder.php
│ ├── laravelsolr.php
│ ├── LaravelSolrServiceProvider.php
│ └── SolrServiceProvider.php
└── tests/
└── Unit/
├── Console/Commands/
├── Models/
└── Services/
パッケージには、すべての機能の包括的なテストが含まれています。テストを実行するには:
vendor/bin/phpunit
貢献は大歓迎です!お気軽にプルリクエストを送信してください。
このパッケージは、MIT ライセンスに基づいてライセンスされたオープンソース ソフトウェアです。