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 private $_query = ''; 13 private $_snippetSize = 256; 14 private $_aroundKeyword = 5; 15 16 public function __construct($host, $port, $index) 17 { 18 $this->_sphinx = new SphinxClient(); 19 $this->_sphinx->SetServer($host, $port); 20 $this->_sphinx->SetMatchMode(SPH_MATCH_EXTENDED2); 21 $this->_sphinx->SetFieldWeights(array('categories' => 5, 'title' => 20, 'body' => 3)); 22 $this->_sphinx->SetFilter('level', array(1)); 23 24 $this->_index = $index; 25 } 26 27 public function search($keywords, $categories, $start, $resultsPerPage = 10) 28 { 29 $this->_sphinx->SetLimits($start, $resultsPerPage); 30 $query = ''; 31 if (!empty($keywords) && empty($categories)){ 32 $query = "@(body,title,categories) {$keywords}"; 33 } else { 34 $query = "@(body,title) {$keywords} @categories ".$categories; 35 } 36 $this->_query = $query; 37 $res = $this->_sphinx->Query($query, $this->_index); 38 $this->_result = $res; 39 if (empty($res['matches'])) { 40 return false; 41 } 42 43 $pageMapper = new PageMapper(); 44 45 $pageCrcList = array_keys($res['matches']); 46 $pagesIds = $pageMapper->getByCrc($pageCrcList); 47 48 $pagesList = array(); 49 $body = array(); 50 $title = array(); 51 $category = array(); 52 foreach ($pageCrcList as $crc){ 53 if (!empty($pagesIds[$crc]['hid'])){ 54 $bodyHtml = p_render('xhtml',p_get_instructions(getSection($pagesIds[$crc]['page'], $pagesIds[$crc]['title'])),$info); 55 } else { 56 $bodyHtml = p_wiki_xhtml($pagesIds[$crc]['page']); 57 } 58 $body[$crc] = strip_tags($bodyHtml); 59 $title[$crc] = strip_tags($pagesIds[$crc]['title']); 60 $category[$crc] = $pagesIds[$crc]['page']; 61 } 62 63 $starQuery = $this->starQuery($keywords); 64 $bodyExcerpt = $this->getExcerpt($body, $starQuery); 65 $titleExcerpt = $this->getExcerpt($title, $starQuery); 66 $i = 0; 67 $results = array(); 68 foreach($body as $crc => $notused){ 69 $results[$crc] = array( 70 'page' => $pagesIds[$crc]['page'], 71 'bodyExcerpt' => $bodyExcerpt[$i], 72 'titleExcerpt' => $titleExcerpt[$i], 73 'hid' => $pagesIds[$crc]['hid'], 74 'title' => $pagesIds[$crc]['title'] 75 ); 76 $i++; 77 } 78 return $results; 79 } 80 81 public function getTotalFound() 82 { 83 return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0; 84 } 85 86 public function getExcerpt($data, $query) 87 { 88 return $this->_sphinx->BuildExcerpts($data, $this->_index, $query, 89 array( 90 'limit' => $this->_snippetSize, 91 'around' => $this->_aroundKeyword 92 ) 93 ); 94 } 95 96 public function starQuery($query) 97 { 98 $words = explode(" ", $query); 99 $starQuery = ''; 100 foreach($words as $word){ 101 $starQuery .= "*".$word."* "; 102 } 103 return $starQuery; 104 } 105 106 public function getQuery() 107 { 108 return $this->_query; 109 } 110 111 public function setSnippetSize($symbols = 256) 112 { 113 $this->_snippetSize = $symbols; 114 } 115 116 public function setArroundWordsCount($words = 5) 117 { 118 $this->_aroundKeyword = $words; 119 } 120} 121