xref: /plugin/sphinxsearch-was/SphinxSearch.php (revision 0:c723787235e2)
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' => 10));
18
19        $this->_index = $index;
20    }
21
22    public function search($query, $start, $resultsPerPage = 10)
23    {
24        $this->_sphinx->SetLimits($start, $resultsPerPage);
25        $res = $this->_sphinx->Query($query, $this->_index);
26        $this->_result = $res;
27        if (empty($res['matches'])) {
28            return false;
29	}
30
31        $pageMapper = new PageMapper();
32
33        $pageCrcList = array_keys($res['matches']);
34        $pagesIds = $pageMapper->getByCrc($pageCrcList);
35
36        $pagesList = array();
37        foreach ($pageCrcList as $crc){
38            $pagesList[] = $pagesIds[$crc];
39            $data[$pagesIds[$crc]] = p_wiki_xhtml($pagesIds[$crc]);
40        }
41
42        $pagesExcerpt = $this->_sphinx->BuildExcerpts($data, $this->_index, $query);
43        $i = 0;
44        foreach($data as $key => $v){
45            $data[$key] = $pagesExcerpt[$i++];
46        }
47        return $data;
48    }
49
50    public function getTotalFound()
51    {
52        return !empty($this->_result['total_found'])?$this->_result['total_found'] : 0;
53    }
54}
55