addSort() public method

Adds a sort param to the query.
public addSort ( mixed $sort )
$sort mixed Sort parameter
 public function findByUserAndTags(User $user, $tags = null)
 {
     $filterQuery = new Query\Filtered();
     $searchFilter = new Filter\Bool();
     $searchFilter->addMust(new Filter\Term(array('user' => $user->getId())));
     if (!empty($tags)) {
         $tagFilter = new Filter\Terms('tags', explode('+', $tags));
         $tagFilter->setExecution('and');
         $searchFilter->addMust($tagFilter);
     }
     $filterQuery->setFilter($searchFilter);
     $searchQuery = new Query($filterQuery);
     $searchQuery->addSort(array('createdAt' => 'desc'));
     $agg = new Aggregation\Terms('tags');
     $agg->setField('tags')->setOrder('_count', 'desc')->setSize(100);
     $searchQuery->addAggregation($agg);
     return $searchQuery;
 }
Example #2
0
 /**
  * @group unit
  */
 public function testAddSort()
 {
     $query = new Query();
     $sortParam = array('firstname' => array('order' => 'asc'));
     $query->addSort($sortParam);
     $this->assertEquals($query->getParam('sort'), array($sortParam));
 }
Example #3
0
 /**
  * Create a new elastica query from an array of queries.
  *
  * @param array $queries
  * @return Query
  */
 protected function newQuery(array $queries)
 {
     if (!empty($queries)) {
         $container = new BoolQuery();
         foreach ($queries as $query) {
             $container = $this->addQueryToContainer($query, $container);
         }
         $query = new ElasticaQuery($container);
         $query->addSort('_score');
     } else {
         $query = new ElasticaQuery();
     }
     return $query;
 }
Example #4
0
 /**
  * Perform a search based on the specified filter
  *
  * @param $typeTerms array
  * @param null $user
  * @param null $sortColumn
  * @param string $sortDirection
  * @param int $limit
  * @return array
  */
 public function filteredSearch($typeTerms, $user = null, $sortColumn = null, $sortDirection = 'desc', $limit = 50)
 {
     $query = new Query();
     $query->setFrom(0);
     $query->setSize($limit);
     $boolQuery = new BoolQuery();
     // Handle 'must' filters
     if (isset($typeTerms['must'])) {
         foreach ($typeTerms['must'] as $type => $term) {
             $termQueryType = new TermQuery([$type => $term]);
             $boolQuery->addMust($termQueryType);
         }
     }
     // Handle 'should' filters
     if (isset($typeTerms['should'])) {
         foreach ($typeTerms['should'] as $type => $term) {
             $termQueryType = new TermQuery([$type => $term]);
             $boolQuery->addShould($termQueryType);
         }
     }
     $query->setQuery($boolQuery);
     if ($user) {
         $userId = $user->id;
         $termQueryUser = new TermQuery(['user_id' => $userId]);
         $boolQuery->addMust($termQueryUser);
     }
     if ($sortColumn) {
         $query->addSort([$sortColumn => ['order' => $sortDirection]]);
     }
     $resultSet = $this->getTypedIndex()->search($query);
     return $this->getModels($resultSet);
 }
 /**
  *
  * @param Report $report        	
  * @param number $limit        	
  * @param string $sort        	
  * @param string $order        	
  * @param boolean $lazy        	
  * @param boolean $active        	
  * @param boolean $silent        	
  *
  * @return ArrayCollection $data
  */
 public function generateResults(Report $report, $limit = 0, $sort = '_score', $order = 'desc', $lazy = true, $active = true, $silent = false)
 {
     $data = new ArrayCollection();
     $request = $this->getServiceLocator()->get('Request');
     $silent = $request->getQuery('debug') ? false : $silent;
     if ($report instanceof Report) {
         $agent = $report->getAgent();
         $lead_query = new \Agent\Elastica\Query\BoolQuery();
         if ($active) {
             $lead_query->addMust(new Elastica\Query\Match('active', 1));
         }
         $client = $this->getElasticaClient();
         if ($agent && $client) {
             $filters = $agent->getFilter();
             if ($filters) {
                 $lead_query = $this->applyFilters($lead_query, $filters);
             }
             $criteria = $agent->getCriteria(false);
             if ($criteria) {
                 try {
                     $query = new Elastica\Query();
                     /* @var $criterion \Agent\Entity\AgentCriterion */
                     foreach ($criteria as $i => $criterion) {
                         $lead_query = $this->buildQuery($lead_query, $criterion);
                     }
                     $query->setQuery($lead_query);
                     $size = $limit ? $limit : 1000;
                     $query->setParam('track_scores', true);
                     $query->addSort([$sort => ['order' => $order]]);
                     $query->setSize($size);
                     $results = $this->runQuery($query, $lazy, $silent);
                     $total = isset($results['total']) ? $results['total'] : false;
                     if ($total && $results['results']) {
                         foreach ($results['results'] as $result) {
                             $data->add($result);
                         }
                     }
                     if ($total > $size) {
                         $limit = $limit ?: $total;
                         for ($page = 1; $page < ceil($limit / $size); $page++) {
                             $query->setFrom($page * $size);
                             $results = $this->runQuery($query, $silent);
                             if ($results['results']) {
                                 foreach ($results as $result) {
                                     if (is_array($result)) {
                                         foreach ($result as $r) {
                                             if ($r instanceof Result) {
                                                 $data->add($r);
                                             }
                                         }
                                     } elseif ($result instanceof Result) {
                                         $data->add($result);
                                     }
                                 }
                             }
                         }
                     }
                 } catch (\Exception $e) {
                     if (!$silent) {
                         $this->getFlashMessenger()->addErrorMessage($e->getMessage());
                     }
                 }
             }
         }
     }
     return $data;
 }
