function delete_docs($dbpath, $identifiers) { // Open the database we're going to be deleting from. $db = new XapianWritableDatabase($dbpath, Xapian::DB_OPEN); foreach ($identifiers as $identifier) { $idterm = 'Q' . $identifier; $db->delete_document($idterm); } }
function index($datapath, $dbpath) { // Create or open the database we're going to be writing to. $db = new XapianWritableDatabase($dbpath, Xapian::DB_CREATE_OR_OPEN); // Set up a TermGenerator that we'll use in indexing $termgenerator = new XapianTermGenerator(); $termgenerator->set_stemmer(new XapianStem('en')); // open the file $fH = open_file($datapath); // Read the header row in $headers = get_csv_headers($fH); while (($row = parse_csv_row($fH, $headers)) !== false) { // mapping from field name to value using first row headers // We're just going to use id_NUMBER, TITLE and DESCRIPTION $description = $row['DESCRIPTION']; $title = $row['TITLE']; $identifier = $row['id_NUMBER']; $collection = $row['COLLECTION']; $maker = $row['MAKER']; // we make a document and tell the term generator to use this $doc = new XapianDocument(); $termgenerator->set_document($doc); // index each field with a suitable prefix $termgenerator->index_text($title, 1, 'S'); $termgenerator->index_text($description, 1, 'XD'); // index fields without prefixes for general search $termgenerator->index_text($title); $termgenerator->increase_termpos(); $termgenerator->index_text($description); ### Start of new indexing code. // index the MATERIALS field, splitting on semicolons $materials = explode(";", $row['MATERIALS']); foreach ($materials as $material) { $material = strtolower(trim($material)); if ($material != '') { $doc->add_boolean_term('XM' . $material); } } ### End of new indexing code. // store all the fields for display purposes $doc->set_data(json_encode($row)); // we use the identifier to ensure each object ends up // in the database only once no matter how many times // we run the indexer $idterm = "Q" . $identifier; $doc->add_term($idterm); $db->replace_document($idterm, $doc); } }
function index($datapath, $dbpath) { // Create or open the database we're going to be writing to. $db = new XapianWritableDatabase($dbpath, Xapian::DB_CREATE_OR_OPEN); // Set up a TermGenerator that we'll use in indexing. $termgenerator = new XapianTermGenerator(); $termgenerator->set_stemmer(new XapianStem('english')); // Open the file. $fH = open_file($datapath); // Read the header row in. $headers = get_csv_headers($fH); while (($row = parse_csv_row($fH, $headers)) !== false) { // '$row' maps field name to value. The field names come from the // first row of the CSV file. // // We're just going to use DESCRIPTION, TITLE and id_NUMBER. $description = $row['DESCRIPTION']; $title = $row['TITLE']; $identifier = $row['id_NUMBER']; // We make a document and tell the term generator to use this. $doc = new XapianDocument(); $termgenerator->set_document($doc); // Index each field with a suitable prefix. $termgenerator->index_text($title, 1, 'S'); $termgenerator->index_text($description, 1, 'XD'); // Index fields without prefixes for general search. $termgenerator->index_text($title); $termgenerator->increase_termpos(); $termgenerator->index_text($description); // Store all the fields for display purposes. $doc->set_data(json_encode($row)); // We use the identifier to ensure each object ends up in the // database only once no matter how many times we run the // indexer. $idterm = "Q" . $identifier; $doc->add_boolean_term($idterm); $db->replace_document($idterm, $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; }
if (php_sapi_name() != "cli") { print "This example script is written to run under the command line ('cli') version of\n"; print "the PHP interpreter, but you're using the '".php_sapi_name()."' version\n"; exit(1); } include "php5/xapian.php"; if ($argc != 2) { print "Usage: {$argv[0]} PATH_TO_DATABASE\n"; exit(1); } try { // Open the database for update, creating a new database if necessary. $database = new XapianWritableDatabase($argv[1], Xapian::DB_CREATE_OR_OPEN); $indexer = new XapianTermGenerator(); $stemmer = new XapianStem("english"); $indexer->set_stemmer($stemmer); $para = ''; $lines = file("php://stdin"); 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); $indexer->set_document($doc);
} } # Check that open_stub() writable form is wrapped as expected. try { $db = Xapian::auto_open_stub("nosuchdir/nosuchdb", Xapian::DB_OPEN); print "Opened non-existent stub database\n"; exit(1); } catch (Exception $e) { if ($e->getMessage() !== "DatabaseOpeningError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") { print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n"; exit(1); } } # Check that DB_BACKEND_STUB works as expected. try { $db = new XapianWritableDatabase("nosuchdir/nosuchdb", Xapian::DB_OPEN | Xapian::DB_BACKEND_STUB); print "Opened non-existent stub database\n"; exit(1); } catch (Exception $e) { if ($e->getMessage() !== "DatabaseOpeningError: Couldn't open stub database file: nosuchdir/nosuchdb (No such file or directory)") { print "DatabaseOpeningError Exception string not as expected, got: '{$e->getMessage()}'\n"; exit(1); } } # Regression test for bug#193, fixed in 1.0.3. $vrp = new XapianNumberValueRangeProcessor(0, '$', true); $a = '$10'; $b = '20'; $vrp->apply($a, $b); if (Xapian::sortable_unserialise($a) != 10) { print Xapian::sortable_unserialise($a) . " != 10\n";
foreach ($doc->termlist_begin() as $k => $term) { $s .= $term . ':' . $k->get_wdf() . ' '; } if ($s !== 'ask:1 i:1 in:2 nothing:1 return:2 tea:1 time:2 ') { print "PHP Iterator wrapping of TermIterator keys doesn't work ({$s})\n"; 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 = new XapianWritableDatabase('', Xapian::DB_BACKEND_INMEMORY); $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) {