<?php
/**
 * Created by PhpStorm.
 * User: chendeben
 * Date: 2017/12/6
 * Time: 11:55
 */

namespace PhpLife\Library;

use PhpLife\Config\Sphinx\Core;

require_once 'sphinxapi.php';

class Sphinx
{
    private $server;

    public static $instance;

    public function __construct($is_pre = FALSE)
    {
        $config = Core::getData();
        $host = $config['host'];
        $port = $config['port'];
        $this->server=new \SphinxClient();
        $this->server->SetServer($host,$port);
    }

    public static function instance($is_pre = false)
    {
        $key = $is_pre ? 'pre' : 'formal';
        if (!isset(self::$instance[$key])) {
            $instance = new self($is_pre);
            self::$instance[$key] = $instance;
        } else {
            $instance = self::$instance[$key];
        }
        return $instance;
    }

    public function query($keyword,$offset,$count,$max=1000){
        $this->server->SetLimits ( $offset, $count, $max, 0 );
        $res=$this->server->Query($keyword);
        return$res;
    }
}