alpixel/elastica-query-sorter-bundle
Installation:
composer require alpixel/elastica-query-sorter-bundle
Register the bundle in AppKernel.php:
new Alpixel\Bundle\ElasticaQuerySorterBundle\AlpixelElasticaQuerySorterBundle(),
Basic Configuration (config.yml):
alpixel_elastica_query_sorter:
views:
clear_sort: "path/to/clear_sort_template.html.twig"
sort_link: "path/to/sort_link_template.html.twig"
item_per_page: 10 # Optional: Default items per page
First Use Case:
Query and Repository:
$results = $this->get('alpixel.services.elastica_query_sorter')->sort($repository, $query);
elastica_sort() helper:
<th>{{ elastica_sort('Name', 'name') }}</th>
Controller Integration:
$repository = $this->get('fos_elastica.manager')->getRepository('AppBundle:Product');
$query = $repository->queryCustom($filters);
$results = $this->get('alpixel.services.elastica_query_sorter')->sort($repository, $query);
results to Twig for rendering.Twig Integration:
elastica_sort() in table headers to generate clickable sort links:
<th>{{ elastica_sort('Price', 'price.value') }}</th>
twitter_bootstrap3_translated):
{{ pagerfanta(results, 'twitter_bootstrap3_translated') }}
Dynamic Sorting:
?sort=price&direction=desc).{{ elastica_sort('Name', 'name', {'default_direction': 'desc'}) }}
Nested Fields:
product.serial_number) by passing dot-notation:
{{ elastica_sort('Serial', 'product.serial_number') }}
elastica_sort(). Example:
# config/elastica.yml
AppBundle\Document\Product:
properties:
price:
type: float
product:
properties:
serial_number:
type: string
$form = $this->createForm(MySearchForm::class);
$form->handleRequest($request);
$query = $repository->queryCustom($form->getData());
sort_link.html.twig, clear_sort.html.twig) in Resources/views/blocks/.Deprecated Bundle:
QueryBuilder changes).Twig Helper Dependencies:
elastica_sort() relies on the alpixel_elastica_query_sorter.views.sort_link template. If missing, sort links break.Resources/views/blocks/sort_link.html.twig to your project.Pagination Conflicts:
white_october/pagerfanta-bundle). Ensure both bundles are properly initialized.results is a Pagerfanta\Pagerfanta instance. If not, the sorter may not be applied.Nested Field Sorting:
product.serial_number) assumes the nested field exists in the mapping. Error: Silent failures if the path is invalid.$client = $this->get('fos_elastica.client');
$mapping = $client->getIndex('your_index')->getType('AppBundle:Product')->getMapping();
Request Parameter Overrides:
?sort=field&direction=asc/desc. Gotcha: Custom route parameters (e.g., @Route("/search/{sort}", ...)) may conflict.Request::query to merge defaults:
$sort = $request->query->get('sort', 'name');
$direction = $request->query->get('direction', 'asc');
Log Queries:
# config.yml
fos_elastica:
client: ~
debug: true
Elastica\Query objects.Template Debugging:
sort_link.html.twig to add debug info:
{{ dump(app.request.query.all()) }}
Service Availability:
php bin/console debug:container alpixel.services.elastica_query_sorter
php bin/console cache:clear
Custom Sort Logic:
Alpixel\Bundle\ElasticaQuerySorterBundle\Service\ElasticaQuerySorter:
# config/services.yml
services:
app.elastica_query_sorter:
class: AppBundle\Service\CustomElasticaQuerySorter
parent: alpixel.services.elastica_query_sorter
arguments: ['@fos_elastica.manager']
sort() method.Dynamic Field Mapping:
elastica_sort() to dynamically resolve fields:
{{ elastica_sort('Dynamic Field', app.callback('getSortField', ['field'])) }}
$twig->addFunction(new \Twig_SimpleFunction('callback', function ($name, $args) {
return $this->get($name)->__invoke($args);
}));
Alternative Pagination:
sort() method to return a Knp\Component\Pager\Pagination\PaginationInterface.How can I help you explore Laravel packages today?