cmsig/seal-opensearch-adapter
OpenSearch adapter for the cmsig/search SEAL project. Write and index documents in an OpenSearch server via the OpenSearch PHP client, usable directly or via DSN (TLS and basic auth supported). Still under active development.
Install the Package
composer require cmsig/seal-opensearch-adapter
Ensure cmsig/seal is also installed as a dependency.
Configure OpenSearch Client
Use the OpenSearch\ClientBuilder to create a client instance:
use OpenSearch\ClientBuilder;
use CmsIg\Seal\Adapter\Opensearch\OpensearchAdapter;
use CmsIg\Seal\Engine;
$client = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
Initialize SEAL Engine
Pass the adapter and your schema to the Engine:
$engine = new Engine(new OpensearchAdapter($client), $schema);
First Use Case: Indexing a Document
$document = new Document($schema, ['id' => '1', 'title' => 'Test']);
$engine->index($document);
OpensearchAdapter.php for adapter-specific logic.Indexing Documents
Use the Engine to index documents with SEAL’s schema:
$engine->index($document);
Searching Leverage SEAL’s query builder for OpenSearch-compatible queries:
$results = $engine->search(new Query\Match('title', 'Test'));
Bulk Operations Use SEAL’s bulk helpers for efficiency:
$bulk = new Bulk();
$bulk->addIndex($document1);
$bulk->addIndex($document2);
$engine->bulk($bulk);
Laravel Service Provider
Bind the Engine in a service provider for dependency injection:
$this->app->singleton(Engine::class, function ($app) {
$client = ClientBuilder::create()->setHosts(config('opensearch.hosts'))->build();
return new Engine(new OpensearchAdapter($client), $app->make(Schema::class));
});
Configuration via .env
Use DSN strings for flexibility:
OPENSEARCH_DSN=opensearch://username:password@127.0.0.1:9200?tls=true
Schema Management
Define your schema in a Laravel model or config file, then pass it to the Engine.
Schema Mismatch Ensure your SEAL schema aligns with OpenSearch’s mapping. SEAL abstracts this, but complex mappings (e.g., nested objects) may require manual adjustments.
Connection Issues
Verify OpenSearch server availability and credentials. Use try-catch for connection errors:
try {
$engine->index($document);
} catch (\OpenSearch\Common\Exceptions\CurlException $e) {
Log::error('OpenSearch connection failed: ' . $e->getMessage());
}
Bulk Operation Limits OpenSearch has bulk request size limits. Monitor payload size in bulk operations.
Enable OpenSearch Logging Configure the client for verbose logging:
$client = ClientBuilder::create()
->setHosts(['127.0.0.1:9200'])
->setLogger(new \Monolog\Logger('opensearch'))
->build();
Check Raw Responses Access the underlying OpenSearch response for debugging:
$response = $engine->search($query)->getRawResponse();
Custom Mappings
Override the adapter’s getMapping method to customize field mappings:
class CustomOpensearchAdapter extends OpensearchAdapter {
public function getMapping(Schema $schema) {
return [
'properties' => [
'title' => ['type' => 'text', 'analyzer' => 'custom_analyzer']
]
];
}
}
Query Modifiers Extend SEAL’s query builder to add OpenSearch-specific features (e.g., runtime fields).
TLS Configuration For secure connections, configure TLS in the client builder:
$client = ClientBuilder::create()
->setHosts(['127.0.0.1:9200'])
->setTlsVerification(true)
->build();
Use SEAL’s Query DSL Leverage SEAL’s query builder for portable, engine-agnostic queries.
Monitor Performance Profile bulk operations and large searches for performance bottlenecks.
Community Feedback Engage with the SEAL discussions for best practices.
How can I help you explore Laravel packages today?