e1on/omines-datatables-elasticsearch-adapter-bundle
Install the Bundle
composer require e1on/omines-datatables-elasticsearch-adapter-bundle
Enable the bundle in config/bundles.php:
return [
// ...
E1on\OminesDatatablesElasticsearchAdapterBundle\OminesDatatablesElasticsearchAdapterBundle::class => ['all' => true],
];
Configure Elasticsearch Client
Add Elasticsearch client configuration to config/packages/e1on_omines_datatables_elasticsearch_adapter.yaml:
e1on_omines_datatables_elasticsearch_adapter:
clients:
default:
host: 'elasticsearch'
port: 9200
First Use Case: Basic DataTable Define a DataTable with Elasticsearch adapter in a controller or service:
use E1on\OminesDatatablesElasticsearchAdapterBundle\ElasticaAdapter;
$table = $this->createDataTable()
->setName('logs') // Required: Unique name per table
->add('timestamp', DateTimeColumn::class, ['field' => '@timestamp'])
->add('message', TextColumn::class, ['globalSearchable' => true])
->createAdapter(ElasticaAdapter::class, [
'client' => 'default', // Uses configured client
'index' => 'logs-*',
]);
Define DataTable Structure Use Omines DataTables Bundle to define columns and mappings:
$table = $this->createDataTable()
->setName('users')
->add('id', NumberColumn::class, ['field' => 'id', 'orderable' => true])
->add('name', TextColumn::class, ['globalSearchable' => true, 'field' => 'name.keyword'])
->add('email', EmailColumn::class, ['field' => 'email']);
Configure Elasticsearch Adapter
Pass adapter-specific options to createAdapter():
->createAdapter(ElasticaAdapter::class, [
'client' => 'custom_client', // Named client from config
'index' => 'users_index',
'type' => '_doc', // Elasticsearch type (default: '_doc')
'searchAfter' => true, // Enable search_after for pagination (default: true)
'size' => 10, // Results per page (default: 10)
'from' => 0, // Starting offset (ignored with search_after)
'sort' => ['@timestamp' => ['order' => 'desc']], // Required for search_after
]);
Handle Pagination with search_after
The adapter automatically uses search_after for efficient pagination. Ensure your DataTable frontend supports passing the last sorted value for subsequent requests.
Global Search Integration
Leverage Elasticsearch’s query_string or multi_match for global search:
->add('global', TextColumn::class, [
'globalSearchable' => true,
'field' => ['name.keyword', 'email', 'description'],
]);
return $this->json($table->getData());
logs-*) or aliases for flexible index targeting.->createAdapter(ElasticaAdapter::class, [
'client' => 'default',
'cache' => ['enabled' => true, 'ttl' => 300], // Cache for 5 minutes
]);
->createAdapter(ElasticaAdapter::class, [
'mappings' => [
'properties' => [
'nested_field' => [
'type' => 'nested',
'properties' => [...],
],
],
],
]);
->createAdapter(ElasticaAdapter::class, [
'aggregations' => [
'status_counts' => ['terms' => ['field' => 'status']],
],
]);
Missing setName()
DataTable name is required.setName() with a unique identifier (e.g., 'logs', 'users').search_after Requires Sorting
sort field in the adapter config (e.g., ['@timestamp' => ['order' => 'desc']]). search_after relies on the last sorted value.Field Mismatches
field option (e.g., field => 'name.keyword' for keyword searches).Index/Type Not Found
NoSuchIndexException or NoSuchTypeException.index and type (default: _doc) exist in Elasticsearch. Use wildcards (e.g., logs-*) cautiously.Performance Issues
size values or unoptimized queries.size (e.g., size: 10) and use search_after for pagination. Avoid from with large offsets.Enable Elasticsearch Logging
Add to config/packages/monolog.yaml:
handlers:
elastica:
type: stream
path: "%kernel.logs_dir%/elastica.log"
level: debug
channels: ["elastica"]
Then configure the Elastica client to log queries:
$client = Elastica\Client::create([
'host' => 'elasticsearch',
'logger' => new \Monolog\Logger('elastica'),
]);
Inspect Raw Queries Use the adapter’s debug mode to dump Elasticsearch queries:
$table->createAdapter(ElasticaAdapter::class, [
'client' => 'default',
'debug' => true, // Dumps query to Symfony profiler
]);
Validate DataTable Configuration
Use the validate() method to check for errors:
if (!$table->validate()) {
throw new \RuntimeException('DataTable validation failed: ' . $table->getErrors());
}
Client Configuration
config/packages/e1on_omines_datatables_elasticsearch_adapter.yaml:
e1on_omines_datatables_elasticsearch_adapter:
clients:
custom_client:
host: 'elasticsearch:9200'
port: 9200
timeout: 5
'client' => 'custom_client'
search_after Behavior
from/size pagination when search_after is enabled (default: true).from/size, explicitly set searchAfter: false (not recommended for large datasets).Field Data Types
keyword, text) must align with DataTable column types:
TextColumn for text fields.KeywordColumn for keyword fields.DateTimeColumn for date fields.use E1on\OminesDatatablesElasticsearchAdapterBundle\ElasticaAdapter;
How can I help you explore Laravel packages today?