/** * 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); }
/** * Tests the find() and findAll() methods */ public function testFind() { $model = ExampleSolrActiveRecord::model()->find(); $behavior = $model->asa("ASolrSearchable"); /* @var ASolrSearchable $behavior */ $model->index(); $behavior->getSolrDocument()->getSolrConnection()->commit(); $criteria = new ASolrCriteria(); $criteria->query = "id:" . $model->id; $fromSolr = $behavior->findBySolr($criteria); $this->assertTrue($fromSolr instanceof ExampleSolrActiveRecord); foreach ($fromSolr->attributeNames() as $attribute) { $this->assertEquals($model->{$attribute}, $fromSolr->{$attribute}); } $criteria = new ASolrCriteria(); $criteria->setLimit(10); $results = $model->findAllBySolr($criteria); $this->assertEquals(10, count($results)); foreach ($results as $result) { $this->assertTrue($result instanceof ExampleSolrActiveRecord); } }
/** * Processes a list of results * @param SolrObject $rawResults the raw results to process * @param ASolrResultList $list the list of results */ protected function processResults($rawResults, $list = null) { if ($list === null) { $list = new ASolrResultList(); } $modelClass = $this->_modelClass; $highlighting = isset($this->_solrObject->highlighting); if ($highlighting) { $highlights = array_values((array) $this->_solrObject->highlighting); } if ($rawResults) { foreach ($rawResults as $n => $row) { $result = $modelClass::model()->populateRecord($row); /* @var ASolrDocument $result */ $result->setPosition($n + $this->_criteria->getOffset()); $result->setSolrResponse($this); if ($highlighting && isset($highlights[$n])) { $result->setHighlights($highlights[$n]); } $list->add($result); } } return $list; }
/** * Tests performing a facetted search without getting results at the same time */ public function testFacetsWithoutResults() { $connection = new ASolrConnection(); $connection->clientOptions->hostname = SOLR_HOSTNAME; $connection->clientOptions->port = SOLR_PORT; $connection->clientOptions->path = SOLR_PATH; $doc = new SolrInputDocument(); $doc->addField('id', 334455); $doc->addField('cat', 'Software'); $doc->addField('cat', 'Lucene'); $doc->addField("popularity", 20); $doc->addField("incubationdate_dt", date("Y-m-d\\TH:i:s\\Z")); $this->assertTrue($connection->index($doc)); $this->assertTrue($connection->commit()); $criteria = new ASolrCriteria(); $criteria->query = "lucene"; $criteria->offset = 0; $criteria->limit = 0; $criteria->facet = true; $criteria->addFacetField("cat")->addFacetField("name"); $criteria->addFacetDateField("incubationdate_dt"); $criteria->facetDateStart = "2005-10-10T00:00:00Z"; $criteria->facetDateEnd = "2015-10-10T00:00:00Z"; $criteria->facetDateGap = "+12MONTH"; $criteria->addFacetQuery("popularity:[* TO 10]"); $criteria->addFacetQuery("popularity:[10 TO 20]"); $criteria->addFacetQuery("popularity:[20 TO *]"); $response = $connection->search($criteria); $this->assertTrue($response instanceof ASolrQueryResponse); $this->assertTrue(isset($response->getDateFacets()->incubationdate_dt)); $this->assertTrue($response->getDateFacets()->incubationdate_dt instanceof ASolrFacet); $results = $response->getResults(); $this->assertEquals(0, count($results)); $this->assertTrue($connection->delete(334455)); $this->assertTrue($connection->commit()); }
/** * Finds multiple solr document that have the specified attribute values. * @param array $attributes list of attribute values (indexed by attribute names) that the solr documents should match. * @param ASolrCriteria $criteria The solr search criteria * @return ASolrDocument[] the documents found. */ public function findAllByAttributes($attributes, $criteria = null) { Yii::trace(get_class($this) . '.findAllByAttributes()', 'packages.solr.ASolrDocument'); if ($criteria === null) { $criteria = new ASolrCriteria(); } $query = array(); foreach ($attributes as $attribute => $value) { $query[] = $attribute . ":" . $value; } $criteria->setQuery(implode(" AND ", $query)); return $this->query($criteria, true); }
/** * Merges this criteria with another * @param ASolrCriteria $criteria the criteria to merge with * @return ASolrCriteria the merged criteria */ public function mergeWith(ASolrCriteria $criteria) { foreach ($criteria->getParams() as $name => $value) { if ($value === null) { continue; } if ($name == "q" && ($query = $this->getQuery()) != "") { $value = "(" . $query . ") AND (" . $criteria->getQuery() . ")"; } if (!is_array($value)) { $this->setParam($name, $value); } else { foreach ($value as $key => $val) { $this->addParam($name, $val); } } } return $this; }
/** * 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; }
/** * Makes a search query with the given criteria and returns the raw solr object. * Usually you should use the search() method instead. * @param ASolrCriteria $criteria the search criteria * @return SolrObject the response from solr */ protected function rawSearch(ASolrCriteria $criteria) { if ($this->enableProfiling) { $profileTag = "packages.solr.AConnection.rawSearch(" . $criteria->__toString() . ")"; Yii::beginProfile($profileTag); } $this->resetClient(); // solr client is not safely reusable, reset before every request $response = $this->getClient()->query($criteria)->getResponse(); if ($this->enableProfiling) { Yii::endProfile($profileTag); } return $response; }
/** * 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); }
/** * An example of a solr named scope * @return ExampleExtendedSolrDocument $this with the scope applied */ public function exampleScope() { $criteria = new ASolrCriteria(); $criteria->setLimit(100); $criteria->setQuery('name:test'); $this->getSolrCriteria()->mergeWith($criteria); return $this; }