4

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?)

rene
  • 41,474
  • 78
  • 114
  • 152
Fabian
  • 1,806
  • 5
  • 25
  • 40
  • boost is for giving importantce to a field more than other while Elasticsearch is searching https://www.elastic.co/guide/en/elasticsearch/guide/current/query-time-boosting.html – Amine Jallouli Dec 19 '15 at 04:06

1 Answers1

12

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.

bratek
  • 493
  • 5
  • 10
  • In order for us to have the ordering working we have to include the fields we want to order things with. Is there a way to avoid this? Otherwise, if we want order by id, then the ids will be searchable also – cellulosa Sep 12 '14 at 11:10
  • @cellulosa maybe this fits your use case http://stackoverflow.com/questions/17051709/no-mapping-found-for-field-in-order-to-sort-on-in-elasticsearch – tuxone Mar 02 '15 at 15:47
  • i have somehow same question, would you plz help me? http://stackoverflow.com/questions/36306089/sort-or-merge-result-in-foselasticabundle – parisssss Apr 01 '16 at 08:31