Exemple #1
0
 /**
  * Init by laravel Config
  * @param $config see demo.ini file
  */
 public function __construct($config)
 {
     self::setConfig($config);
     $scheme = new \XSFieldScheme();
     foreach ($this->_config as $key => $value) {
         if (is_array($value)) {
             $scheme->addField($key, $value);
         }
     }
     $scheme->checkValid(true);
     $this->_scheme = $this->_bindScheme = $scheme;
     $this->setScheme($scheme);
 }
 public function testLogger()
 {
     $log = XSFieldScheme::logger();
     $this->assertInstanceOf('XSFieldScheme', $log);
     $this->assertEquals('id', strval($log->getFieldId()));
     $this->assertFalse($log->getFieldTitle());
     $this->assertNotEquals(false, $log->getField('pinyin', false));
     $this->assertNotEquals(false, $log->getField('partial', false));
     $this->assertTrue($log->getField('total')->isNumeric());
     $this->assertTrue($log->getField('lastnum')->isNumeric());
     $this->assertTrue($log->getField('currnum')->isNumeric());
     $this->assertFalse($log->getField('currtag')->isNumeric());
     $this->assertTrue($log->getField('body')->isSpeical());
     $this->assertEquals(XSFieldScheme::MIXED_VNO, $log->getFieldBody()->vno);
 }
Exemple #3
0
    -h|--help           显示帮助信息

EOF;
    exit(0);
}
// create xs project
$ini = XSUtil::toProjectIni($project);
if (!file_exists($ini)) {
    echo "错误:无效的项目名称 ({$project}),不存在相应的配置文件。\n";
    exit(-1);
}
try {
    $db = XSSearch::LOG_DB;
    $log_ready = false;
    $xs = new XS($ini);
    $xs->setScheme(XSFieldScheme::logger());
    $search = $xs->search;
    try {
        // NOTE: use setQuery to call preQueryString for preparing fieldset
        $search->setDb($db)->setQuery('dummy');
        $search->setTimeout(0);
        // sometimes user may import lots of terms
        $log_ready = true;
    } catch (Exception $e) {
    }
    // hot, query ==> read-only
    if ($hot !== null) {
        $limit = $limit === null ? 10 : intval($limit);
        $type = $hot === 'cur' ? 'currnum' : ($hot === 'last' ? 'lastnum' : 'total');
        $result = $search->getHotQuery($limit, $type);
        if (count($result) === 0) {
Exemple #4
0
 private function loadIniFile($file)
 {
     $cache = false;
     $cache_write = '';
     if (strlen($file) < 255 && file_exists($file)) {
         $cache_key = md5(__CLASS__ . '::ini::' . realpath($file));
         if (function_exists('apc_fetch')) {
             $cache = apc_fetch($cache_key);
             $cache_write = 'apc_store';
         } elseif (function_exists('xcache_get') && php_sapi_name() !== 'cli') {
             $cache = xcache_get($cache_key);
             $cache_write = 'xcache_set';
         } elseif (function_exists('eaccelerator_get')) {
             $cache = eaccelerator_get($cache_key);
             $cache_write = 'eaccelerator_put';
         }
         if ($cache && isset($cache['mtime']) && isset($cache['scheme']) && filemtime($file) <= $cache['mtime']) {
             $this->_scheme = $this->_bindScheme = unserialize($cache['scheme']);
             $this->_config = $cache['config'];
             return;
         }
         $data = file_get_contents($file);
     } else {
         $data = $file;
         $file = substr(md5($file), 8, 8) . '.ini';
     }
     $this->_config = $this->parseIniData($data);
     if ($this->_config === false) {
         throw new XSException('Failed to parse project config file/string: \'' . substr($file, 0, 10) . '...\'');
     }
     $scheme = new XSFieldScheme();
     foreach ($this->_config as $key => $value) {
         if (is_array($value)) {
             $scheme->addField($key, $value);
         }
     }
     $scheme->checkValid(true);
     if (!isset($this->_config['project.name'])) {
         $this->_config['project.name'] = basename($file, '.ini');
     }
     $this->_scheme = $this->_bindScheme = $scheme;
     if ($cache_write != '') {
         $cache['mtime'] = filemtime($file);
         $cache['scheme'] = serialize($this->_scheme);
         $cache['config'] = $this->_config;
         call_user_func($cache_write, $cache_key, $cache);
     }
 }
 /**
  * 获取搜索日志的字段方案
  * @return XSFieldScheme 搜索日志字段方案
  */
 public static function logger()
 {
     if (self::$_logger === null) {
         $scheme = new self();
         $scheme->addField('id', array('type' => 'id'));
         $scheme->addField('pinyin');
         $scheme->addField('partial');
         $scheme->addField('total', array('type' => 'numeric', 'index' => 'self'));
         $scheme->addField('lastnum', array('type' => 'numeric', 'index' => 'self'));
         $scheme->addField('currnum', array('type' => 'numeric', 'index' => 'self'));
         $scheme->addField('currtag', array('type' => 'string'));
         $scheme->addField('body', array('type' => 'body'));
         self::$_logger = $scheme;
     }
     return self::$_logger;
 }
Exemple #6
0
 /**
  * 获取相关搜索词列表
  * @param string $query 搜索语句, 若传入 null 使用默认语句
  * @param int $limit 需要返回的相关搜索数量上限, 默认为 6, 最大值为 20
  * @return array 返回搜索词组成的数组
  */
 public function getRelatedQuery($query = null, $limit = 6)
 {
     $ret = array();
     $limit = max(1, min(20, intval($limit)));
     // Simple to disable query with field filter
     if ($query === null) {
         $query = $this->cleanFieldQuery($this->_query);
     }
     if (empty($query) || strpos($query, ':') !== false) {
         return $ret;
     }
     // Search the log database
     $op = $this->_defaultOp;
     $this->xs->setScheme(XSFieldScheme::logger());
     try {
         $result = $this->setDb(self::LOG_DB)->setFuzzy()->setLimit($limit + 1)->search($query);
         foreach ($result as $doc) {
             $doc->setCharset($this->_charset);
             $body = $doc->body;
             if (!strcasecmp($body, $query)) {
                 continue;
             }
             $ret[] = $body;
             if (count($ret) == $limit) {
                 break;
             }
         }
     } catch (XSException $e) {
         if ($e->getCode() != CMD_ERR_XAPIAN) {
             throw $e;
         }
     }
     $this->restoreDb();
     $this->xs->restoreScheme();
     $this->_defaultOp = $op;
     return $ret;
 }
 public function testGetCustomTokenizer()
 {
     $this->assertInstanceOf('XSTokenizerSplit', $this->object->getField('date')->getCustomTokenizer());
     $this->assertInstanceOf('XSTokenizerFull', $this->object->getField('pid')->getCustomTokenizer());
 }
Exemple #8
0
 public function testAddSearchLog()
 {
     $search = self::$xs->search;
     $search->addSearchLog('php 教程');
     $search->addSearchLog('php 教学');
     $search->addSearchLog('php 教导', 999);
     $search->addSearchLog('php 教程');
     self::$xs->index->reopen(true)->flushLogging();
     sleep(2);
     self::$xs->setScheme(XSFieldScheme::logger());
     $search->reopen(true);
     $docs = $search->setDb(XSSearch::LOG_DB)->search('php');
     $search->setDb(null);
     self::$xs->restoreScheme();
     $this->assertEquals($docs[0]->total, 999);
     $this->assertEquals($docs[1]->total, 2);
     $this->assertEquals($docs[2]->total, 1);
 }