-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathSearchResult.php
255 lines (213 loc) · 6.12 KB
/
SearchResult.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
<?php
declare(strict_types=1);
namespace Meilisearch\Search;
/**
* @implements \IteratorAggregate<array<int, array<string, mixed>>>
*/
class SearchResult implements \Countable, \IteratorAggregate
{
/**
* @var array<int, array<string, mixed>>
*/
private array $hits;
/**
* `estimatedTotalHits` is the attributes returned by the Meilisearch server
* and its value will not be modified by the methods in this class.
* Please, use `hitsCount` if you want to know the real size of the `hits` array at any time.
*/
private ?int $estimatedTotalHits = null;
private int $hitsCount;
private ?int $offset = null;
private ?int $limit = null;
private int $semanticHitCount;
private ?int $hitsPerPage = null;
private ?int $page = null;
private ?int $totalPages = null;
private ?int $totalHits = null;
private int $processingTimeMs;
private bool $numberedPagination;
private string $query;
/**
* @var array<string, mixed>
*/
private array $facetDistribution;
/**
* @var array<string, mixed>
*/
private array $facetStats;
/**
* @var array<string, mixed>
*/
private array $raw;
public function __construct(array $body)
{
if (isset($body['estimatedTotalHits'])) {
$this->numberedPagination = false;
$this->offset = $body['offset'];
$this->limit = $body['limit'];
$this->estimatedTotalHits = $body['estimatedTotalHits'];
} else {
$this->numberedPagination = true;
$this->hitsPerPage = $body['hitsPerPage'];
$this->page = $body['page'];
$this->totalPages = $body['totalPages'];
$this->totalHits = $body['totalHits'];
}
$this->semanticHitCount = $body['semanticHitCount'] ?? 0;
$this->hits = $body['hits'] ?? [];
$this->hitsCount = \count($body['hits']);
$this->processingTimeMs = $body['processingTimeMs'];
$this->query = $body['query'];
$this->facetDistribution = $body['facetDistribution'] ?? [];
$this->facetStats = $body['facetStats'] ?? [];
$this->raw = $body;
}
/**
* Return a new {@see SearchResult} instance.
*
* The $options parameter is an array, and the following keys are accepted:
* - transformFacetDistribution (callable)
* - transformHits (callable)
*
* The method does NOT trigger a new search.
*/
public function applyOptions($options): self
{
if (\array_key_exists('transformHits', $options) && \is_callable($options['transformHits'])) {
$this->transformHits($options['transformHits']);
}
if (\array_key_exists('transformFacetDistribution', $options) && \is_callable($options['transformFacetDistribution'])) {
$this->transformFacetDistribution($options['transformFacetDistribution']);
}
return $this;
}
public function transformHits(callable $callback): self
{
$this->hits = $callback($this->hits);
$this->hitsCount = \count($this->hits);
return $this;
}
public function transformFacetDistribution(callable $callback): self
{
$this->facetDistribution = $callback($this->facetDistribution);
return $this;
}
public function getHit(int $key, $default = null)
{
return $this->hits[$key] ?? $default;
}
/**
* @return array<int, array>
*/
public function getHits(): array
{
return $this->hits;
}
public function getOffset(): ?int
{
return $this->offset;
}
public function getLimit(): ?int
{
return $this->limit;
}
public function getHitsCount(): int
{
return $this->hitsCount;
}
/**
* @return non-negative-int
*/
public function getSemanticHitCount(): int
{
return $this->semanticHitCount;
}
public function count(): int
{
return $this->hitsCount;
}
public function getEstimatedTotalHits(): ?int
{
return $this->estimatedTotalHits;
}
public function getProcessingTimeMs(): int
{
return $this->processingTimeMs;
}
public function getQuery(): string
{
return $this->query;
}
public function getHitsPerPage(): ?int
{
return $this->hitsPerPage;
}
public function getPage(): ?int
{
return $this->page;
}
public function getTotalPages(): ?int
{
return $this->totalPages;
}
public function getTotalHits(): ?int
{
return $this->totalHits;
}
/**
* @return array<string, mixed>
*/
public function getFacetDistribution(): array
{
return $this->facetDistribution;
}
/**
* @return array<string, mixed>
*/
public function getFacetStats(): array
{
return $this->facetStats;
}
/**
* Return the original search result.
*
* @return array<string, mixed>
*/
public function getRaw(): array
{
return $this->raw;
}
public function toArray(): array
{
$arr = [
'hits' => $this->hits,
'hitsCount' => $this->hitsCount,
'processingTimeMs' => $this->processingTimeMs,
'query' => $this->query,
'facetDistribution' => $this->facetDistribution,
];
if (!$this->numberedPagination) {
$arr = array_merge($arr, [
'offset' => $this->offset,
'limit' => $this->limit,
'estimatedTotalHits' => $this->estimatedTotalHits,
]);
} else {
$arr = array_merge($arr, [
'hitsPerPage' => $this->hitsPerPage,
'page' => $this->page,
'totalPages' => $this->totalPages,
'totalHits' => $this->totalHits,
]);
}
return $arr;
}
public function toJSON(): string
{
return json_encode($this->toArray(), JSON_PRETTY_PRINT);
}
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->hits);
}
}