/** * Utilizes Solr's MoreLikeThis component to return similar pages * @see WikiaSearchTest::testMoreLikeThis * @param WikiaSearchConfig $searchConfig * @return WikiaSearchResultSet */ private function moreLikeThis(WikiaSearchConfig $searchConfig) { $query = $searchConfig->getQuery(WikiaSearchConfig::QUERY_RAW); $streamBody = $searchConfig->getStreamBody(); $streamUrl = $searchConfig->getStreamUrl(); if (!($query || $streamBody || $streamUrl)) { throw new Exception("A query, url, or stream is required."); } $mlt = $this->client->createMoreLikeThis(); $mlt->setMltFields(implode(',', $searchConfig->getMltFields()))->setFields($searchConfig->getRequestedFields())->addParam('mlt.match.include', 'false')->setStart($searchConfig->getStart())->setRows($searchConfig->getRows())->setDocumentClass('WikiaSearchResult'); if ($searchConfig->getInterestingTerms() == 'list') { $mlt->setInterestingTerms('list'); } if ($searchConfig->getMltFilterQuery()) { $mlt->addFilterQuery(array('query' => $searchConfig->getMltFilterQuery(), 'key' => 'mltfilterquery')); } if ($query !== null) { $mlt->setQuery($query); } else { if ($streamBody) { $mlt->addParam('stream.body', $streamBody); } else { if ($streamUrl) { $mlt->addParam('stream.url', $streamUrl); } } } try { $mltResult = $this->client->moreLikeThis($mlt); } catch (Exception $e) { $mltResult = F::build('Solarium_Result_Select_Empty'); Wikia::Log(__METHOD__, '', $e); } $results = F::build('WikiaSearchResultSet', array($mltResult, $searchConfig)); return $results; }
/** * Get a collection of the laws most similar to the present law. */ function get_related() { /* * The number of results to return. The default is 5. */ if (!isset($this->num_results)) { $this->num_results = 5; } /* * Intialize Solarium. */ $client = new Solarium_Client($GLOBALS['solr_config']); if ($client === FALSE) { return FALSE; } /* * Create a MoreLikeThis query instance. */ $query = $client->createMoreLikeThis(); /* * Note that we have to escape colons in this query. */ $query->setQuery('section:' . str_replace(':', '\\:', $this->section_number)); $query->setMltFields('text,tags,catch_line'); $query->setMatchInclude(TRUE); $query->setStart(0)->setRows($this->num_results); /* * Execute the query and return the result. */ $results = $client->select($query); /* * If our query fails. */ if ($results === FALSE) { return FALSE; } /* * Create a new, blank object to store our related sections. */ $related = new StdClass(); /* * Iterate through the returned documents */ $i = 0; foreach ($results as $document) { $law = new Law(); $law->law_id = $document->id; $law->get_law(); $related->{$i} = $law; $i++; } return TRUE; }
<?php require 'init.php'; htmlHeader(); // create a client instance $client = new Solarium_Client($config); // get a morelikethis query instance $query = $client->createMoreLikeThis(); $query->setQuery('id:SP2514N'); $query->setMltFields('manu,cat'); $query->setMinimumDocumentFrequency(1); $query->setMinimumTermFrequency(1); $query->createFilterQuery('stock')->setQuery('inStock:true'); $query->setInterestingTerms('details'); $query->setMatchInclude(true); // this executes the query and returns the result $resultset = $client->select($query); echo 'Document used for matching:<br/><table>'; foreach ($resultset->getMatch() as $field => $value) { // this converts multivalue fields to a comma-separated string if (is_array($value)) { $value = implode(', ', $value); } echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>'; } echo '</table><hr/>'; // display the total number of MLT documents found by solr echo 'Number of MLT matches found: ' . $resultset->getNumFound() . '<br/><br/>'; echo '<b>Listing of matched docs:</b>'; // show MLT documents using the resultset iterator foreach ($resultset as $document) {