Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit db00cb8

Browse files
1.2
* [*] TRUNCATE RTINDEX WITH RECONFIGURE
1 parent cf0fbce commit db00cb8

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

TODO.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# getDataSetFromSphinx
2+
3+
Использовалось в SteamboatEngine/functions.php в версии 1.26.1 (latest implementation), предполагался перенос в SphinxToolkit,
4+
но не сделан (т.к. есть зависимость от AppLogger)
5+
6+
Как временное решение, функция задается индивидуально для каждого проекта.
7+
8+
```php
9+
/**
10+
* Загружает список айдишников из сфинкс-индекса по переданному запросу.
11+
*
12+
* Old implementation is `\SteamBoat\SBSearch::get_IDs_DataSet`
13+
*
14+
* @param string $search_query - строка запроса
15+
* @param string $source_index - имя индекса
16+
* @param string $sort_field - поле сортировки
17+
* @param string $sort_order - условие сортировки
18+
* @param int $limit - количество
19+
* @param array $option_weight - опции "веса"
20+
* @return array - список айдишников
21+
*/
22+
function getDataSetFromSphinx(string $search_query, string $source_index, string $sort_field, string $sort_order = 'DESC', int $limit = 5, array $option_weight = []): array
23+
{
24+
$found_dataset = [];
25+
$compiled_request = '';
26+
if (empty($source_index)) return $found_dataset;
27+
try {
28+
$search_request = \Arris\Toolkit\SphinxToolkit::createInstance()
29+
->select()
30+
->from($source_index);
31+
if (!empty($sort_field)) {
32+
$search_request = $search_request
33+
->orderBy($sort_field, $sort_order);
34+
}
35+
if (!empty($option_weight)) {
36+
$search_request = $search_request
37+
->option('field_weights', $option_weight);
38+
}
39+
if (!is_null($limit) && is_numeric($limit)) {
40+
$search_request = $search_request
41+
->limit($limit);
42+
}
43+
if (strlen($search_query) > 0) {
44+
$search_request = $search_request
45+
->match(['title'], $search_query);
46+
}
47+
$search_result = $search_request->execute();
48+
while ($row = $search_result->fetchAssoc()) {
49+
$found_dataset[] = $row['id'];
50+
}
51+
} catch (Exception $e) {
52+
\Arris\AppLogger::scope('sphinx')->error(
53+
__CLASS__ . '/' . __METHOD__ .
54+
" Error fetching data from `{$source_index}` : " . $e->getMessage(),
55+
[
56+
htmlspecialchars(urldecode($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])),
57+
$search_request->getCompiled(),
58+
$e->getCode()
59+
]
60+
);
61+
}
62+
return $found_dataset;
63+
} // get_IDs_DataSet()
64+
```

src/SphinxToolkit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function rebuildAbstractIndex(string $mysql_table, string $sphinx_index,
8080
$chunk_size = $this->rai_options['chunk_length'];
8181

8282
// truncate
83-
$sphinx_connection->query("TRUNCATE RTINDEX {$sphinx_index} ");
83+
$sphinx_connection->query("TRUNCATE RTINDEX {$sphinx_index} WITH RECONFIGURE");
8484

8585
// get total count
8686
$total_count = $this->mysql_GetRowCount($mysql_connection, $mysql_table, $condition);
@@ -156,7 +156,7 @@ public function rebuildAbstractIndexMVA(string $mysql_table, string $sphinx_inde
156156
throw new Exception("`{$sphinx_index}` not present", 1);
157157

158158
// truncate
159-
$sphinx_connection->query("TRUNCATE RTINDEX {$sphinx_index} ");
159+
$sphinx_connection->query("TRUNCATE RTINDEX {$sphinx_index} WITH RECONFIGURE");
160160

161161
// get total count
162162
$total_count = $this->mysql_GetRowCount($mysql_connection, $mysql_table, $condition);

traits/SphinxToolkitHelper.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ public static function mb_str_replace($search, $replace, $subject, &$count = 0)
8585
if (is_array($subject)) {
8686
// call mb_replace for each single string in $subject
8787
foreach ($subject as &$string) {
88-
$string = \Arris\mb_str_replace($search, $replace, $string, $c);
88+
$string = SphinxToolkitHelper::mb_str_replace($search, $replace, $string, $c);
8989
$count += $c;
9090
}
9191
} elseif (is_array($search)) {
9292
if (!is_array($replace)) {
9393
foreach ($search as &$string) {
94-
$subject = \Arris\mb_str_replace($string, $replace, $subject, $c);
94+
$subject = SphinxToolkitHelper::mb_str_replace($string, $replace, $subject, $c);
9595
$count += $c;
9696
}
9797
} else {
9898
$n = max(count($search), count($replace));
9999
while ($n--) {
100-
$subject = \Arris\mb_str_replace(current($search), current($replace), $subject, $c);
100+
$subject = SphinxToolkitHelper::mb_str_replace(current($search), current($replace), $subject, $c);
101101
$count += $c;
102102
next($search);
103103
next($replace);
@@ -109,7 +109,6 @@ public static function mb_str_replace($search, $replace, $subject, &$count = 0)
109109
$subject = implode($replace, $parts);
110110
}
111111
return $subject;
112-
113112
}
114113

115114
}

0 commit comments

Comments
 (0)