forked from heartshare/yii2-search-component
/
Search.php
173 lines (151 loc) · 4.95 KB
/
Search.php
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
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* @link https://github.com/himiklab/yii2-search-component
* @copyright Copyright (c) 2014 HimikLab
* @license http://opensource.org/licenses/MIT MIT
*/
namespace himiklab\search;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use yii\helpers\FileHelper;
use ZendSearch\Lucene\Analysis\Analyzer\Analyzer;
use ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8;
use ZendSearch\Lucene\Analysis\Analyzer\Common\Utf8\CaseInsensitive;
use ZendSearch\Lucene\Document;
use ZendSearch\Lucene\Document\Field;
use ZendSearch\Lucene\Index\Term as IndexTerm;
use ZendSearch\Lucene\Lucene;
use ZendSearch\Lucene\Search\Query\Term;
use ZendSearch\Lucene\Search\Query\Wildcard;
/**
* Yii2 Zend Lucene search component.
*
* @see http://framework.zend.com/manual/1.12/en/zend.search.lucene.html
* @author HimikLab
* @package himiklab\search
*/
class Search extends Component
{
/** @var array $models */
public $models = [];
/** @var string $indexDirectory alias or directory path */
public $indexDirectory = '@app/runtime/search';
/** @var bool $caseSensitivity */
public $caseSensitivity = false;
/** @var int $minPrefixLength Minimum term prefix length (number of minimum non-wildcard characters) */
public $minPrefixLength = 3;
/** @var int $resultsLimit 0 means no limit */
public $resultsLimit = 0;
/** @var \ZendSearch\Lucene\Index $luceneIndex */
protected $luceneIndex;
public function __destruct()
{
$this->luceneIndex->commit();
}
public function init()
{
if ($this->caseSensitivity) {
Analyzer::setDefault(new Utf8());
} else {
Analyzer::setDefault(new CaseInsensitive());
}
$this->indexDirectory = FileHelper::normalizePath(Yii::getAlias($this->indexDirectory));
$this->luceneIndex = $this->getLuceneIndex($this->indexDirectory);
}
/**
* Indexing the contents of the specified models.
* @throws InvalidConfigException
*/
public function index()
{
if ($this->luceneIndex->count() !== 0) {
$this->luceneIndex = Lucene::create($this->indexDirectory);
}
/** @var \yii\db\ActiveRecord $model */
foreach ($this->models as $model) {
if (!is_subclass_of($model::className(), __NAMESPACE__ . '\SearchInterface')) {
throw new InvalidConfigException('The model object must implement `SearchInterface`');
}
/** @var SearchInterface $page */
foreach ($model::find()->all() as $page) {
$this->luceneIndex->addDocument(
$this->createDocument($page->getSearchTitle(), $page->getSearchBody(), $page->getSearchUrl())
);
}
}
}
/**
* Search page for the term in the index.
* @param string $term
* @return array ('results' => array \ZendSearch\Lucene\Search\QueryHit, 'query' => string)
*/
public function find($term)
{
Wildcard::setMinPrefixLength($this->minPrefixLength);
Lucene::setResultSetLimit($this->resultsLimit);
return ['results' => $this->luceneIndex->find($term), 'query' => $term];
}
/**
* Remove page from the index.
* @param string $url
*/
public function delete($url)
{
$query = new Term(new IndexTerm($url, 'url'));
$hits = $this->luceneIndex->find($query);
foreach ($hits as $hit) {
$this->luceneIndex->delete($hit);
}
}
/**
* Add page to the index.
* @param string $title
* @param string $body
* @param string $url
*/
public function add($title, $body, $url)
{
$this->delete($url);
$this->luceneIndex->addDocument(
$this->createDocument($title, $body, $url)
);
}
/**
* Full index optimization.
* Each index segment is entirely independent portion of data.
* So indexes containing more segments need more memory and time for searching.
* Index optimization is a process of merging several segments into a new one.
* Index optimization works with data streams and doesn't
* take a lot of memory but does require processor resources and time.
*/
public function optimize()
{
$this->luceneIndex->optimize();
}
protected function getLuceneIndex($directory)
{
if (file_exists($directory . DIRECTORY_SEPARATOR . 'segments.gen')) {
return Lucene::open($directory);
} else {
return Lucene::create($directory);
}
}
protected function createDocument($title, $body, $url)
{
$document = new Document();
$document->addField(Field::text(
'title',
$title
));
$document->addField(Field::text(
'body',
strip_tags($body)
));
$document->addField(Field::keyword(
'url',
$url
));
return $document;
}
}