Sphinx.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: chendeben
  5. * Date: 2017/12/6
  6. * Time: 11:55
  7. */
  8. namespace PhpLife\Library;
  9. use PhpLife\Config\Sphinx\Core;
  10. require_once 'sphinxapi.php';
  11. class Sphinx
  12. {
  13. private $server;
  14. public static $instance;
  15. public function __construct($is_pre = FALSE)
  16. {
  17. $config = Core::getData();
  18. $host = $config['host'];
  19. $port = $config['port'];
  20. $this->server=new \SphinxClient();
  21. $this->server->SetServer($host,$port);
  22. }
  23. public static function instance($is_pre = false)
  24. {
  25. $key = $is_pre ? 'pre' : 'formal';
  26. if (!isset(self::$instance[$key])) {
  27. $instance = new self($is_pre);
  28. self::$instance[$key] = $instance;
  29. } else {
  30. $instance = self::$instance[$key];
  31. }
  32. return $instance;
  33. }
  34. public function query($keyword,$offset,$count,$max=1000){
  35. $this->server->SetLimits ( $offset, $count, $max, 0 );
  36. $res=$this->server->Query($keyword);
  37. return$res;
  38. }
  39. }