/** * Changes the query to a "More Like This" query. */ public function setMoreLikeThis(Query &$solarium_query, QueryInterface $query, $mlt_options = array(), $index_fields = array(), $fields) { $solarium_query = $this->solr->createMoreLikeThis(array('handler' => 'select')); // The fields to look for similarities in. if (empty($mlt_options['fields'])) { return; } $mlt_fl = array(); foreach ($mlt_options['fields'] as $mlt_field) { // Solr 4 has a bug which results in numeric fields not being supported // in MLT queries. // Date fields don't seem to be supported at all. $version = $this->getSolrVersion(); if ($fields[$mlt_field][0] === 'd' || $version == 4 && in_array($fields[$mlt_field][0], array('i', 'f'))) { continue; } $mlt_fl[] = $fields[$mlt_field]; // For non-text fields, set minimum word length to 0. if (isset($index_fields[$mlt_field]) && !SearchApiUtility::isTextType($index_fields[$mlt_field]->getType())) { $solarium_query->addParam('f.' . $fields[$mlt_field] . '.mlt.minwl', 0); } } //$solarium_query->setHandler('mlt'); $solarium_query->setMltFields($mlt_fl); /** @var \Solarium\Plugin\CustomizeRequest\CustomizeRequest $customizer */ $customizer = $this->solr->getPlugin('customizerequest'); $customizer->createCustomization('id')->setType('param')->setName('qt')->setValue('mlt'); // @todo Make sure these configurations are correct $solarium_query->setMinimumDocumentFrequency(1); $solarium_query->setMinimumTermFrequency(1); }
<?php require __DIR__ . '/init.php'; use Solarium\Client; htmlHeader(); // create a client instance $client = new 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