1<?php 2/* 3 * To change this template, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6 7class SphinxSearch 8{ 9 private $_sphinx = null; 10 private $_result = array(); 11 private $_index = null; 12 public function __construct($host, $port, $index) 13 { 14 $this->_sphinx = new SphinxClient(); 15 $this->_sphinx->SetServer($host, $port); 16 $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 17 $this->_sphinx->SetFieldWeights(array('categories' => 5, 'title' => 20, 'body' => 3)); 18 $this->_sphinx->SetFilter('level', array(1)); 19 20 $this->_index = $index; 21 } 22 23 public function search($query, $start, $resultsPerPage = 10) 24 { 25 $this->_sphinx->SetLimits($start, $resultsPerPage); 26 $res = $this->_sphinx->Query($query, $this->_index); 27 $this->_result = $res; 28 if (empty($res['matches'])) { 29 return false; 30 } 31 32 $pageMapper = new PageMapper(); 33 34 $pageCrcList = array_keys($res['matches']); 35 $pagesIds = $pageMapper->getByCrc($pageCrcList); 36 37 $pagesList = array(); 38 foreach ($pageCrcList as $crc){ 39 $data[$crc] = p_wiki_xhtml($pagesIds[$crc]['page']); 40 } 41 42 $pagesExcerpt = $this->_sphinx->BuildExcerpts($data, $this->_index, $query); 43 $i = 0; 44 $results = array(); 45 foreach($data as $crc => $notused){ 46 $results[$crc] = array( 'page' => $pagesIds[$crc]['page'], 'excerpt' => $pagesExcerpt[$i++], 'hid' => $pagesIds[$crc]['hid'], 'title' => $pagesIds[$crc]['title']); 47 } 48 return $results; 49 } 50 51 public function getTotalFound() 52 { 53 return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 54 } 55} 56