/** * Tests the results functions */ public function testResults() { $queryResponse = new ASolrQueryResponse($this->mockSolrObject(), $this->mockSolrCriteria()); $results = $queryResponse->getResults(); $this->assertEquals(3, $results->count()); foreach ($results as $n => $result) { $this->assertEquals("test item " . ($n + 1), $result->name); $this->assertEquals(1.0, $result->getScore()); } }
/** * Fetches the data from the persistent data storage. * @return array list of data items */ protected function fetchData() { $criteria = new ASolrCriteria(); $criteria->mergeWith($this->getCriteria()); if (($pagination = $this->getPagination()) !== false) { $pagination->setItemCount(999999999); // set to an unreasonably high value to save an extra request $pagination->applyLimit($criteria); } if (($sort = $this->getSort()) !== false) { $sort->applyOrder($criteria); } if ($this->model instanceof CActiveRecord) { // this should be a model with ASolrSearchable attached if ($this->loadFromDB) { $results = $this->model->getSolrDocument()->findAll($criteria); $this->_solrQueryResponse = $this->model->getSolrDocument()->getSolrConnection()->getLastQueryResponse(); $ids = array(); foreach ($results as $n => $item) { $ids[$n] = $item->getPrimaryKey(); } if (!empty($ids)) { $c = new CDbCriteria(); $fields = $ids; array_unshift($fields, $this->model->getTableAlias() . '.' . $this->model->getMetaData()->tableSchema->primaryKey); $c->order = 'FIELD(' . implode(',', $fields) . ')'; // keep the order of objects as it is from solr's results $data = $this->model->findAllByPk($ids, $c); $ids = array_flip($ids); foreach ($data as $n => $model) { $model->setSolrDocument($results[$ids[$model->getPrimaryKey()]]); } } else { $data = array(); // prevent any errors } } else { $data = $this->model->findAllBySolr($criteria); $this->_solrQueryResponse = $this->model->getSolrDocument()->getSolrConnection()->getLastQueryResponse(); } } else { $data = $this->model->findAll($criteria); $this->_solrQueryResponse = $this->model->getSolrConnection()->getLastQueryResponse(); } if ($pagination) { $pagination->setItemCount($this->_solrQueryResponse->getResults()->total); } return $data; }