技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> PHP --> 用PHP和xapian构建全文检索

用PHP和xapian构建全文检索

浏览:3372次  出处信息
#ffa500">/**
  •    * Search method
  •    *
  •    */
  •   public function search($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

    1. <?php
    2. require_once 'XapianWrapper.php';
    3. $x = new XapianWrapper();
    4. $res = $x->index(array());
    5. print_r($res);

    Search.php

    1. <?php
    2. require_once 'XapianWrapper.php';
    3. $x = new XapianWrapper();
    4. $params = array('search' => 'foo');
    5. $res = $x->search($params);
    6. print_r($res);

    delete.php

    1. <?php
    2. require_once 'XapianWrapper.php';
    3. $x = new XapianWrapper();
    4. $params = array(
    5.     'items' => array('foo'),
    6. );
    7. $res = $x->delete($params);
    8. print_r($res);

    使用示例:
    您下载刚才的源码包后,就可以导入db.sql,并在命令里运行程序;

    1. bash$ php index.php
    2. stdClass Object
    3. (
    4.     [indexed] => Array
    5.         (
    6.             [0] => Array
    7.                 (
    8.                     [name] => foo
    9.                     [guid] => foo
    10.                     [url] =>
    11.                 )
    12.             [1] => Array
    13.                 (
    14.                     [name] => bar
    15.                     [guid] => bar
    16.                     [url] =>
    17.                 )
    18.         )
    19. )
    20. bash$ php search.php
    21. stdClass Object
    22. (
    23.     [result_count] => 2
    24.     [results] => Array
    25.         (
    26.             [0] => Array
    27.                 (
    28.                     [position] => 1
    29.                     [url] =>
    30.                     [name] => foo
    31.                     [summary] => foo bar test
    32.                     [date] => 20081105
    33.                     [unique_key] => foo
    34.                     [percent] => 100
    35.                 )
    36.             [1] => Array
    37.                 (
    38.                     [position] => 2
    39.                     [url] =>
    40.                     [name] => bar
    41.                     [summary] => test foo bar
    42.                     [date] => 20091105
    43.                     [unique_key] => bar
    44.                     [percent] => 50
    45.                 )
    46.         )
    47.     [execute] => stdClass Object
    48.         (
    49.             [call] => search
    50.             [offset] => 0
    51.             [count] => 10
    52.             [start] => 1256674866.79
    53.             [end] => 1256674866.79
    54.             [time] => 0.000944852828979
    55.             [debug] => Xapian::Query(Zfoo:(pos=1))
    56.         )
    57. )
    58. bash$ php delete.php
    59. Array
    60. (
    61.     [0] => foo
    62. )
    63. bash$ php search.php
    64. stdClass Object
    65. (
    66.     [result_count] => 1
    67.     [results] => Array
    68.         (
    69.             [0] => Array
    70.                 (
    71.                     [position] => 1
    72.                     [url] =>
    73.                     [name] => bar
    74.                     [summary] => test foo bar
    75.                     [date] => 20091105
    76.                     [unique_key] => bar
    77.                     [percent] => 100
    78.                 )
    79.         )
    80.     [execute] => stdClass Object
    81.         (
    82.             [call] => search
    83.             [offset] => 0
    84.             [count] => 10
    85.             [start] => 1256674876.02
    86.             [end] => 1256674876.02
    87.             [time] => 0.000872850418091
    88.             [debug] => Xapian::Query(Zfoo:(pos=1))
    89.         )
    90. )

    接下来,扩展您自己的程序来满足您的各种需求吧,欢迎反馈.好好地享受检索的乐趣吧.

    建议继续学习:

    1. Xapian搜索体系结构    (阅读:3883)
    2. Hermes:来自腾讯的实时检索分析平台    (阅读:2812)
    3. Xapian的查询分析器    (阅读:2713)
    4. 基于Lucene/XML的站内全文检索解决方案:WebLucene    (阅读:2408)
    5. 利用Sphinx实现实时全文检索    (阅读:2345)
    6. 检测文本正文是否包含有特定词的PHP扩展    (阅读:2337)
    7. Xapian 术语表    (阅读:2190)
    8. 分布式全文检索系统SolrCloud简介    (阅读:1657)
    9. 检索结果聚类展望    (阅读:1643)
    QQ技术交流群:445447336,欢迎加入!
    扫一扫订阅我的微信号:IT技术博客大学习
    建议继续学习
    近3天十大热文
    © 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

    京ICP备15002552号-1