cmsig/seal-solr-adapter
Apache Solr adapter for the SEAL search engine. Index and write documents to a SolrCloud instance using collections, with direct client setup or DSN-based configuration for common frameworks.
Install Dependencies:
composer require cmsig/seal cmsig/seal-solr-adapter solarium/solarium
solarium/solarium is required for Solr client functionality.Configure Solr Client:
Define a Solr client instance using Solarium’s Client class with a Curl adapter:
use Solr\Client;
use Solarium\Core\Client\Adapter\Curl;
use Symfony\Component\EventDispatcher\EventDispatcher;
$client = new Client(new Curl(), new EventDispatcher(), [
'endpoint' => [
'localhost' => [
'scheme' => 'http',
'host' => '127.0.0.1',
'port' => '8983',
'username' => 'solr',
'password' => 'SolrRocks',
],
],
]);
Initialize SEAL Engine:
Pass the Solr client to the SolrAdapter and integrate with SEAL’s schema:
use CmsIg\Seal\Adapter\Solr\SolrAdapter;
use CmsIg\Seal\Engine;
$engine = new Engine(new SolrAdapter($client), $schema);
First Use Case: Index a document:
$document = $engine->index('collection_name', [
'id' => '123',
'title' => 'Test Document',
'content' => 'Searchable content...',
]);
cmsig/seal for schema definitions and query patterns.Indexing Documents:
Use SEAL’s index() method to push documents to Solr collections:
$engine->index('products', [
'sku' => 'ABC123',
'name' => 'Laptop',
'price' => 999.99,
]);
products, articles).Querying: Leverage SEAL’s query builder for Solr-compatible syntax:
$results = $engine->search('products', [
'query' => 'laptop',
'filter' => ['price' => ['range' => ['from' => 500]]],
]);
Bulk Operations: Use Solr’s batch API via SEAL’s bulk methods:
$engine->bulkIndex('products', [
['id' => '1', 'name' => 'Item 1'],
['id' => '2', 'name' => 'Item 2'],
]);
Configuration Management:
.env and use DSN strings for flexibility:
SEAL_SOLR_DSN="solr://solr:SolrRocks@127.0.0.1:8983?tls=true"
$dsn = parse_url($_ENV['SEAL_SOLR_DSN']);
$client = new Client(new Curl(), new EventDispatcher(), [
'endpoint' => [$dsn['host'] => [
'scheme' => $dsn['scheme'] ?? 'http',
'host' => parse_url($dsn['host'], PHP_URL_HOST),
'port' => $dsn['port'] ?? 8983,
'username' => $dsn['user'] ?? null,
'password' => $dsn['pass'] ?? null,
]],
]);
Schema Alignment:
$schema = new \CmsIg\Seal\Schema\Schema([
'fields' => [
'id' => ['type' => 'string', 'required' => true],
'name' => ['type' => 'text'],
'price' => ['type' => 'float'],
],
]);
Event Handling:
DocumentIndexed) to trigger post-indexing logic:
$dispatcher->addListener('document.indexed', function ($event) {
Log::info('Indexed document: ' . $event->getDocument()->getId());
});
Error Handling:
try {
$engine->index('collection', $document);
} catch (\Solarium\Exception\SolrConnectionException $e) {
report($e);
// Fallback to cache or retry logic
}
Authentication:
configsets API requires authentication by default. Either:
solr.disableConfigSetsCreateAuthChecks=true in solrconfig.xml.Cloud Mode Requirements:
Schema Mismatches:
*_text) may cause unexpected behavior if not defined in Solr.Connection Timeouts:
$client = new Client(new Curl(), new EventDispatcher(), [
'timeout' => 5.0, // 5 seconds
]);
Enable Solarium Logging: Add a logger to the Solarium client to inspect raw Solr requests/responses:
use Psr\Log\LoggerInterface;
use Solarium\Core\Client\Adapter\Curl;
$adapter = new Curl();
$adapter->setLogger(new \Monolog\Logger('solr'));
$client = new Client($adapter, new EventDispatcher(), [...]);
Check Solr Logs:
$SOLR_HOME/logs) are invaluable for diagnosing issues like schema errors or query failures.Validate Collections:
if (!$engine->collectionExists('products')) {
$engine->createCollection('products');
}
Use DSN for Environment Flexibility:
solr://user:pass@host:port?tls=true) simplify switching between dev/staging/prod environments.Leverage Solr’s Atomic Updates:
update() method:
$engine->update('products', '123', [
'price' => ['set' => 899.99],
]);
Batch Indexing for Performance:
$engine->bulkIndex('products', array_map(fn ($item) => [
'id' => $item['id'],
'name' => $item['name'],
], $items));
Monitor Collection Health:
$status = $engine->getCollectionStatus('products');
Extend the Adapter:
SolrAdapter to add custom logic (e.g., pre-processing documents):
class CustomSolrAdapter extends SolrAdapter {
public function index($collection, array $document) {
$document['processed_at'] = now()->toDateTimeString();
return parent::index($collection, $document);
}
}
How can I help you explore Laravel packages today?