I am new to doctrine mongodb query builder and trying to combine query builder and mongodb to do a full text search in php(Symfony 2.6) first of all I have indexed the field of the collection I want to search in. and then, in my action I have the query below:
$dm = $this->getDoctrineMongoDB()->getManager();
$qb = $dm->createQueryBuilder('MyBundle:MyCollection');
$expr = $qb->expr()->operator('$text', array(
'$search' => 'I love stackoverflow',
'$language' => 'none', // or one of those other languages
),
array('score' => array('$meta' => 'textScore'))
);
$qb->field(null)->equals($expr->getQuery());
$qb->sort(array('score' => array('$meta' => 'textScore')));
This query returns the desired results but it does not sort them. I mean it does not sort the results based on the score of each document that matches with the query. and I have gone through all of the text search and sort documents in mongoDb and stack overflow answers like php mongodb full-text search and sort and MongoDB+Doctrine: How to sort the query by text search score but none of them helped me fix this query textScore-based sorting. I really need your help and every bit of your help will be much appreciated.
Fortunately I found the answer to my question. My doctrine mongodb was old and that`s why query builder did not recognize the method sortMeta() So I upgraded the doctrine/mongodb version 1.0 to the last version 1.2.x-dev and the code below getting from this link MongoDB+Doctrine: How to sort the query by text search score works like a charm .