|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Lucene; |
|
4
|
|
|
|
|
5
|
|
|
use ZendSearch\Lucene\Lucene; |
|
6
|
|
|
use ZendSearch\Exception\ExceptionInterface; |
|
7
|
|
|
use ZendSearch\Lucene\Index as LuceneIndex; |
|
8
|
|
|
use ZendSearch\Lucene\SearchIndexInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Index |
|
12
|
|
|
* @package BestServedCold\LaravelZendSearch\Lucene |
|
13
|
|
|
*/ |
|
14
|
|
|
class Index |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var LuceneIndex |
|
18
|
|
|
*/ |
|
19
|
|
|
private $index; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string|boolean $path |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $path; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Filter |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $filter; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Index constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Filter $filter |
|
35
|
|
|
*/ |
|
36
|
23 |
|
public function __construct(Filter $filter) |
|
37
|
|
|
{ |
|
38
|
23 |
|
$this->filter = $filter; |
|
39
|
23 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Open |
|
43
|
|
|
* |
|
44
|
|
|
* @param string|boolean $path |
|
45
|
|
|
* @param bool $forceCreate |
|
46
|
|
|
* @return $this |
|
47
|
|
|
* @throws ExceptionInterface |
|
48
|
|
|
* @throws \Exception |
|
49
|
|
|
*/ |
|
50
|
4 |
|
public function open($path = false, $forceCreate = true) |
|
51
|
|
|
{ |
|
52
|
4 |
|
$this->path = $path ? $path : $this->path; |
|
53
|
4 |
|
$this->index = $this->createIndex($this->path(), $forceCreate); |
|
54
|
2 |
|
$this->filter->setFilters(); |
|
55
|
2 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param integer $limit |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function limit($limit) |
|
63
|
|
|
{ |
|
64
|
1 |
|
Lucene::setResultSetLimit($limit); |
|
65
|
1 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Create Index |
|
70
|
|
|
* |
|
71
|
|
|
* Extends the Lucene "open" method to create the index if it doesn't exist. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string|boolean $path |
|
74
|
|
|
* @param boolean $forceCreate |
|
75
|
|
|
* @return SearchIndexInterface |
|
76
|
|
|
* @throws \Exception |
|
77
|
|
|
*/ |
|
78
|
3 |
|
private function createIndex($path, $forceCreate = true) |
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
3 |
|
$index = Lucene::open($path); |
|
82
|
3 |
|
} catch (ExceptionInterface $error) { |
|
83
|
3 |
|
if ($forceCreate) { |
|
84
|
2 |
|
$index = Lucene::create($path); |
|
85
|
2 |
|
} else { |
|
86
|
1 |
|
throw $error; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $index; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param $path |
|
95
|
|
|
*/ |
|
96
|
23 |
|
public function setPath($path) |
|
97
|
|
|
{ |
|
98
|
23 |
|
$this->path = $path; |
|
99
|
23 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
* @throws \Exception |
|
104
|
|
|
*/ |
|
105
|
4 |
|
protected function path() |
|
106
|
|
|
{ |
|
107
|
4 |
|
if (!$this->path) { |
|
108
|
1 |
|
throw new \Exception('No path specified nor config variable set.'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
return $this->path; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return LuceneIndex |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function get() |
|
118
|
|
|
{ |
|
119
|
1 |
|
return $this->index; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|