forked from himiklab/yii2-search-component-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch.php
199 lines (178 loc) · 5.9 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* @link https://github.com/himiklab/yii2-search-component-v2
* @copyright Copyright (c) 2014 HimikLab
* @license http://opensource.org/licenses/MIT MIT
*/
namespace himiklab\yii2\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\MultiTerm;
use ZendSearch\Lucene\Search\Query\Term;
use ZendSearch\Lucene\Search\Query\Wildcard;
use ZendSearch\Lucene\Search\QueryParser;
/**
* Yii2 Zend Lucine search component v2.
*
* @see http://framework.zend.com/manual/1.12/en/zend.search.lucene.html
* @author HimikLab
* @package himiklab\yii2\search
*/
class Search extends Component
{
/** @var array */
public $models = [];
/** @var string alias or directory path */
public $indexDirectory = '@app/runtime/search';
/** @var bool */
public $caseSensitivity = false;
/** @var int Minimum term prefix length (number of minimum non-wildcard characters) */
public $minPrefixLength = 3;
/** @var int 0 means no limit */
public $resultsLimit = 0;
/** @var \ZendSearch\Lucene\Index */
protected $luceneIndex;
public function __destruct()
{
$this->luceneIndex->commit();
}
public function init()
{
QueryParser::setDefaultEncoding('UTF-8');
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 $modelName */
foreach ($this->models as $modelName) {
/** @var behaviors\SearchBehavior $model */
/** @var array $page */
$model = new $modelName;
if ($model->hasMethod('getSearchModels')) {
foreach ($model->getSearchModels()->all() as $pageModel) {
$this->luceneIndex->addDocument($this->createDocument(
call_user_func($model->searchFields, $pageModel)
));
}
} else {
throw new InvalidConfigException(
"Not found right `SearchBehavior` behavior in `{$modelName}`."
);
}
}
}
/**
* Search page for the term in the index.
* @param string $term
* @param array $fields (string => string)
* @return array ('results' => \ZendSearch\Lucene\Search\QueryHit[], 'query' => string)
*/
public function find($term, $fields = [])
{
Wildcard::setMinPrefixLength($this->minPrefixLength);
Lucene::setResultSetLimit($this->resultsLimit);
if (empty($fields)) {
return [
'results' => $this->luceneIndex->find($term),
'query' => $term
];
}
$fieldTerms[] = new IndexTerm($term);
foreach ($fields as $field => $fieldText) {
$fieldTerms[] = new IndexTerm($fieldText, $field);
}
return [
'results' => $this->luceneIndex->find(new MultiTerm($fieldTerms)),
'query' => $term
];
}
/**
* Delete document from the index.
* @param string $text
* @param string|null $field
*/
public function delete($text, $field = null)
{
$query = new Term(new IndexTerm($text, $field));
$hits = $this->luceneIndex->find($query);
foreach ($hits as $hit) {
$this->luceneIndex->delete($hit);
}
}
/**
* Add document to the index.
* @param array $fields ('name' => string, 'value' => string, ['type' => string])
* Default type is 'text'.
*/
public function add($fields)
{
$this->luceneIndex->addDocument(
$this->createDocument($fields)
);
}
/**
* 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();
}
/**
* @param string $directory
* @return \ZendSearch\Lucene\SearchIndexInterface
*/
protected function getLuceneIndex($directory)
{
if (file_exists($directory . DIRECTORY_SEPARATOR . 'segments.gen')) {
return Lucene::open($directory);
} else {
return Lucene::create($directory);
}
}
/**
* @param array $fields ('name' => string, 'value' => string, ['type' => string])
* Default type is 'text'.
* @return Document
*/
protected function createDocument($fields)
{
$document = new Document();
foreach ($fields as $field) {
if (!isset($field['type'])) {
$field['type'] = behaviors\SearchBehavior::FIELD_TEXT;
}
$document->addField(Field::$field['type'](
$field['name'],
$field['value']
));
}
return $document;
}
}