|
4 | 4 |
|
5 | 5 | use Closure;
|
6 | 6 | use Exception;
|
| 7 | +use Foolz\SphinxQL\Drivers\ConnectionInterface; |
7 | 8 | use Foolz\SphinxQL\Exception\ConnectionException;
|
8 | 9 | use Foolz\SphinxQL\Exception\SphinxQLException;
|
9 | 10 | use PDO;
|
@@ -317,7 +318,7 @@ public function checkIndexExist(string $sphinx_index)
|
317 | 318 | */
|
318 | 319 | private static $spql_connection_port;
|
319 | 320 | /**
|
320 |
| - * @var Connection |
| 321 | + * @var ConnectionInterface |
321 | 322 | */
|
322 | 323 | private static $spql_connection;
|
323 | 324 |
|
@@ -523,6 +524,79 @@ public static function rt_RebuildAbstractIndex(PDO $pdo_connection, string $sql_
|
523 | 524 | return $total_updated;
|
524 | 525 | }
|
525 | 526 |
|
| 527 | + |
| 528 | + /** |
| 529 | + * |
| 530 | + * |
| 531 | + * @param string $search_query |
| 532 | + * @param string $source_index |
| 533 | + * @param string $sort_field |
| 534 | + * @param string $sort_order |
| 535 | + * @param int $limit |
| 536 | + * @param array $option_weight |
| 537 | + * @return array |
| 538 | + * |
| 539 | + * @throws ConnectionException |
| 540 | + * @throws DatabaseException |
| 541 | + * @throws SphinxQLException |
| 542 | + */ |
| 543 | + public static function spql_getDataSet(string $search_query, string $source_index, string $sort_field, string $sort_order = 'DESC', int $limit = 5, array $option_weight = []): array |
| 544 | + { |
| 545 | + $found_dataset = []; |
| 546 | + $compiled_request = ''; |
| 547 | + |
| 548 | + if (empty($source_index)) return $found_dataset; |
| 549 | + |
| 550 | + try { |
| 551 | + $search_request = self::createInstance() |
| 552 | + ->select() |
| 553 | + ->from($source_index); |
| 554 | + |
| 555 | + if (!empty($sort_field)) { |
| 556 | + $search_request = $search_request |
| 557 | + ->orderBy($sort_field, $sort_order); |
| 558 | + } |
| 559 | + |
| 560 | + if (!empty($option_weight)) { |
| 561 | + $search_request = $search_request |
| 562 | + ->option('field_weights', $option_weight); |
| 563 | + } |
| 564 | + |
| 565 | + if (!is_null($limit) && is_numeric($limit)) { |
| 566 | + $search_request = $search_request |
| 567 | + ->limit($limit); |
| 568 | + } |
| 569 | + |
| 570 | + if (strlen($search_query) > 0) { |
| 571 | + $search_request = $search_request |
| 572 | + ->match(['title'], $search_query); |
| 573 | + } |
| 574 | + |
| 575 | + $search_result = $search_request->execute(); |
| 576 | + |
| 577 | + while ($row = $search_result->fetchAssoc()) { |
| 578 | + $found_dataset[] = $row['id']; |
| 579 | + } |
| 580 | + |
| 581 | + } catch (Exception $e) { |
| 582 | + |
| 583 | + $meta = SphinxToolkitHelper::showMeta(self::$spql_connection); |
| 584 | + |
| 585 | + self::$spql_logger->error( |
| 586 | + __CLASS__ . '/' . __METHOD__ . |
| 587 | + " Error fetching data from `{$source_index}` : " . $e->getMessage(), |
| 588 | + [ |
| 589 | + $e->getCode(), |
| 590 | + htmlspecialchars(urldecode($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])), |
| 591 | + $search_request->getCompiled(), |
| 592 | + $meta |
| 593 | + ] |
| 594 | + ); |
| 595 | + } |
| 596 | + return $found_dataset; |
| 597 | + } // get_IDs_DataSet() |
| 598 | + |
| 599 | + |
526 | 600 | /**
|
527 | 601 | *
|
528 | 602 | * @param string $index_name
|
|
0 commit comments