#ffa500">/**
* Search method
*
*/
public functionsearch($params){
$this->xapian_init_readonly();
$start = microtime(true);
// queries array to later construct full query
$arr_queries = array();
// from date
if(!empty($params['date_from'])) {
$arr_queries[] = new XapianQuery(XapianQuery::OP_VALUE_GE, 6, date('Ymd', strtotime($params['date_from'])));
}
// to date
if(!empty($params['date_to'])) {
$arr_queries[] = new XapianQuery(XapianQuery::OP_VALUE_LE, 6, date('Ymd', strtotime($params['date_to'])));
}
// unique key
if(!empty($params['unique_key'])) {
$arr_queries[] = new XapianQuery(self::XAPIAN_PREFIX_UID . $params['unique_key']);
}
// normal search query parsed
if(!empty($params['search'])) {
$qp = new XapianQueryParser();
$qp->set_stemmer($this->xapian_stemmer);
$qp->set_database($this->xapian_read_db);
$qp->set_stemming_strategy(XapianQueryParser::STEM_SOME);
$arr_queries[] = $qp->parse_query($params['search']);
}
// Find the results for the query.
// construct final query
$query = array_pop($arr_queries);
foreach($arr_queries as$sq){
$query = new XapianQuery(XapianQuery::OP_AND, $query, $sq);
}
$this->xapian_enquire->set_query($query);
// set the count to the specified params
$offset = (isset($params['offset'])) ? intval($params['offset']) : 0;
$count = (isset($params['count'])) ? intval($params['count']) : self::DEFAULT_COUNT;
$matches = $this->xapian_enquire->get_mset($offset, $count);
$response = new stdClass();
$response->result_count = $matches->get_matches_estimated();
$results = array();
$i = $matches->begin();
while (!$i->equals($matches->end())){
$m = array();
$n = $i->get_rank() + 1;
$doc = $i->get_document();
$m['position'] = $n;
$m['url'] = $doc->get_value(self::XAPIAN_FIELD_URL);
$m['name'] = $doc->get_value(self::XAPIAN_FIELD_NAME);
$m['summary'] = $doc->get_value(self::XAPIAN_FIELD_SUMMARY);
$m['date'] = $doc->get_value(self::XAPIAN_FIELD_DATE);
$m['unique_key'] = $doc->get_value(self::XAPIAN_FIELD_UID);
$m['percent'] = $i->get_percent();
$results[count($results)] = $m;
$i->next();
}
$response->results = $results;
$end = microtime(true);
// runtime info
$response->execute = new stdClass();
$response->execute->call = 'search';
$response->execute->offset = $offset;
$response->execute->count = $count;
$response->execute->start = $start;
$response->execute->end = $end;
$response->execute->time = $end - $start;
// debug stuff
$response->execute->debug = $query->get_description();
return $response;
}
}
index.php
- <?php
-
- require_once 'XapianWrapper.php';
-
- $x = new XapianWrapper();
- $res = $x->index(array());
- print_r($res);
Search.php
- <?php
-
- require_once 'XapianWrapper.php';
-
- $x = new XapianWrapper();
- $params = array('search' => 'foo');
- $res = $x->search($params);
- print_r($res);
delete.php
- <?php
-
- require_once 'XapianWrapper.php';
- $x = new XapianWrapper();
- $params = array(
- 'items' => array('foo'),
- );
- $res = $x->delete($params);
- print_r($res);
使用示例:
您下载刚才的源码包后,就可以导入db.sql,并在命令里运行程序;
- bash$ php index.php
- stdClass Object
- (
- [indexed] => Array
- (
- [0] => Array
- (
- [name] => foo
- [guid] => foo
- [url] =>
- )
-
- [1] => Array
- (
- [name] => bar
- [guid] => bar
- [url] =>
- )
-
- )
-
- )
- bash$ php search.php
- stdClass Object
- (
- [result_count] => 2
- [results] => Array
- (
- [0] => Array
- (
- [position] => 1
- [url] =>
- [name] => foo
- [summary] => foo bar test
- [date] => 20081105
- [unique_key] => foo
- [percent] => 100
- )
-
- [1] => Array
- (
- [position] => 2
- [url] =>
- [name] => bar
- [summary] => test foo bar
- [date] => 20091105
- [unique_key] => bar
- [percent] => 50
- )
-
- )
-
- [execute] => stdClass Object
- (
- [call] => search
- [offset] => 0
- [count] => 10
- [start] => 1256674866.79
- [end] => 1256674866.79
- [time] => 0.000944852828979
- [debug] => Xapian::Query(Zfoo:(pos=1))
- )
-
- )
- bash$ php delete.php
- Array
- (
- [0] => foo
- )
- bash$ php search.php
- stdClass Object
- (
- [result_count] => 1
- [results] => Array
- (
- [0] => Array
- (
- [position] => 1
- [url] =>
- [name] => bar
- [summary] => test foo bar
- [date] => 20091105
- [unique_key] => bar
- [percent] => 100
- )
-
- )
-
- [execute] => stdClass Object
- (
- [call] => search
- [offset] => 0
- [count] => 10
- [start] => 1256674876.02
- [end] => 1256674876.02
- [time] => 0.000872850418091
- [debug] => Xapian::Query(Zfoo:(pos=1))
- )
-
- )
接下来,扩展您自己的程序来满足您的各种需求吧,欢迎反馈.好好地享受检索的乐趣吧.