-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathSearch.php
814 lines (685 loc) · 26.6 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\services;
use Craft;
use craft\base\Element;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\config\DbConfig;
use craft\db\Query;
use craft\db\Table;
use craft\events\SearchEvent;
use craft\helpers\Db;
use craft\helpers\Search as SearchHelper;
use craft\helpers\StringHelper;
use craft\models\Site;
use craft\search\SearchQuery;
use craft\search\SearchQueryTerm;
use craft\search\SearchQueryTermGroup;
use yii\base\Component;
use yii\db\Schema;
/**
* Handles search operations.
* An instance of the Search service is globally accessible in Craft via [[\craft\base\ApplicationTrait::getSearch()|`Craft::$app->search`]].
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
class Search extends Component
{
// Constants
// =========================================================================
/**
* @event SearchEvent The event that is triggered before a search is performed.
*/
const EVENT_BEFORE_SEARCH = 'beforeSearch';
/**
* @event SearchEvent The event that is triggered after a search is performed.
*/
const EVENT_AFTER_SEARCH = 'afterSearch';
// Properties
// =========================================================================
/**
* @var int The minimum word length that keywords must be in order to use a full-text search.
*/
public $minFullTextWordLength;
/**
* @var
*/
private $_tokens;
/**
* @var
*/
private $_terms;
/**
* @var
*/
private $_groups;
/**
* @var int Because the `keywords` column in the search index table is a
* B-TREE index on Postgres, you can get an "index row size exceeds maximum
* for index" error with a lot of data. This value is a hard limit to
* truncate search index data for a single row in Postgres.
*/
public $maxPostgresKeywordLength = 2450;
// Public Methods
// =========================================================================
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->minFullTextWordLength === null) {
if (Craft::$app->getDb()->getIsMysql()) {
$this->minFullTextWordLength = 4;
} else {
$this->minFullTextWordLength = 1;
}
}
}
/**
* Indexes the attributes of a given element defined by its element type.
*
* @param ElementInterface $element
* @return bool Whether the indexing was a success.
* @throws \craft\errors\SiteNotFoundException
*/
public function indexElementAttributes(ElementInterface $element): bool
{
/** @var Element $element */
// Does it have any searchable attributes?
$searchableAttributes = $element::searchableAttributes();
$searchableAttributes[] = 'slug';
if ($element::hasTitles()) {
$searchableAttributes[] = 'title';
}
foreach ($searchableAttributes as $attribute) {
$value = $element->getSearchKeywords($attribute);
$this->_indexElementKeywords($element->id, $attribute, '0', $element->siteId, $value);
}
return true;
}
/**
* Indexes the field values for a given element and site.
*
* @param int $elementId The ID of the element getting indexed.
* @param int $siteId The site ID of the content getting indexed.
* @param array $fields The field values, indexed by field ID.
* @return bool Whether the indexing was a success.
* @throws \craft\errors\SiteNotFoundException
*/
public function indexElementFields(int $elementId, int $siteId, array $fields): bool
{
foreach ($fields as $fieldId => $value) {
$this->_indexElementKeywords($elementId, 'field', (string)$fieldId, $siteId, $value);
}
return true;
}
/**
* Filters a list of element IDs by a given search query.
*
* @param int[] $elementIds The list of element IDs to filter by the search query.
* @param string|array|SearchQuery $query The search query (either a string or a SearchQuery instance)
* @param bool $scoreResults Whether to order the results based on how closely they match the query.
* @param int|null $siteId The site ID to filter by.
* @param bool $returnScores Whether the search scores should be included in the results. If true, results will be returned as `element ID => score`.
* @return array The filtered list of element IDs.
*/
public function filterElementIdsByQuery(array $elementIds, $query, bool $scoreResults = true, int $siteId = null, bool $returnScores = false): array
{
if (is_string($query)) {
$query = new SearchQuery($query, Craft::$app->getConfig()->getGeneral()->defaultSearchTermOptions);
} else if (is_array($query)) {
$options = $query;
$query = $options['query'];
unset($options['query']);
$options = array_merge(Craft::$app->getConfig()->getGeneral()->defaultSearchTermOptions, $options);
$query = new SearchQuery($query, $options);
}
// Fire a 'beforeSearch' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_SEARCH)) {
$this->trigger(self::EVENT_BEFORE_SEARCH, new SearchEvent([
'elementIds' => $elementIds,
'query' => $query,
'siteId' => $siteId,
]));
}
// Get tokens for query
$this->_tokens = $query->getTokens();
$this->_terms = [];
$this->_groups = [];
// Set Terms and Groups based on tokens
foreach ($this->_tokens as $obj) {
if ($obj instanceof SearchQueryTermGroup) {
$this->_groups[] = $obj->terms;
} else {
$this->_terms[] = $obj;
}
}
// Get where clause from tokens, bail out if no valid query is there
$where = $this->_getWhereClause($siteId);
if ($where === false || empty($where)) {
return [];
}
if ($siteId !== null) {
$where .= sprintf(' AND %s = %s', Craft::$app->getDb()->quoteColumnName('siteId'), Craft::$app->getDb()->quoteValue($siteId));
}
// Begin creating SQL
$sql = sprintf('SELECT * FROM %s WHERE %s', Craft::$app->getDb()->quoteTableName(Table::SEARCHINDEX), $where);
// Append elementIds to QSL
if (!empty($elementIds)) {
$sql .= sprintf(' AND %s IN (%s)',
Craft::$app->getDb()->quoteColumnName('elementId'),
implode(',', $elementIds)
);
}
// Execute the sql
$results = Craft::$app->getDb()->createCommand($sql)->queryAll();
// Are we scoring the results?
if ($scoreResults) {
$scoresByElementId = [];
// Loop through results and calculate score per element
foreach ($results as $row) {
$elementId = $row['elementId'];
$score = $this->_scoreRow($row);
if (!isset($scoresByElementId[$elementId])) {
$scoresByElementId[$elementId] = $score;
} else {
$scoresByElementId[$elementId] += $score;
}
}
// Sort found elementIds by score
arsort($scoresByElementId);
if ($returnScores) {
return $scoresByElementId;
}
// Just return the ordered element IDs
return array_keys($scoresByElementId);
}
// Don't apply score, just return the IDs
$elementIds = [];
foreach ($results as $row) {
$elementIds[] = $row['elementId'];
}
$elementIds = array_unique($elementIds);
// Fire an 'afterSearch' event
if ($this->hasEventHandlers(self::EVENT_AFTER_SEARCH)) {
$this->trigger(self::EVENT_AFTER_SEARCH, new SearchEvent([
'elementIds' => $elementIds,
'query' => $query,
'siteId' => $siteId,
]));
}
return $elementIds;
}
// Private Methods
// =========================================================================
/**
* Indexes keywords for a specific element attribute/field.
*
* @param int $elementId
* @param string $attribute
* @param string $fieldId
* @param int $siteId
* @param string $dirtyKeywords
* @throws \craft\errors\SiteNotFoundException
*/
private function _indexElementKeywords(int $elementId, string $attribute, string $fieldId, int $siteId, string $dirtyKeywords)
{
$attribute = strtolower($attribute);
// Acquire a lock for this element/attribute/field ID/site ID
$mutex = Craft::$app->getMutex();
$lockKey = "searchindex:{$elementId}:{$attribute}:{$fieldId}:{$siteId}";
if (!$mutex->acquire($lockKey)) {
// Not worth waiting around; for all we know the other process has newer search attributes anyway
return;
}
// Drop all current rows for this element/attribute/site ID
$db = Craft::$app->getDb();
$db->createCommand()
->delete(Table::SEARCHINDEX, [
'elementId' => $elementId,
'attribute' => $attribute,
'fieldId' => $fieldId,
'siteId' => $siteId,
])
->execute();
$driver = $db->getDriverName();
/** @var Site $site */
$site = Craft::$app->getSites()->getSiteById($siteId);
// Clean 'em up
$cleanKeywords = SearchHelper::normalizeKeywords($dirtyKeywords, [], true, $site->language);
// Save 'em
$columns = [
'elementId' => $elementId,
'attribute' => $attribute,
'fieldId' => $fieldId,
'siteId' => $site->id,
];
if ($cleanKeywords !== null && $cleanKeywords !== false && $cleanKeywords !== '') {
// Add padding around keywords
$cleanKeywords = ' ' . $cleanKeywords . ' ';
}
if ($driver === DbConfig::DRIVER_PGSQL) {
$maxSize = $this->maxPostgresKeywordLength;
} else {
$maxSize = Db::getTextualColumnStorageCapacity(Schema::TYPE_TEXT);
}
if ($maxSize !== null && $maxSize !== false) {
$cleanKeywords = $this->_truncateSearchIndexKeywords($cleanKeywords, $maxSize);
}
$columns['keywords'] = $cleanKeywords;
if ($driver === DbConfig::DRIVER_PGSQL) {
$columns['keywords_vector'] = $cleanKeywords;
}
// Insert/update the row in searchindex
$db->createCommand()
->insert(Table::SEARCHINDEX, $columns, false)
->execute();
// Release the lock
$mutex->release($lockKey);
}
/**
* Calculate score for a result.
*
* @param array $row A single result from the search query.
* @return float The total score for this row.
*/
private function _scoreRow(array $row): float
{
// Starting point
$score = 0;
// Loop through AND-terms and score each one against this row
foreach ($this->_terms as $term) {
$score += $this->_scoreTerm($term, $row);
}
// Loop through each group of OR-terms
foreach ($this->_groups as $terms) {
// OR-terms are weighted less depending on the amount of OR terms in the group
$weight = 1 / count($terms);
// Get the score for each term and add it to the total
foreach ($terms as $term) {
$score += $this->_scoreTerm($term, $row, $weight);
}
}
return $score;
}
/**
* Calculate score for a row/term combination.
*
* @param SearchQueryTerm $term The SearchQueryTerm to score.
* @param array $row The result row to score against.
* @param float|int $weight Optional weight for this term.
* @return float The total score for this term/row combination.
*/
private function _scoreTerm(SearchQueryTerm $term, array $row, $weight = 1): float
{
// Skip these terms: exact filtering is just that, no weighted search applies since all elements will
// already apply for these filters.
if ($term->exact || !($keywords = $this->_normalizeTerm($term->term))) {
return 0;
}
// Account for substrings
if (!$term->subLeft) {
$keywords = ' ' . $keywords;
}
if (!$term->subRight) {
$keywords .= ' ';
}
// Get haystack and safe word count
$haystack = $row['keywords'];
$wordCount = count(array_filter(explode(' ', $haystack)));
// Get number of matches
$score = StringHelper::countSubstrings($haystack, $keywords);
if ($score) {
// Exact match
if (trim($keywords) === trim($haystack)) {
$mod = 100;
} // Don't scale up for substring matches
else if ($term->subLeft || $term->subRight) {
$mod = 10;
} else {
$mod = 50;
}
// If this is a title, 5X it
if ($row['attribute'] === 'title') {
$mod *= 5;
}
$score = ($score / $wordCount) * $mod * $weight;
}
return $score;
}
/**
* Get the complete where clause for current tokens
*
* @param int|null $siteId The site ID to search within
* @return string|false
*/
private function _getWhereClause(int $siteId = null)
{
$where = [];
// Add the regular terms to the WHERE clause
if (!empty($this->_terms)) {
$condition = $this->_processTokens($this->_terms, true, $siteId);
if ($condition === false) {
return false;
}
$where[] = $condition;
}
// Add each group to the where clause
foreach ($this->_groups as $group) {
$condition = $this->_processTokens($group, false, $siteId);
if ($condition === false) {
return false;
}
$where[] = $condition;
}
// And combine everything with AND
return implode(' AND ', $where);
}
/**
* Generates partial WHERE clause for search from given tokens
*
* @param array $tokens
* @param bool $inclusive
* @param int|null $siteId
* @return string|false
* @throws \Throwable
*/
private function _processTokens(array $tokens = [], bool $inclusive = true, int $siteId = null)
{
$glue = $inclusive ? ' AND ' : ' OR ';
$where = [];
$words = [];
foreach ($tokens as $obj) {
// Get SQL and/or keywords
list($sql, $keywords) = $this->_getSqlFromTerm($obj, $siteId);
if ($sql === false && $inclusive) {
return false;
}
// If we have SQL, just add that
if ($sql) {
$where[] = $sql;
} // No SQL but keywords, save them for later
else if ($keywords !== null && $keywords !== '') {
if ($inclusive && Craft::$app->getDb()->getIsMysql()) {
$keywords = '+' . $keywords;
}
$words[] = $keywords;
}
}
// If we collected full-text words, combine them into one
if (!empty($words)) {
$where[] = $this->_sqlFullText($words, true, $glue);
}
// If we have valid where clauses now, stringify them
if (!empty($where)) {
// Implode WHERE clause to a string
$where = implode($glue, $where);
// And group together for non-inclusive queries
if (!$inclusive) {
$where = "({$where})";
}
} else {
// If the tokens didn't produce a valid where clause,
// make sure we return false
$where = false;
}
return $where;
}
/**
* Generates a piece of WHERE clause for fallback (LIKE) search from search term
* or returns keywords to use in a full text search clause
*
* @param SearchQueryTerm $term
* @param int|null $siteId
* @return array
* @throws \Throwable
*/
private function _getSqlFromTerm(SearchQueryTerm $term, int $siteId = null): array
{
// Initiate return value
$sql = null;
$keywords = null;
$isMysql = Craft::$app->getDb()->getIsMysql();
// Check for other attributes
if ($term->attribute !== null) {
// Is attribute a valid fieldId?
$fieldId = $this->_getFieldIdFromAttribute($term->attribute);
if ($fieldId) {
$attr = 'fieldId';
$val = $fieldId;
} else {
$attr = 'attribute';
$val = $term->attribute;
}
// Use subselect for attributes
$subSelect = $this->_sqlWhere($attr, '=', $val);
} else {
$subSelect = null;
}
// Sanitize term
if ($term->term !== null) {
$keywords = $this->_normalizeTerm($term->term);
// Make sure that it didn't result in an empty string (e.g. if they entered '&')
// unless it's meant to search for *anything* (e.g. if they entered 'attribute:*').
if ($keywords !== '' || $term->subLeft) {
// If we're on PostgreSQL and this is a phrase or exact match, we have to special case it.
if (!$isMysql && $term->phrase) {
$sql = $this->_sqlPhraseExactMatch($keywords, $term->exact);
} else {
// Create fulltext clause from term
if ($this->_doFullTextSearch($keywords, $term)) {
if ($term->subRight) {
if ($isMysql) {
$keywords .= '*';
} else {
$keywords .= ':*';
}
}
// Add quotes for exact match
if ($isMysql && StringHelper::contains($keywords, ' ')) {
if (StringHelper::first($keywords, 1) === '*') {
$keywords = StringHelper::insert($keywords, '"', 1);
} else {
$keywords = '"' . $keywords;
}
if (StringHelper::last($keywords, 1) === '*') {
$keywords = StringHelper::insert($keywords, '"', StringHelper::length($keywords) - 1);
} else {
$keywords .= '"';
}
}
// Determine prefix for the full-text keyword
if ($term->exclude) {
$keywords = '-' . $keywords;
}
// Only create an SQL clause if there's a subselect. Otherwise, return the keywords.
if ($subSelect !== null) {
// If there is a subselect, create the full text SQL bit
$sql = $this->_sqlFullText($keywords);
}
} // Create LIKE clause from term
else {
if ($term->exact) {
// Create exact clause from term
$operator = $term->exclude ? 'NOT LIKE' : 'LIKE';
$keywords = ($term->subLeft ? '%' : ' ') . $keywords . ($term->subRight ? '%' : ' ');
} else {
// Create LIKE clause from term
$operator = $term->exclude ? 'NOT LIKE' : 'LIKE';
$keywords = ($term->subLeft ? '%' : '% ') . $keywords . ($term->subRight ? '%' : ' %');
}
// Generate the SQL
$sql = $this->_sqlWhere('keywords', $operator, $keywords);
}
}
}
} else {
// Support for attribute:* syntax to just check if something has *any* keyword value.
if ($term->subLeft) {
$sql = $this->_sqlWhere('keywords', '!=', '');
}
}
// If we have a where clause in the subselect, add the keyword bit to it.
if ($subSelect !== null && $sql !== null) {
$sql = $this->_sqlSubSelect($subSelect . ' AND ' . $sql, $siteId);
// We need to reset keywords even if the subselect ended up in no results.
$keywords = null;
}
return [$sql, $keywords];
}
/**
* Normalize term from tokens, keep a record for cache.
*
* @param string $term
* @return string
*/
private function _normalizeTerm(string $term): string
{
static $terms = [];
if (!array_key_exists($term, $terms)) {
$terms[$term] = SearchHelper::normalizeKeywords($term);
}
return $terms[$term];
}
/**
* Get the fieldId for given attribute or 0 for unmatched.
*
* @param string $attribute
* @return int
*/
private function _getFieldIdFromAttribute(string $attribute): int
{
// Get field id from service
/** @var Field $field */
$field = Craft::$app->getFields()->getFieldByHandle($attribute);
// Fallback to 0
return $field ? $field->id : 0;
}
/**
* Get SQL bit for simple WHERE clause
*
* @param string $key The attribute.
* @param string $oper The operator.
* @param string $val The value.
* @return string
*/
private function _sqlWhere(string $key, string $oper, string $val): string
{
$key = Craft::$app->getDb()->quoteColumnName($key);
return sprintf("(%s %s '%s')", $key, $oper, $val);
}
/**
* Get SQL necessary for a full text search.
*
* @param mixed $val String or Array of keywords
* @param bool $bool Use In Boolean Mode or not
* @param string $glue If multiple values are passed in as an array, the operator to combine them (AND or OR)
* @return string
* @throws \Throwable
*/
private function _sqlFullText($val, bool $bool = true, string $glue = ' AND '): string
{
if (Craft::$app->getDb()->getIsMysql()) {
return sprintf("MATCH(%s) AGAINST('%s'%s)", Craft::$app->getDb()->quoteColumnName('keywords'), (is_array($val) ? implode(' ', $val) : $val), ($bool ? ' IN BOOLEAN MODE' : ''));
}
if ($glue === ' AND ') {
$glue = ' & ';
} else {
$glue = ' | ';
}
if (is_array($val)) {
foreach ($val as $key => $value) {
if (StringHelper::contains($value, ' ')) {
$temp = explode(' ', $val[$key]);
$temp = implode(' & ', $temp);
$val[$key] = $temp;
}
}
} else {
// If where here, it's a single string with punctuation that's been stripped out (i.e. "multi-site").
// We can assume "and".
if (StringHelper::contains($val, ' ')) {
$val = StringHelper::replace($val, ' ', ' & ');
}
}
return sprintf("%s @@ '%s'::tsquery", Craft::$app->getDb()->quoteColumnName('keywords_vector'), (is_array($val) ? implode($glue, $val) : $val));
}
/**
* Get SQL bit for sub-selects.
*
* @param string $where
* @param int|null $siteId
* @return string|false
*/
private function _sqlSubSelect(string $where, int $siteId = null)
{
$query = (new Query())
->select(['elementId'])
->from([Table::SEARCHINDEX])
->where($where);
if ($siteId !== null) {
$query->andWhere(['siteId' => $siteId]);
}
$elementIds = $query->column();
if (!empty($elementIds)) {
return Craft::$app->getDb()->quoteColumnName('elementId') . ' IN (' . implode(', ', $elementIds) . ')';
}
return false;
}
/**
* Whether or not to do a full text search or not.
*
* @param string $keywords
* @param SearchQueryTerm $term
* @return bool
*/
private function _doFullTextSearch(string $keywords, SearchQueryTerm $term): bool
{
return $keywords !== '' && !$term->subLeft && !$term->exact && !$term->exclude && strlen($keywords) >= $this->minFullTextWordLength;
}
/**
* This method will return PostgreSQL specific SQL necessary to find an exact phrase search.
*
* @param string $val The phrase or exact value to search for.
* @param bool $exact Whether this should be an exact match or not.
* @return string The SQL to perform the search.
*/
private function _sqlPhraseExactMatch(string $val, bool $exact = false): string
{
$ftVal = explode(' ', $val);
$ftVal = implode(' & ', $ftVal);
$likeVal = !$exact ? '%' . $val . '%' : $val;
return sprintf("%s @@ '%s'::tsquery AND %s LIKE '%s'", Craft::$app->getDb()->quoteColumnName('keywords_vector'), $ftVal, Craft::$app->getDb()->quoteColumnName('keywords'), $likeVal);
}
/**
* @param string $cleanKeywords The string of space separated search keywords.
* @param int $maxSize The maximum size the keywords string should be.
* @return string The (possibly) truncated keyword string.
*/
private function _truncateSearchIndexKeywords(string $cleanKeywords, int $maxSize): string
{
$cleanKeywordsLength = strlen($cleanKeywords);
// Give ourselves a little wiggle room.
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$maxSize = ceil($maxSize * 0.95);
if ($cleanKeywordsLength > $maxSize) {
// Time to truncate.
$cleanKeywords = mb_strcut($cleanKeywords, 0, $maxSize);
// Make sure we don't cut off a word in the middle.
if ($cleanKeywords[mb_strlen($cleanKeywords) - 1] !== ' ') {
$position = mb_strrpos($cleanKeywords, ' ');
if ($position) {
$cleanKeywords = mb_substr($cleanKeywords, 0, $position + 1);
}
}
}
return $cleanKeywords;
}
}