-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEngine.php
228 lines (212 loc) · 7.06 KB
/
Engine.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace VFou\Search;
use DateTime;
use Exception;
use VFou\Search\Query\QueryBuilder;
use VFou\Search\Services\Index;
use VFou\Search\Tokenizers\AlphaNumericTokenizer;
use VFou\Search\Tokenizers\DateFormatTokenizer;
use VFou\Search\Tokenizers\DateSplitTokenizer;
use VFou\Search\Tokenizers\LowerCaseTokenizer;
use VFou\Search\Tokenizers\RemoveAccentsTokenizer;
use VFou\Search\Tokenizers\singleQuoteTokenizer;
use VFou\Search\Tokenizers\TrimPunctuationTokenizer;
use VFou\Search\Tokenizers\WhiteSpaceTokenizer;
class Engine
{
/**
* @var Index $index
*/
private $index;
/**
* @var array $config
*/
private $config;
/**
* Engine constructor.
* @param array $config
* @throws Exception
*/
public function __construct($config = [])
{
$defaultConfig = $this->getDefaultConfig();
$this->config = array_replace_recursive($defaultConfig, $config);
$this->index = new Index($this->config['config'], $this->config['schemas'], $this->config['types']);
}
/**
* Get the Engine's index. Used to perform modifications to the index,
* such as clearing the cache or rebuilding the index
* @return Index
*/
public function getIndex(){
return $this->index;
}
/**
* Insert or update a given document to the index
* @param $document
* @return bool
* @throws Exception
*/
public function update($document){
return $this->index->update($document);
}
/**
* Insert or update multiple documents to the index
* @param array $document
* @return bool
* @throws Exception
*/
public function updateMultiple(array $document){
return $this->index->updateMultiple($document);
}
/**
* perform a search
* @param string|array|QueryBuilder $query
* @param array $filters
* @return array
* @throws Exception
*/
public function search($query, $filters = []){
if(is_a($query, QueryBuilder::class)){
return $this->index->search($query->getQuery(), $query->getFilters());
}
return $this->index->search($query, $filters);
}
/**
* @param $token
* @param bool $providePonderations
* @return array
* @throws Exception
* @deprecated Suggesting functions now have another suggestion function available. Please use suggestToken($token) instead
*/
public function suggest($token){
return $this->suggestToken($token);
}
/**
* Suggest last word for a search
* @param $query
* @return array
* @throws Exception
*/
public function suggestToken($query){
$terms = explode(' ', $query);
$search = array_pop($terms);
$tokens = $this->index->tokenizeQuery($search);
$suggestions = [];
foreach($tokens as $token) {
$suggestions = array_replace($suggestions, $this->index->suggestToken($token));
}
$before = implode(' ',$terms);
foreach($suggestions as &$suggest){
$suggest = $before.' '.$suggest;
}
return array_chunk($suggestions, 10)[0];
}
/**
* @param $field
* @param $value
* @param bool|string $wrapSpan if true, wrap <span> tags around the matching values.
* if it's a string, adds the string as a class
* @return array
* @throws Exception
*/
public function suggestField($field, $value, $wrapSpan = false){
return $this->index->suggestField($field, $value, $wrapSpan);
}
/**
* delete the given document ID from the index
* @param $id
* @return bool
* @throws Exception
*/
public function delete($id){
return $this->index->delete($id);
}
/**
* Returns the default configuration
* @return array
*/
private function getDefaultConfig(){
return [
'config' => [
'var_dir' => $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'var',
'index_dir' => DIRECTORY_SEPARATOR.'engine'.DIRECTORY_SEPARATOR.'index',
'documents_dir' => DIRECTORY_SEPARATOR.'engine'.DIRECTORY_SEPARATOR.'documents',
'cache_dir' => DIRECTORY_SEPARATOR.'engine'.DIRECTORY_SEPARATOR.'cache',
'fuzzy_cost' => 1,
'approximate_limit' => 5,
'connex' => [
'threshold' => 0.9,
'min' => 3,
'max' => 10,
'limitToken' => 20,
'limitDocs' => 10
],
'serializableObjects' => [
DateTime::class => function($datetime) { /** @var DateTime $datetime */ return $datetime->getTimestamp(); }
]
],
'schemas' => [
'example-post' => [
'title' => [
'_type' => 'string',
'_indexed' => true,
'_boost' => 10
],
'content' => [
'_type' => 'text',
'_indexed' => true,
'_boost' => 0.5
],
'date' => [
'_type' => 'datetime',
'_indexed' => true,
'_boost' => 2
],
'categories' => [
'_type' => 'list',
'_type.' => 'string',
'_indexed' => true,
'_filterable' => true,
'_boost' => 6
],
'comments' => [
'_type' => 'list',
'_type.' => 'array',
'_array' => [
'author' => [
'_type' => 'string',
'_indexed' => true,
'_filterable' => true,
'_boost' => 1
],
'date' => [
'_type' => 'datetime',
'_indexed' => true,
'_boost' => 0
],
'message' => [
'_type' => 'text',
'_indexed' => true,
'_boost' => 0.1
]
]
]
]
],
'types' => [
'datetime' => [
DateFormatTokenizer::class,
DateSplitTokenizer::class
],
'_default' => [
RemoveAccentsTokenizer::class,
LowerCaseTokenizer::class,
WhiteSpaceTokenizer::class,
singleQuoteTokenizer::class,
AlphaNumericTokenizer::class
]
]
];
}
}