exit(1); } # Test GeoSpatial API $coord = new XapianLatLongCoord(); $coord = new XapianLatLongCoord(-41.288889, 174.777222); define('COORD_SLOT', 2); $metric = new XapianGreatCircleMetric(); $range = 42.0; $centre = new XapianLatLongCoords($coord); $query = new XapianQuery(new XapianLatLongDistancePostingSource(COORD_SLOT, $centre, $metric, $range)); $db = Xapian::inmemory_open(); $coords = new XapianLatLongCoords(); $coords->append(new XapianLatLongCoord(40.6048, -74.4427)); $doc = new XapianDocument(); $doc->add_term("coffee"); $doc->add_value(COORD_SLOT, $coords->serialise()); $db->add_document($doc); $centre = new XapianLatLongCoords(); $centre->append(new XapianLatLongCoord(40.6048, -74.4427)); $ps = new XapianLatLongDistancePostingSource(COORD_SLOT, $centre, $metric, $range); $q = new XapianQuery("coffee"); $q = new XapianQuery(XapianQuery::OP_AND, $q, new XapianQuery($ps)); $enq = new XapianEnquire($db); $enq->set_query($q); $mset = $enq->get_mset(0, 10); if ($mset->size() != 1) { print "Expected one result with XapianLatLongDistancePostingSource, got "; print $mset->size() . "\n"; exit(1); } $s = '';
$sorter = new XapianMultiValueKeyMaker(); $sorter->add_value(0, true); $sorter->add_value(1, true); $enquire->set_sort_by_key($sorter, true); $mset = $enquire->get_mset(0, 10); mset_expect_order($mset, array(1, 2, 3, 4, 5)); $md = new XapianValueSetMatchDecider(0, true); $md->add_value("ABC"); $doc = new XapianDocument(); $doc->add_value(0, "ABCD"); if ($md->apply($doc)) { print "Unexpected result from ValueSetMatchDecider->apply(); expected false\n"; exit(1); } $doc = new XapianDocument(); $doc->add_value(0, "ABC"); if (!$md->apply($doc)) { print "Unexpected result from ValueSetMatchDecider->apply(); expected true\n"; exit(1); } $mset = $enquire->get_mset(0, 10, 0, null, $md, null); mset_expect_order($mset, array(2)); $md = new XapianValueSetMatchDecider(0, false); $md->add_value("ABC"); $mset = $enquire->get_mset(0, 10, 0, null, $md, null); mset_expect_order($mset, array(1, 3, 4, 5)); function mset_expect_order($mset, $a) { if ($mset->size() != sizeof($a)) { print "MSet has " . $mset->size() . " entries, expected " . sizeof($a) . "\n"; exit(1);
/** * Add a post to the index. Adds more metadata than may be strictly * required! * * @param Post $post the post being inserted */ public function index_post($post) { $doc = new XapianDocument(); // Store some useful stuff with the post $doc->set_data($post->content); $doc->add_value(self::XAPIAN_FIELD_URL, $post->permalink); $doc->add_value(self::XAPIAN_FIELD_TITLE, $post->title); $doc->add_value(self::XAPIAN_FIELD_USERID, $post->user_id); $doc->add_value(self::XAPIAN_FIELD_PUBDATE, $post->pubdate); $doc->add_value(self::XAPIAN_FIELD_CONTENTTYPE, $post->content_type); $doc->add_value(self::XAPIAN_FIELD_ID, $post->id); // Index title and body $this->_indexer->set_document($doc); $this->_indexer->index_text($post->title, 50); // add weight to titles $this->_indexer->index_text($post->content, 1); // Add terms $tags = $post->tags; foreach ($tags as $id => $tag) { $tag = (string) $tag; $this->_indexer->index_text($tag, 1, 'XTAG'); // with index for filter $this->_indexer->index_text($tag, 2); // without prefix for index } // Add uid $id = $this->get_uid($post); $doc->add_term($id); return $this->_database->replace_document($id, $doc); }
/** * Index file contents * * @param array $lines The array of the file contents, each entry corresponds to a new line (included) */ protected function _index($lines, $file_path) { if (empty($lines)) { return false; } // Open the database for update, creating a new database if necessary. $database = new XapianWritableDatabase(self::$_database_path, Xapian::DB_CREATE_OR_OPEN); $indexer = new XapianTermGenerator(); $stemmer = new XapianStem("english"); $indexer->set_stemmer($stemmer); $para = ''; //$lines = file($path); foreach ($lines as $line) { $line = rtrim($line); if ($line == "" && $para != "") { // We've reached the end of a paragraph, so index it. $doc = new XapianDocument(); $doc->set_data($para); $doc->add_value('file', $file_path); //add meta-information to the entry $indexer->set_document($doc); $indexer->index_text($para); // Add the document to the database. $database->add_document($doc); $para = ""; } else { if ($para != "") { $para .= " "; } $para .= $line; } } // Set the database handle to Null to ensure that it gets closed // down cleanly or uncommitted changes may be lost. $database = Null; }
} $db->set_metadata('Foo', 'Foo'); if ($db->get_metadata('Foo') !== 'Foo') { print "Unexpected value for metadata associated with 'Foo' (expected 'Foo'): '" . $db->get_metadata('Foo') . "'\n"; exit(1); } # Test OP_SCALE_WEIGHT and corresponding constructor $query4 = new XapianQuery($op_scale_weight, new XapianQuery('foo'), 5.0); if ($query4->get_description() != "Xapian::Query(5 * foo)") { print "Unexpected \$query4->get_description()\n"; exit(1); } # Test MultiValueSorter. $doc = new XapianDocument(); $doc->add_term("foo"); $doc->add_value(0, "ABB"); $db2->add_document($doc); $doc->add_value(0, "ABC"); $db2->add_document($doc); $doc->add_value(0, "ABC"); $db2->add_document($doc); $doc->add_value(0, "ABCD"); $db2->add_document($doc); $doc->add_value(0, "ABCÿ"); $db2->add_document($doc); $enquire = new XapianEnquire($db2); $enquire->set_query(new XapianQuery("foo")); $sorter = new XapianMultiValueSorter(); $sorter->add(0); $enquire->set_sort_by_key($sorter); $mset = $enquire->get_mset(0, 10);