daniel-iwaniec/elasticsearch-bundle
Installation
composer require ongr/elasticsearch-bundle
Ensure ONGRELASTICSEARCH_BUNDLE is enabled in config/bundles.php.
Configure Elasticsearch Connection
Add to config/packages/ongr_elasticsearch.yaml:
ongr_elasticsearch:
clients:
default:
url: '%env(ES_URL)%'
index_name: 'your_index'
Define a Document Entity
use ONGR\ElasticsearchBundle\Annotation as ES;
#[ES\Document]
class Product
{
#[ES\Property(type: 'text')]
private $name;
#[ES\Property(type: 'integer')]
private $price;
}
First Query
use ONGR\ElasticsearchBundle\Result\ResultInterface;
use ONGR\ElasticsearchBundle\Finder\PaginatedFinderInterface;
$finder = $this->get('ongr_elasticsearch.finder.paginated');
/** @var ResultInterface $result */
$result = $finder->find(Product::class, ['name' => 'test']);
Use the Domain-Specific Language (DSL) for complex queries:
use ONGR\ElasticsearchBundle\Finder\PaginatedFinderInterface;
use ONGR\ElasticsearchBundle\Query\Builder;
$queryBuilder = new Builder();
$queryBuilder
->from(Product::class)
->addQuery(
$queryBuilder->bool()
->should(
$queryBuilder->match()->field('name')->value('test'),
$queryBuilder->range()->field('price')->gt(100)
)
)
->setSize(10);
$finder->findBy($queryBuilder->getQuery());
IndexManager to create/update indices:
$indexManager = $this->get('ongr_elasticsearch.index_manager');
$indexManager->createIndex(Product::class);
BulkIndexer for batch inserts:
$bulkIndexer = $this->get('ongr_elasticsearch.bulk_indexer');
$bulkIndexer->index($products); // Array of Product entities
$paginatedFinder = $this->get('ongr_elasticsearch.finder.paginated');
$results = $paginatedFinder->find(Product::class, [], 1, 10); // Page 1, 10 items
$cursorFinder = $this->get('ongr_elasticsearch.finder.cursor');
$cursor = $cursorFinder->find(Product::class, ['price' => ['gt' => 0]]);
foreach ($cursor as $product) {
// Process each product
}
php bin/console ongr:es:document:generate --index=products --class=Product
php bin/console ongr:es:reindex --index=products --entity=App\Entity\Product
Index Name Conflicts
index_name in config matches your document’s @Document annotation.@Document(indexName="custom_index").Mapping Mismatches
php bin/console ongr:es:mapping:regenerate
$client = $this->get('ongr_elasticsearch.client');
$client->indices()->getMapping(['index' => 'your_index']);
Connection Issues
ES_URL in .env (e.g., http://localhost:9200).ONGRELASTICSEARCH_BUNDLE__CLIENTS__DEFAULT__TIMEOUT to adjust timeouts.Annotation Caching
@ES\Property fields:
php bin/console cache:clear
ongr_elasticsearch:
clients:
default:
logger: true
$query = $finder->getQueryBuilder()->getQuery();
dump($query->toArray()); // Raw Elasticsearch query
Custom Query Builders
Extend ONGRELASTICSEARCH\ElasticsearchBundle\Query\Builder for domain-specific logic.
Event Subscribers
Listen to ONGRELASTICSEARCH\ElasticsearchBundle\Event\IndexEvent for pre/post-index hooks:
use ONGR\ElasticsearchBundle\Event\IndexEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
IndexEvent::PRE_INDEX => 'onPreIndex',
];
}
public function onPreIndex(IndexEvent $event)
{
// Modify document before indexing
}
}
Custom Index Managers
Implement ONGRELASTICSEARCH\ElasticsearchBundle\Index\IndexManagerInterface for advanced index control.
select() in queries to limit returned fields:
$queryBuilder->select(['name', 'price']);
search_after for Deep Pagination:
$queryBuilder->setSearchAfter([$lastSortValue]);
How can I help you explore Laravel packages today?