-
Notifications
You must be signed in to change notification settings - Fork 2
/
SearchEntryPoint.php
142 lines (119 loc) · 3.9 KB
/
SearchEntryPoint.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
<?php
declare(strict_types=1);
namespace MeiliSearchBundle\Search;
use MeiliSearchBundle\Index\IndexOrchestratorInterface;
use MeiliSearchBundle\Event\PostSearchEvent;
use MeiliSearchBundle\Event\PreSearchEvent;
use MeiliSearchBundle\Exception\RuntimeException;
use MeiliSearchBundle\Result\ResultBuilderInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Throwable;
use function sprintf;
/**
* @author Guillaume Loulier <[email protected]>
*/
final class SearchEntryPoint implements SearchEntryPointInterface
{
private const INDEX = 'index';
private const ERROR = 'error';
private const HITS = 'hits';
private const QUERY = 'query';
private const OPTIONS = 'options';
/**
* @var EventDispatcherInterface|null
*/
private $eventDispatcher;
/**
* @var IndexOrchestratorInterface
*/
private $indexOrchestrator;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var ResultBuilderInterface|null
*/
private $resultBuilder;
/**
* @var string|null
*/
private $prefix;
public function __construct(
IndexOrchestratorInterface $indexOrchestrator,
?ResultBuilderInterface $resultBuilder = null,
?EventDispatcherInterface $eventDispatcher = null,
?LoggerInterface $logger = null,
?string $prefix = null
) {
$this->indexOrchestrator = $indexOrchestrator;
$this->resultBuilder = $resultBuilder;
$this->eventDispatcher = $eventDispatcher;
$this->logger = $logger ?: new NullLogger();
$this->prefix = $prefix;
}
/**
* {@inheritdoc}
*/
public function search(string $index, string $query, array $options = []): SearchResultInterface
{
try {
$index = $this->indexOrchestrator->getIndex(null === $this->prefix ? $index : sprintf('%s%s', $this->prefix, $index));
} catch (Throwable $throwable) {
$this->logger->error('The search cannot occur as an error occurred when fetching the index', [
self::INDEX => $index,
self::ERROR => $throwable->getMessage(),
]);
throw $throwable;
}
$this->dispatch(new PreSearchEvent([
self::INDEX => $index,
self::QUERY => $query,
self::OPTIONS => $options,
]));
$this->logger->info('A query has been made', [
self::INDEX => $index,
self::QUERY => $query,
]);
try {
$result = $index->search($query, $options);
} catch (Throwable $throwable) {
$this->logger->error('The query has failed', [
self::ERROR => $throwable->getMessage(),
self::QUERY => $query,
self::OPTIONS => $options,
]);
throw new RuntimeException($throwable->getMessage(), 0, $throwable);
}
$this->buildModels($result[self::HITS]);
$searchResult = SearchResult::create(
$result[self::HITS],
$result['offset'],
$result['limit'],
$result['nbHits'],
$result['exhaustiveNbHits'],
$result['processingTimeMs'],
$result[self::QUERY]
);
$this->dispatch(new PostSearchEvent($searchResult));
return $searchResult;
}
private function buildModels(array &$results): void
{
foreach ($results as $key => $hit) {
if (null !== $this->resultBuilder && $this->resultBuilder->support($hit)) {
$results[$key] = $this->resultBuilder->build($hit);
}
}
}
private function dispatch(Event $event): void
{
if (null === $this->eventDispatcher) {
return;
}
$this->eventDispatcher->dispatch($event);
}
}