• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • 位置: php 中文手册 -> PECL扩展库

    Sphinx(全文检索引擎)

    sphinx支持中文全文检索,并且支持分词高亮。sphinx高速的建立索引(峰值达10M/s),高性能的搜索(2-4G的文件数据上,平均每次检索0.1秒左右),可处理海量数据 (字符串,布尔..)。Sphinx的特点:

    • 快速创建索引:3分钟左右即可创建近100万条记录的索引,并且采用了增量索引的方式,重建索引非常迅速。
    • 闪电般的检索速度:尽管是1千万条的大数据量,查询数据的速度也在毫秒级以上,2-4G的文本量中平均查询速度不到0.1秒。
    • 为很多脚本语言设计了检索API,如PHP,Python,Perl,Ruby等,因此你可以在大部分编程应用中很方便地调用Sphinx的相关接口。
    • 为MySQL设计了一个存储引擎插件,因此如果你在MySQL上使用Sphinx,那简直就方便到家了。
    • 支持分布式搜索,可以横向扩展系统性能。

    sphinx安装

    安装此 PECL 扩展相关的信息可在手册中标题为 PECL 扩展的安装章节中找到。但需要PHP 5.2.2+版本。

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

    范例

    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)
        }
      }
    }
    
    it is deprecated, use instead SphinxQL-Query-Builder
    SphinxQL-Query-Builder is an ORM for Sphinx.
    ORM generally is for dumb developers that have no ability to write SQL complex queries.
    More, ORMs are covering just 10...15% of the SQL syntax.
    So, my advice, don't go with ORM in any case ...
    Use the SQL syntax always:
    http://sphinxsearch.com/docs/current.html#sphinxql-reference
    PS:
    I have seen many ORMs like Doctrine or others, but they are a joke comparing with the SQL ;)

    SphinxClient 类

    SphinxClient
    {
    	/* 方法 */
    	public addQuery ( string $query [, string $index = "*" [, string $comment = "" ]] ) : int
    	public buildExcerpts ( array $docs , string $index , string $words [, array $opts ] ) : array
    	public buildKeywords ( string $query , string $index , bool $hits ) : array
    	public close ( void ) : bool
    	public __construct ( void )
    	public escapeString ( string $string ) : string
    	public getLastError ( void ) : string
    	public getLastWarning ( void ) : string
    	public open ( void ) : bool
    	public query ( string $query [, string $index = "*" [, string $comment = "" ]] ) : array
    	public resetFilters ( void ) : void
    	public resetGroupBy ( void ) : void
    	public runQueries ( void ) : array
    	public setArrayResult ( bool $array_result = false ) : bool
    	public setConnectTimeout ( float $timeout ) : bool
    	public setFieldWeights ( array $weights ) : bool
    	public setFilter ( string $attribute , array $values [, bool $exclude = false ] ) : bool
    	public setFilterFloatRange ( string $attribute , float $min , float $max [, bool $exclude = FALSE ] ) : bool
    	public setFilterRange ( string $attribute , int $min , int $max [, bool $exclude = FALSE ] ) : bool
    	public setGeoAnchor ( string $attrlat , string $attrlong , float $latitude , float $longitude ) : bool
    	public setGroupBy ( string $attribute , int $func [, string $groupsort = "@group desc" ] ) : bool
    	public setGroupDistinct ( string $attribute ) : bool
    	public setIDRange ( int $min , int $max ) : bool
    	public setIndexWeights ( array $weights ) : bool
    	public setLimits ( int $offset , int $limit [, int $max_matches = 0 [, int $cutoff = 0 ]] ) : bool
    	public setMatchMode ( int $mode ) : bool
    	public setMaxQueryTime ( int $qtime ) : bool
    	public setOverride ( string $attribute , int $type , array $values ) : bool
    	public setRankingMode ( int $ranker ) : bool
    	public setRetries ( int $count [, int $delay = 0 ] ) : bool
    	public setSelect ( string $clause ) : bool
    	public setServer ( string $server , int $port ) : bool
    	public setSortMode ( int $mode [, string $sortby ] ) : bool
    	public status ( void ) : array
    	public updateAttributes ( string $index , array $attributes , array $values [, bool $mva = FALSE ] ) : int
    }