Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting in FOSElasticaBundle

I use FOSElasticaBundle in my project to search on my Player entity. As i only want to search entities with the property isactive on a value 1, i followed the documentation on "Filtering Results and Executing a Default Query": FriendsOfSymfony/FOSElasticaBundle/README.md

$query = new \Elastica\Query\QueryString($searchterm);
$term = new \Elastica\Filter\Term(array('isactive' => true));

$filteredQuery = new \Elastica\Query\Filtered($query, $term);
$players = $this->get('fos_elastica.finder.xxx.player')->find($filteredQuery);

The configuration of my bundle looks like following:

fos_elastica:
clients:
    default: { host: localhost, port: 9200 }
serializer:
    callback_class: FOS\ElasticaBundle\Serializer\Callback
    serializer: serializer
indexes:
    xxx:
        client: default
        types:
            player:
                mappings:
                    firstname: { boost: 3 }
                    lastname: { boost: 3 }
                serializer:
                    groups: [elastica, Default]
                persistence:
                    driver: orm
                    model: xxx\FrontendBundle\Entity\Player
                    listener: ~
                    provider: ~
                    finder: ~

Now i want to do some sorting and cut back the result with limit and offset. How can i achieve this?

I found a solution like

$finalQuery = new \Elastica\Query($boolQuery);
$finalQuery->setSort(array('price' => array('order' => 'asc')));

But i dont have an Elastica\Query object and the AbstractQuery didn't support this method. Same with

$elasticaQuery->addSort($sort);

What to do? Where to read?? ://

(in addition, if we are here already: what does {boost: 3} really do exactly?)

like image 848
Fabian Avatar asked Dec 20 '22 18:12

Fabian


1 Answers

you have to create a generic Elastica\Query() object. Then you can add sort to this query with ->addSort($sort)

And later you can assign a proper query with ->setQuery();

Your example should look like this

$query = new \Elastica\Query();
$query->addSort(array('price' => array('order' => 'asc')));

$q = new \Elastica\Query\QueryString($searchterm);
$term = new \Elastica\Filter\Term(array('isactive' => true));
$filteredQuery = new \Elastica\Query\Filtered($q, $term);

$query->setQuery($filteredQuery);
$players = $this->get('fos_elastica.finder.xxx.player')->find($query);

Boost allows you to make one field more\less important than other within a query.

like image 79
bratek Avatar answered Jan 02 '23 17:01

bratek