[Xapian-discuss] Re: Practical example/explanation using an
existing database
John Wards
jwards at whiteoctober.co.uk
Wed Jul 25 16:52:37 BST 2007
Hi Edwin,
Here is some sample PHP code to build an index. Not perfect but works
and should give you a kick start....
-----------
//Define some constants for ease of use
define("XapianClientID",1);
define("XapianInfo",2);
define("XapianName",3);
define("XapianCounty",4);
define("XapianCountyID",5);
$result = db_query("SELECT clientid,info,name,county,countyid FROM
business");
include('xapian.php');
try {
// Create a database, overwrite if exists.
$database = new XapianWritableDatabase("/home/mydirectory/xapiantest/testdatabase", Xapian::DB_CREATE_OR_OVERWRITE);
$indexer = new XapianTermGenerator();
$stemmer = new XapianStem("english");
$indexer->set_stemmer($stemmer);
while ($data = db_fetch_assoc($result)) {
$doc = new XapianDocument();
$doc->add_value(XapianClientID,$data["clientid"]);
$doc->add_value(XapianInfo,$data["info"]);
$doc->add_value(XapianName,$data["name"]);
$doc->add_value(XapianCounty,$data["county"]);
$doc->add_value(XapianCountyID,$data["countyid"]);
//Adds a boolean term
$doc->add_term("XCO".$data["countyid"]);
//Assign the document to the TermGenerator which will generate the terms used for searching
$indexer->set_document($doc);
$indexer->index_text("$data["info"]")
//Give this a higher weight
$indexer->index_text("$data["name"]",2);
. $indexer->index_text("$data["county"]");
// Add the document to the database.
$database->add_document($doc);
}
// Set the database handle to Null to ensure that it gets closed
// down cleanly or unflushed changes may be lost.
$database = Null;
} catch (Exception $e) {
print $e->getMessage() . "\n";
exit(1);
}
-----------
Here is some code to search
----------
//Define some constants for ease of use
define("XapianClientID",1);
define("XapianInfo",2);
define("XapianName",3);
define("XapianCounty",4);
define("XapianCountyID",5);
if(isset($_GET["page"])){
$page = intval($_GET["page"]);
}else{
$page =0;
}
include('xapian.php');
// Open the database for searching
try {
$database = new XapianDatabase("/home/mydirectory/xapiantest/testdatabase");
// Start an enquire session.
$enquire = new XapianEnquire($database);
$query_string = $_GET["q"];
$qp = new XapianQueryParser();
$stemmer = new XapianStem("english");
$qp->set_stemmer($stemmer);
$qp->set_database($database);
$qp->set_stemming_strategy(XapianQueryParser::STEM_ALL);
$qp->set_default_op(XapianQuery::OP_AND);
//Returns a XapianQuery Object
$query = $qp->parse_query($query_string);
if($_GET["inpcountyid"]){
$query = new XapianQuery(XapianQuery::OP_FILTER,$query,new XapianQuery('XCO'.$_GET["inpcountyid"]));
}
$enquire->set_query($query);
//Returns a XapianMSet object
$matches = $enquire->get_mset($page, 10);
print "Parsed query is: {$query->get_description()}\n<br \>";
print "{$matches->get_matches_estimated()} results found:\n <br \>";
//Returns a XapianMSetIterator object
$i = $matches->begin();
while (!$i->equals($matches->end())) {
//Returns XapianDocument object
$odoc = $i->get_document();
echo "Score: ".$i->get_percent()."%<br />";
echo "Client ID: ".$odoc->get_value(XapianClientID)."<br />";
echo "Bus Name: ".$odoc->get_value(XapianName)."<br />";
echo "Additional Info: ".$odoc->get_value(XapianInfo)."<br />";
echo "County: ".$odoc->get_value(XapianCounty)."<br />";
$i->next();
}
} catch (Exception $e) {
print $e->getMessage() . "\n";
exit(1);
}
----------
More information about the Xapian-discuss
mailing list