Xapian PHP sample code for searching by numeric range

From , 5 Years ago, written in PHP, viewed 81 times.
URL https://pastebin.vip/view/2c27a260
  1. <?php
  2. if (php_sapi_name() != "cli") {
  3.     print "This example script is written to run under the command line ('cli') version of\n";
  4.     print "the PHP interpreter, but you're using the '".php_sapi_name()."' version\n";
  5.     exit(1);
  6. }
  7. include "xapian.php";
  8. if ($argc != 2) {
  9.     print "Usage: {$argv[0]} PATH_TO_DATABASE\n";
  10.     exit(1);
  11. }
  12. try {
  13.     // Open the database for update, creating a new database if necessary.
  14.     $database = new XapianWritableDatabase($argv[1], Xapian::DB_CREATE_OR_OVERWRITE);
  15.     // add a document with a term and a timestamp value
  16.     $doc = new XapianDocument();
  17.     $doc->add_term("foo");
  18.     $doc->add_value(1, Xapian::sortable_serialise(1000000000));
  19.     $database->add_document($doc);
  20.     // add another: same term, different timestamp value
  21.     $doc = new XapianDocument();
  22.     $doc->add_term("foo");
  23.     $doc->add_value(1, Xapian::sortable_serialise(2000000000));
  24.     $database->add_document($doc);
  25.     // Set the database handle to Null to ensure that it gets closed
  26.     // down cleanly or unflushed changes may be lost.
  27.     $database = Null;
  28.     // open database for reading
  29.     $database = new XapianDatabase($argv[1]);
  30.     $enquire = new XapianEnquire($database);
  31.     // example 1 using a query processor
  32.     $qp = new XapianQueryParser();
  33.     $qp->set_database($database);
  34.     $datenumproc = new XapianNumberValueRangeProcessor(1);
  35.     $qp->add_valuerangeprocessor($datenumproc);
  36.     // without range: get both docs
  37.     $query = $qp->parse_query("foo");
  38.     $enquire->set_query($query);
  39.     print $enquire->get_mset(0, 10)->size();
  40.     print "\n";
  41.     // with range: get first doc
  42.     $query = $qp->parse_query("foo 1000000000..1500000000");
  43.     $enquire->set_query($query);
  44.     print $enquire->get_mset(0, 10)->size();
  45.     print "\n";
  46.     // example 2 - direct query construction (get first doc)
  47.     $query = new XapianQuery(XapianQuery::OP_VALUE_RANGE,
  48.                              1,
  49.                              Xapian::sortable_serialise(1000000000),
  50.                              Xapian::sortable_serialise(1500000000));
  51.     $enquire->set_query($query);
  52.     print $enquire->get_mset(0, 10)->size();
  53.     print "\n";
  54. } catch (Exception $e) {
  55.     print $e->getMessage() . "\n";
  56.     exit(1);
  57. }
  58. ?>
  59. //php/8952

Reply to "Xapian PHP sample code for searching by numeric range"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website