-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphinx.php
More file actions
161 lines (137 loc) · 5.17 KB
/
Copy pathsphinx.php
File metadata and controls
161 lines (137 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Behavior for simple usage of Sphinx search engine
* http://www.sphinxsearch.com
*
* @copyright 2008, Vilen Tambovtsev
* @author Vilen Tambovtsev
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class SphinxBehavior extends ModelBehavior
{
/**
* Used for runtime configuration of model
*/
var $runtime = array();
var $_defaults = array('server' => 'localhost', 'port' => 3312);
var $_cached_result = null;
/**
* Spinx client object
*
* @var SphinxClient
*/
var $sphinx = null;
function setup(&$model, $config = array())
{
$settings = array_merge($this->_defaults, (array)$config);
$this->settings[$model->alias] = $settings;
App::import('Vendor', 'sphinxapi');
$this->runtime[$model->alias]['sphinx'] = new SphinxClient();
$this->runtime[$model->alias]['sphinx']->SetServer($this->settings[$model->alias]['server'],
$this->settings[$model->alias]['port']);
}
/**
* beforeFind Callback
*
* @param array $query
* @return array Modified query
* @access public
*/
function beforeFind(&$model, $query)
{
if (empty($query['sphinx']) || empty($query['search']))
return true;
if ($model->findQueryType == 'count')
{
$model->recursive = -1;
$query['limit'] = 1;
$query['page'] = 1;
}
else if (empty($query['limit']))
{
$query['limit'] = 9999999;
$query['page'] = 1;
}
foreach ($query['sphinx'] as $key => $setting)
{
switch ($key)
{
case 'filter':
foreach ($setting as $arg)
{
$arg[2] = empty($arg[2]) ? false : $arg[2];
$this->runtime[$model->alias]['sphinx']->SetFilter($arg[0], (array)$arg[1], $arg[2]);
}
break;
case 'filterRange':
case 'filterFloatRange':
$method = 'Set' . $key;
foreach ($setting as $arg)
{
$arg[3] = empty($arg[3]) ? false : $arg[3];
$this->runtime[$model->alias]['sphinx']->{$method}($arg[0], (array)$arg[1], $arg[2], $arg[3]);
}
break;
case 'matchMode':
$this->runtime[$model->alias]['sphinx']->SetMatchMode($setting);
break;
case 'sortMode':
$this->runtime[$model->alias]['sphinx']->SetSortMode(key($setting), reset($setting));
break;
case 'fieldWeights':
$this->runtime[$model->alias]['sphinx']->SetFieldWeights($setting);
break;
case 'rankingMode':
$this->runtime[$model->alias]['sphinx']->SetRankingMode($setting);
break;
default:
break;
}
}
$this->runtime[$model->alias]['sphinx']->SetLimits(($query['page'] - 1) * $query['limit'], $query['limit']);
$indexes = !empty($query['sphinx']['index']) ? implode(',' , $query['sphinx']['index']) : '*';
$result = $this->runtime[$model->alias]['sphinx']->Query($query['search'], $indexes);
if ($result === false)
{
trigger_error("Search query failed: " . $this->runtime[$model->alias]['sphinx']->GetLastError());
return false;
}
else if(isset($result['matches']))
{
if ($this->runtime[$model->alias]['sphinx']->GetLastWarning())
{
trigger_error("Search query warning: " . $this->runtime[$model->alias]['sphinx']->GetLastWarning());
}
}
unset($query['conditions']);
unset($query['order']);
unset($query['offset']);
$query['page'] = 1;
if ($model->findQueryType == 'count')
{
$result['total'] = !empty($result['total']) ? $result['total'] : 0;
$query['fields'] = 'ABS(' . $result['total'] . ') AS count';
}
else
{
$this->_cached_result = $result;
if (isset($result['matches']))
$ids = array_keys($result['matches']);
else
$ids = array(0);
$query['conditions'] = array($model->alias . '.'.$model->primaryKey => $ids);
$query['order'] = 'FIND_IN_SET('.$model->alias.'.'.$model->primaryKey.', \'' . implode(',', $ids) . '\')';
}
return $query;
}
public function afterFind(&$model, $results, $primary) {
if(!is_null($this->_cached_result)) {
foreach($results as &$result) {
$result[$model->name]['_weight'] = $this->_cached_result['matches'][$result[$model->name]['id']]['weight'];
$result[$model->name]['_total_found'] = $this->_cached_result['total_found'];
}
$this->_cached_result = null;
}
return $results;
}
}