Example #6
0
 /**
  * @return ElasticaQuery
  */
 private function prepareQuery($params, $start = null, $limit = null)
 {
     $query = null;
     $filter = null;
     $sort = ['_score' => 'desc'];
     // We'd like to search in both title and description for keywords
     if (!empty($params['keywords'])) {
         $query = new QueryString($params['keywords']);
         $query->setDefaultOperator('AND')->setFields(['title', 'description']);
     }
     // Add location filter is location is selected from autosuggest
     if (!empty($params['location_id'])) {
         $location = Location::find($params['location_id']);
         $filter = new GeoDistance('location', ['lat' => $location->lat, 'lon' => $location->lon], $params['radius'] . 'mi');
         // Sort by nearest hit
         $sort = ['_geo_distance' => ['jobs.location' => [(double) $location->lon, (double) $location->lat], 'order' => 'asc', 'unit' => 'mi']];
     }
     // If neither keyword nor location supplied, then return all
     if (empty($params['keywords']) && empty($params['location_id'])) {
         $query = new MatchAll();
     }
     // We need a filtered query
     $elasticaQuery = new ElasticaQuery(new Filtered($query, $filter));
     $elasticaQuery->addSort($sort);
     // Offset and limits
     if (!is_null($start) && !is_null($limit)) {
         $elasticaQuery->setFrom($start)->setSize($limit);
     }
     // Set up the highlight
     $elasticaQuery->setHighlight(['order' => 'score', 'fields' => ['title' => ['fragment_size' => 100], 'description' => ['fragment_size' => 200]]]);
     return $elasticaQuery;
 }
 /**
  * @param Query      $query
  * @param SortConfig $sortConfig
  */
 protected function applyESSort(Query $query, SortConfig $sortConfig)
 {
     $column = $sortConfig->getColumn();
     if ($column) {
         $direction = $sortConfig->getDirection() ? 'desc' : 'asc';
         // null or false both default to ASC
         $query->addSort([$column => ['order' => $direction]]);
     }
 }