您的位置:首页 > 编程语言 > PHP开发

PHP扩展之针对搜索引擎的扩展(二)—— Sphinx简介、安装及使用

2014-11-07 00:00 1851 查看
一、简介及安装

该扩展提供了针对Sphinx搜索客户端开发库的绑定. Sphinx是一个独立的搜索引擎系统,其目的是为其他相关程序和应用提供快速的、规模可扩展的全文搜索功能. Sphinx有着良好的设计,可以很方便的与SQL数据库结合,并使用脚本语言调用. Sphinx 以及 其客户端库 可以从 官方站点获取,中文用户也可以从Coreseek获取支持中文的版本和服务。

安装需求:需要PHP5.2.2及以上PHP版本。

安装步骤请参考:Linux下编译安装Sphinx、中文分词coreseek及PHP的sphinx扩展

安装过程中,如果 ./configure 不能正确找到 libsphinxclient 文件 (例如, 他被安装在用户自己定义的目录中),使用 ./configure --with-sphinx=$PREFIX来制定libsphinxclient的具体位置($PREFIX是libsphinxclient的安装位置)。

编译安装:

http://pecl.php.net/package/sphinx下载所需的sphinx扩展源码安装包,解压到指定目录,进入该目录。

./configure --with-php-config=/usr/local/php/bin/php-config --with-sphinx=/usr/local/libsphinxclient
make
sudo make install

二、使用实例

Example #1 基本使用范例

<?php

$s = new SphinxClient;
$s->setServer("localhost", 6712);
$s->setMatchMode(SPH_MATCH_ANY);
$s->setMaxQueryTime(3);

$result = $s->query("test");

var_dump($result);

?>

以上例程的输出类似于:

array(10) {
  ["error"]=> string(0) ""
  ["warning"]=> string(0) ""
  ["status"]=> int(0)
  ["fields"]=>
      array(3) {
        [0]=> string(7) "subject"
        [1]=> string(4) "body"
        [2]=> string(6) "author"
      }
  ["attrs"]=>
      array(0) {}
  ["matches"]=>
      array(1) {
        [3]=>
            array(2) {
              ["weight"]=> int(1)
              ["attrs"]=> array(0) {}
            }
      }
  ["total"]=> int(1)
  ["total_found"]=> int(1)
  ["time"]=> float(0)
  ["words"]=> 
      array(1) {
          ["to"]=>
              array(2) {
                  ["docs"]=>int(1)
                  ["hits"]=>int(1)
              }
      }
}

三、SphinxClient 类

SphinxClient 为 Sphinx 提供了面向对象的接口
SphinxClient::addQuery — Add query to multi-query batch
SphinxClient::buildExcerpts — Build text snippets
SphinxClient::buildKeywords — Extract keywords from query
SphinxClient::close — 关闭先前打开的持久连接
SphinxClient::__construct — Create a new SphinxClient object
SphinxClient::escapeString — Escape special characters
SphinxClient::getLastError — Get the last error message
SphinxClient::getLastWarning — Get the last warning
SphinxClient::open — 建立到搜索服务端的持久连接
SphinxClient::query — 执行搜索查询
SphinxClient::resetFilters — Clear all filters
SphinxClient::resetGroupBy — Clear all group-by settings
SphinxClient::runQueries — Run a batch of search queries
SphinxClient::setArrayResult — 控制搜索结果集的返回格式
SphinxClient::setConnectTimeout — Set connection timeout
SphinxClient::setFieldWeights — Set field weights
SphinxClient::setFilter — 增加整数值过滤器
SphinxClient::setFilterFloatRange — Add new float range filter
SphinxClient::setFilterRange — Add new integer range filter
SphinxClient::setGeoAnchor — Set anchor point for a geosphere distance calculations
SphinxClient::setGroupBy — Set grouping attribute
SphinxClient::setGroupDistinct — Set attribute name for per-group distinct values count calculations
SphinxClient::setIDRange — Set a range of accepted document IDs
SphinxClient::setIndexWeights — Set per-index weights
SphinxClient::setLimits — 设置返回结果集偏移量和数目
SphinxClient::setMatchMode — 设置全文查询的匹配模式
SphinxClient::setMaxQueryTime — Set maximum query time
SphinxClient::setOverride — Sets temporary per-document attribute value overrides
SphinxClient::setRankingMode — Set ranking mode
SphinxClient::setRetries — Set retry count and delay
SphinxClient::setSelect — Set select clause
SphinxClient::setServer — 设置searchd的主机名和TCP端口
SphinxClient::setSortMode — Set matches sorting mode
SphinxClient::status — Queries searchd status
SphinxClient::updateAttributes — Update document attributes
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP扩展