1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Lucene; |
4
|
|
|
|
5
|
|
|
use ZendSearch\Lucene\Index\Term; |
6
|
|
|
use ZendSearch\Lucene\Search\Query\Boolean; |
7
|
|
|
use ZendSearch\Lucene\Search\Query\Fuzzy; |
8
|
|
|
use ZendSearch\Lucene\Search\Query\MultiTerm; |
9
|
|
|
use ZendSearch\Lucene\Search\Query\Phrase; |
10
|
|
|
use ZendSearch\Lucene\Search\Query\Wildcard; |
11
|
|
|
use ZendSearch\Lucene\Search\Query\Term as QueryTerm; |
12
|
|
|
use ZendSearch\Lucene\Search\QueryHit; |
13
|
|
|
use ZendSearch\Lucene\Search\QueryParser; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Search |
17
|
|
|
* @package BestServedCold\LaravelZendSearch\Lucene |
18
|
|
|
*/ |
19
|
|
|
class Search |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Index $index |
23
|
|
|
*/ |
24
|
|
|
protected $index; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Query $query |
28
|
|
|
*/ |
29
|
|
|
protected $query; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string|boolean $path |
33
|
|
|
*/ |
34
|
|
|
private $path; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int $limit |
38
|
|
|
*/ |
39
|
|
|
private $limit = 25; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Boolean |
43
|
|
|
*/ |
44
|
|
|
private static $boolean; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Search constructor. |
48
|
|
|
* |
49
|
|
|
* @param Index $index |
50
|
|
|
* @param Query $query |
51
|
|
|
*/ |
52
|
31 |
|
public function __construct(Index $index, Query $query) |
53
|
|
|
{ |
54
|
31 |
|
$this->index = $index; |
55
|
31 |
|
$this->query = $query; |
56
|
31 |
|
QueryParser::setDefaultEncoding('UTF-8'); |
57
|
31 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $name |
61
|
|
|
* @param $arguments |
62
|
|
|
* @return $this |
63
|
|
|
* @throws \BadMethodCallException |
64
|
|
|
*/ |
65
|
2 |
|
public function __call($name, $arguments) |
66
|
|
|
{ |
67
|
2 |
|
if (method_exists($this->query, $name)) { |
68
|
1 |
|
$this->query->$name($arguments); |
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
throw new \BadMethodCallException; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $limit |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
1 |
|
public function limit($limit) |
80
|
|
|
{ |
81
|
1 |
|
$this->limit = $limit; |
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param bool|string $path |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
21 |
|
public function path($path = false) |
90
|
|
|
{ |
91
|
21 |
|
$this->path = $path; |
92
|
21 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $string |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
1 |
|
public function raw($string) |
100
|
|
|
{ |
101
|
1 |
|
$this->query->add(QueryParser::parse($string)); |
102
|
1 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $string |
107
|
|
|
* @param null|string $field |
108
|
|
|
* @param null $offsets |
109
|
|
|
* @return $this |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
2 |
|
public function phrase($string, $field = null, $offsets = null) |
113
|
|
|
{ |
114
|
2 |
|
$this->query->add(new Phrase(explode(' ', $string), $offsets, $field)); |
115
|
2 |
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $string |
120
|
|
|
* @param null|string $field |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
1 |
|
public function fuzzy($string, $field = null) |
124
|
|
|
{ |
125
|
1 |
|
$this->query->add(new Fuzzy($this->indexTerm($field, $string))); |
126
|
1 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $string |
131
|
|
|
* @param null|string $field |
132
|
|
|
* @return Term |
133
|
|
|
*/ |
134
|
4 |
|
protected function indexTerm($string, $field = null) |
135
|
|
|
{ |
136
|
4 |
|
return new Term(strtolower($string), $field); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $string |
141
|
|
|
* @param null|string $field |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
1 |
|
public function wildcard($string, $field = null) |
145
|
|
|
{ |
146
|
1 |
|
$this->query->add(new Wildcard($this->indexTerm($string, $field))); |
147
|
1 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Where |
152
|
|
|
* |
153
|
|
|
* A helper method to access phrase or to pass multiple fields. Phrase doesn't "match" exactly and |
154
|
|
|
* allows searching within the text field rather than matching the whole string. |
155
|
|
|
* |
156
|
|
|
* @param boolean|$string |
157
|
|
|
* @param null|string $field |
158
|
|
|
* @return $this|bool |
159
|
|
|
*/ |
160
|
2 |
|
public function where($string, $field = null) |
161
|
|
|
{ |
162
|
2 |
|
is_array($field) |
163
|
2 |
|
? $this->multiTerm($this->mapWhereArray($string, $field)) |
164
|
2 |
|
: $this->phrase($string, $field); |
165
|
|
|
|
166
|
2 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Match |
171
|
|
|
* |
172
|
|
|
* Provides an exact pattern match. |
173
|
|
|
* |
174
|
|
|
* @param $string |
175
|
|
|
* @param null $field |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
2 |
|
public function match($string, $field = null) |
179
|
|
|
{ |
180
|
2 |
|
$this->query->add($this->term($string, $field)); |
181
|
2 |
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param array $terms |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
1 |
|
public function multiTerm(array $terms) |
189
|
|
|
{ |
190
|
1 |
|
$multiTerm = new MultiTerm; |
191
|
1 |
|
foreach ($terms as $field => $value) { |
192
|
1 |
|
$multiTerm->addTerm($this->indexTerm($value, $field), null); |
193
|
1 |
|
} |
194
|
|
|
|
195
|
1 |
|
$this->query->add($multiTerm); |
196
|
|
|
|
197
|
1 |
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param string $string |
202
|
|
|
* @param array $array |
203
|
|
|
* @return mixed |
204
|
|
|
*/ |
205
|
1 |
|
private function mapWhereArray($string, array $array) |
206
|
|
|
{ |
207
|
1 |
|
return array_map( |
208
|
|
|
function() use ($string) { |
209
|
1 |
|
return $string; |
210
|
1 |
|
}, |
211
|
1 |
|
array_flip($array) |
212
|
1 |
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $string |
217
|
|
|
* @param string|null $field |
218
|
|
|
* @return QueryTerm |
219
|
|
|
*/ |
220
|
3 |
|
public function term($string, $field = null) |
221
|
|
|
{ |
222
|
3 |
|
return new QueryTerm($this->indexTerm($string, $field)); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return mixed |
227
|
|
|
*/ |
228
|
2 |
|
public function hits() |
229
|
|
|
{ |
230
|
2 |
|
$index = $this->index->limit($this->limit)->open($this->path)->get(); |
231
|
2 |
|
self::$boolean = $this->query->getBoolean(); |
232
|
2 |
|
return $this->mapIds($index->find(self::$boolean)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @param array|QueryHit $array |
237
|
|
|
* @return mixed |
238
|
|
|
*/ |
239
|
3 |
|
private function mapIds($array) |
240
|
|
|
{ |
241
|
3 |
|
return array_map( |
242
|
3 |
|
function(QueryHit $hit) { |
243
|
3 |
|
return isset($hit->xref_id) ? $hit->xref_id : null; |
244
|
3 |
|
}, |
245
|
|
|
$array |
246
|
3 |
|
); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return mixed |
251
|
|
|
*/ |
252
|
1 |
|
public static function getLastQuery() |
253
|
|
|
{ |
254
|
1 |
|
return self::$boolean; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|