if ($mset->get_docid(0) != 2) { print "MatchDecider mset has wrong docid in\n"; exit(1); } } class testexpanddecider extends XapianExpandDecider { function apply($term) { return $term[0] !== 'a'; } } $enquire = new XapianEnquire($db); $rset = new XapianRSet(); $rset->add_document(1); $eset = $enquire->get_eset(10, $rset, XapianEnquire::USE_EXACT_TERMFREQ, 1.0, new testexpanddecider()); foreach ($eset->begin() as $t) { if ($t[0] === 'a') { print "XapianExpandDecider was not used\n"; exit(1); } } # Check min_wt argument to get_eset() works (new in 1.2.5). $eset = $enquire->get_eset(100, $rset, XapianEnquire::USE_EXACT_TERMFREQ); $min_wt = 0; foreach ($eset->begin() as $i => $dummy) { $min_wt = $i->get_weight(); } if ($min_wt >= 1.9) { print "ESet min weight too high for testcase\n"; exit(1);
$enquire->set_query($query); $matches = $enquire->get_mset(0, 10, $rset); // Display the results. print "{$matches->get_matches_estimated()} results found:\n"; foreach ($matches->begin() as $i => $docid) { $n = $i->get_rank() + 1; $data = $i->get_document()->get_data(); print "{$n}: {$i->get_percent()}% docid={$docid} [{$data}]\n\n"; } // If no relevant docids were given, invent an RSet containing the top 5 // matches (or all the matches if there are less than 5). if ($rset->is_empty()) { $c = 5; foreach ($matches->begin() as $docid) { $rset->add_document($docid); if (--$c) { break; } } } // Generate an ESet containing terms that the user might want to add to // the query. $eset = $enquire->get_eset(10, $rset); // List the terms. foreach ($eset->begin() as $t => $term) { print "{$term}: weight = {$t->get_weight()}\n"; } } catch (Exception $e) { print $e->getMessage() . "\n"; exit(1); }
/** * Return a list of posts that are similar to the current post */ public function get_similar_posts($post, $max_recommended = 5) { $guid = $this->get_uid($post); $posting = $this->_database->postlist_begin($guid); $enquire = new XapianEnquire($this->_database); $rset = new XapianRset(); $rset->add_document($posting->get_docid()); $eset = $enquire->get_eset(20, $rset); $i = $eset->begin(); $terms = array(); while (!$i->equals($eset->end())) { $terms[] = $i->get_term(); $i->next(); } $query = new XapianQuery(XapianQuery::OP_OR, $terms); $enquire->set_query($query); $matches = $enquire->get_mset(0, $max_recommended + 1); $ids = array(); $i = $matches->begin(); while (!$i->equals($matches->end())) { $n = $i->get_rank() + 1; if ($i->get_document()->get_value(self::XAPIAN_FIELD_ID) != $post->id) { $ids[] = $i->get_document()->get_value(self::XAPIAN_FIELD_ID); } $i->next(); } return $ids; }