/** * Tests the mergeWith() method */ public function testMergeWith() { $criteria = new ASolrCriteria(); $criteria->addField("id"); $criteria->query = "id:1"; $criteria2 = new ASolrCriteria(); $criteria2->addField("name"); $criteria2->query = "id:2"; $criteria->addFilterQuery("test:1"); $criteria2->addFilterQuery("test:2"); $criteria->mergeWith($criteria2); $this->assertTrue(in_array("name", $criteria->getFields())); $this->assertEquals("(id:1) AND (id:2)", $criteria->query); }
/** * Counts the number of rows that match the given criteria * @param ASolrCriteria $criteria the search criteria * @return integer the number of matching rows */ public function count(ASolrCriteria $criteria) { $c = new ASolrCriteria(); $c->mergeWith($criteria); $c->setLimit(0); Yii::trace('Counting Results from Solr: ' . (string) $c, 'packages.solr.ASolrConnection'); return $this->rawSearch($c)->response->numFound; }
/** * 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; }
/** * Finds all active records that matches the given criteria using solr * @param ASolrCriteria $criteria the solr criteria to use for searching * @return CActiveRecord[] an array of results */ public function findAllBySolr($criteria = null) { $c = new ASolrCriteria(); $c->mergeWith($this->getSolrCriteria()); if ($criteria !== null) { $c->mergeWith($criteria); } if ($c->getQuery() == "") { $c->setQuery("*:*"); } return $this->populateFromSolr($this->getSolrDocument()->findAll($c), true); }