-
Notifications
You must be signed in to change notification settings - Fork 206
/
FullText.php
281 lines (253 loc) · 9.53 KB
/
FullText.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\QueryBuilder;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Persistence\Legacy\Content\Gateway as ContentGateway;
use eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator;
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriterionHandler;
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter;
use eZ\Publish\Core\Persistence\TransformationProcessor;
/**
* Full text criterion handler.
*/
class FullText extends CriterionHandler
{
/**
* Full text search configuration options.
*
* @var array
*/
protected $configuration = [
// @see getStopWordThresholdValue()
'stopWordThresholdFactor' => 0.66,
'enableWildcards' => true,
'commands' => [
'apostrophe_normalize',
'apostrophe_to_doublequote',
'ascii_lowercase',
'ascii_search_cleanup',
'cyrillic_diacritical',
'cyrillic_lowercase',
'cyrillic_search_cleanup',
'cyrillic_transliterate_ascii',
'doublequote_normalize',
'endline_search_normalize',
'greek_diacritical',
'greek_lowercase',
'greek_normalize',
'greek_transliterate_ascii',
'hebrew_transliterate_ascii',
'hyphen_normalize',
'inverted_to_normal',
'latin1_diacritical',
'latin1_lowercase',
'latin1_transliterate_ascii',
'latin-exta_diacritical',
'latin-exta_lowercase',
'latin-exta_transliterate_ascii',
'latin_lowercase',
'latin_search_cleanup',
'latin_search_decompose',
'math_to_ascii',
'punctuation_normalize',
'space_normalize',
'special_decompose',
'specialwords_search_normalize',
'tab_search_normalize',
],
];
/**
* @var int|null
*
* @see getStopWordThresholdValue()
*/
private $stopWordThresholdValue;
/**
* Transformation processor to normalize search strings.
*
* @var \eZ\Publish\Core\Persistence\TransformationProcessor
*/
protected $processor;
/** @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator */
private $languageMaskGenerator;
/**
* @param array $configuration
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException On invalid $configuration values
* @throws \Doctrine\DBAL\DBALException
*/
public function __construct(
Connection $connection,
TransformationProcessor $processor,
MaskGenerator $languageMaskGenerator,
array $configuration = []
) {
parent::__construct($connection);
$this->configuration = $configuration + $this->configuration;
$this->processor = $processor;
if (
$this->configuration['stopWordThresholdFactor'] < 0 ||
$this->configuration['stopWordThresholdFactor'] > 1
) {
throw new InvalidArgumentException(
"\$configuration['stopWordThresholdFactor']",
'Stop Word Threshold Factor needs to be between 0 and 1, got: ' . $this->configuration['stopWordThresholdFactor']
);
}
$this->languageMaskGenerator = $languageMaskGenerator;
}
/**
* Check if this criterion handler accepts to handle the given criterion.
*
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
*
* @return bool
*/
public function accept(Criterion $criterion)
{
return $criterion instanceof Criterion\FullText;
}
/**
* Tokenize String.
*
* @param string $string
*
* @return array
*/
protected function tokenizeString($string)
{
return preg_split('/[^\w|*]/u', $string, -1, PREG_SPLIT_NO_EMPTY);
}
/**
* Get single word query expression.
*
* Depending on the configuration of the full text search criterion
* converter wildcards are either transformed into the respective LIKE
* queries, or everything is just compared using equal.
*/
protected function getWordExpression(QueryBuilder $query, string $token): string
{
if ($this->configuration['enableWildcards'] && $token[0] === '*') {
return $query->expr()->like(
'word',
$query->createNamedParameter('%' . substr($token, 1))
);
}
if ($this->configuration['enableWildcards'] && $token[strlen($token) - 1] === '*') {
return $query->expr()->like(
'word',
$query->createNamedParameter(substr($token, 0, -1) . '%')
);
}
return $query->expr()->eq('word', $query->createNamedParameter($token));
}
/**
* Get sub query to select relevant word IDs.
*
* @uses getStopWordThresholdValue To get threshold for words we would like to ignore in query.
*/
protected function getWordIdSubquery(QueryBuilder $query, string $string): string
{
$subQuery = $this->connection->createQueryBuilder();
$tokens = $this->tokenizeString(
$this->processor->transform($string, $this->configuration['commands'])
);
$wordExpressions = [];
foreach ($tokens as $token) {
$wordExpressions[] = $this->getWordExpression($query, $token);
}
// Search for provided string itself as well
$wordExpressions[] = $this->getWordExpression($query, $string);
$whereCondition = $subQuery->expr()->orX(...$wordExpressions);
// If stop word threshold is below 100%, make it part of $whereCondition
if ($this->configuration['stopWordThresholdFactor'] < 1) {
$whereCondition = $subQuery->expr()->andX(
$whereCondition,
$subQuery->expr()->lt(
'object_count',
$query->createNamedParameter($this->getStopWordThresholdValue(), ParameterType::STRING)
)
);
}
$subQuery
->select('id')
->from('ezsearch_word')
->where($whereCondition);
return $subQuery->getSQL();
}
public function handle(
CriteriaConverter $converter,
QueryBuilder $queryBuilder,
Criterion $criterion,
array $languageSettings
) {
$subSelect = $this->connection->createQueryBuilder();
$expr = $queryBuilder->expr();
$subSelect
->select(
'contentobject_id'
)->from(
'ezsearch_object_word_link'
)->where(
$expr->in(
'word_id',
// pass main Query Builder to set query parameters
$this->getWordIdSubquery($queryBuilder, $criterion->value)
)
);
if (!empty($languageSettings['languages'])) {
$languageMask = $this->languageMaskGenerator->generateLanguageMaskFromLanguageCodes(
$languageSettings['languages'],
$languageSettings['useAlwaysAvailable'] ?? true
);
$subSelect->andWhere(
$expr->gt(
$this->dbPlatform->getBitAndComparisonExpression(
'ezsearch_object_word_link.language_mask',
$queryBuilder->createNamedParameter($languageMask, ParameterType::INTEGER)
),
$queryBuilder->createNamedParameter(0, ParameterType::INTEGER)
)
);
}
return $expr->in(
'c.id',
$subSelect->getSQL()
);
}
/**
* Returns an exact content object count threshold to ignore common terms on.
*
* Common terms will be skipped if used in more then a given percentage of the total amount of content
* objects in the database. Percentage is defined by stopWordThresholdFactor configuration.
*
* Example: If stopWordThresholdFactor is 0.66 (66%), and a term like "the" exists in more then 66% of the content, it
* will ignore the phrase as it is assumed to not add any value ot the search.
*
* Caches the result for the instance used as we don't need this to be super accurate as it is based on percentage,
* set by stopWordThresholdFactor.
*
* @return int
*/
protected function getStopWordThresholdValue(): int
{
if ($this->stopWordThresholdValue !== null) {
return $this->stopWordThresholdValue;
}
// Cached value does not exists, do a simple count query on ezcontentobject table
$query = $this->connection->createQueryBuilder();
$query
->select($this->dbPlatform->getCountExpression('id'))
->from(ContentGateway::CONTENT_ITEM_TABLE);
$count = (int)$query->execute()->fetchColumn();
// Calculate the int stopWordThresholdValue based on count (first column) * factor
return $this->stopWordThresholdValue = (int)($count * $this->configuration['stopWordThresholdFactor']);
}
}