Skip to content

Commit cfebb45

Browse files
[10.x] afterRawSearch callback (#904)
* afterRawSearchCallback * wip * wip * wip * wip * wip * wip * afterRawSearch on get/cursor methods * wip * wip * wip * wip * wip * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 21a3cd7 commit cfebb45

File tree

6 files changed

+289
-6
lines changed

6 files changed

+289
-6
lines changed

src/Builder.php

+43-4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ class Builder
4747
*/
4848
public $queryCallback;
4949

50+
/**
51+
* Optional callback after raw search.
52+
*
53+
* @var \Closure|null
54+
*/
55+
public $afterRawSearchCallback;
56+
5057
/**
5158
* The custom index specified for the search.
5259
*
@@ -310,6 +317,19 @@ public function raw()
310317
return $this->engine()->search($this);
311318
}
312319

320+
/**
321+
* Set the callback that should have an opportunity to inspect and modify the raw result returned by the search engine.
322+
*
323+
* @param callable $callback
324+
* @return $this
325+
*/
326+
public function withRawResults($callback)
327+
{
328+
$this->afterRawSearchCallback = $callback;
329+
330+
return $this;
331+
}
332+
313333
/**
314334
* Get the keys of search results.
315335
*
@@ -373,7 +393,9 @@ public function simplePaginate($perPage = null, $pageName = 'page', $page = null
373393
$perPage = $perPage ?: $this->model->getPerPage();
374394

375395
$results = $this->model->newCollection($engine->map(
376-
$this, $rawResults = $engine->paginate($this, $perPage, $page), $this->model
396+
$this,
397+
$this->applyAfterRawSearchCallback($rawResults = $engine->paginate($this, $perPage, $page)),
398+
$this->model
377399
)->all());
378400

379401
$paginator = Container::getInstance()->makeWith(Paginator::class, [
@@ -411,7 +433,7 @@ public function simplePaginateRaw($perPage = null, $pageName = 'page', $page = n
411433

412434
$perPage = $perPage ?: $this->model->getPerPage();
413435

414-
$results = $engine->paginate($this, $perPage, $page);
436+
$results = $this->applyAfterRawSearchCallback($engine->paginate($this, $perPage, $page));
415437

416438
$paginator = Container::getInstance()->makeWith(Paginator::class, [
417439
'items' => $results,
@@ -449,7 +471,9 @@ public function paginate($perPage = null, $pageName = 'page', $page = null)
449471
$perPage = $perPage ?: $this->model->getPerPage();
450472

451473
$results = $this->model->newCollection($engine->map(
452-
$this, $rawResults = $engine->paginate($this, $perPage, $page), $this->model
474+
$this,
475+
$this->applyAfterRawSearchCallback($rawResults = $engine->paginate($this, $perPage, $page)),
476+
$this->model
453477
)->all());
454478

455479
return Container::getInstance()->makeWith(LengthAwarePaginator::class, [
@@ -486,7 +510,7 @@ public function paginateRaw($perPage = null, $pageName = 'page', $page = null)
486510

487511
$perPage = $perPage ?: $this->model->getPerPage();
488512

489-
$results = $engine->paginate($this, $perPage, $page);
513+
$results = $this->applyAfterRawSearchCallback($engine->paginate($this, $perPage, $page));
490514

491515
return Container::getInstance()->makeWith(LengthAwarePaginator::class, [
492516
'items' => $results,
@@ -531,6 +555,21 @@ protected function getTotalCount($results)
531555
)->toBase()->getCountForPagination();
532556
}
533557

558+
/**
559+
* Invoke the "after raw search" callback.
560+
*
561+
* @param mixed $results
562+
* @return mixed
563+
*/
564+
public function applyAfterRawSearchCallback($results)
565+
{
566+
if ($this->afterRawSearchCallback) {
567+
call_user_func($this->afterRawSearchCallback, $results);
568+
}
569+
570+
return $results;
571+
}
572+
534573
/**
535574
* Get the engine that should handle the query.
536575
*

src/Engines/Engine.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ public function keys(Builder $builder)
133133
public function get(Builder $builder)
134134
{
135135
return $this->map(
136-
$builder, $this->search($builder), $builder->model
136+
$builder,
137+
$builder->applyAfterRawSearchCallback($this->search($builder)),
138+
$builder->model
137139
);
138140
}
139141

@@ -146,7 +148,9 @@ public function get(Builder $builder)
146148
public function cursor(Builder $builder)
147149
{
148150
return $this->lazyMap(
149-
$builder, $this->search($builder), $builder->model
151+
$builder,
152+
$builder->applyAfterRawSearchCallback($this->search($builder)),
153+
$builder->model
150154
);
151155
}
152156
}

tests/Integration/AlgoliaSearchableTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,60 @@ public function test_it_can_use_paginated_search_with_query_callback()
161161
], $page2->pluck('name', 'id')->all());
162162
}
163163

164+
public function test_it_can_use_paginated_search_with_after_raw_search_callback()
165+
{
166+
$rawResults = $this->itCanAccessRawSearchResultsOfPaginateUsingAfterRawSearchCallback();
167+
168+
$this->assertIsArray($rawResults);
169+
$this->assertArrayHasKey('hits', $rawResults);
170+
$this->assertArrayHasKey('processingTimeMS', $rawResults);
171+
}
172+
173+
public function test_it_can_use_raw_paginated_search_with_after_raw_search_callback()
174+
{
175+
$rawResults = $this->itCanAccessRawSearchResultsOfPaginateRawUsingAfterRawSearchCallback();
176+
177+
$this->assertIsArray($rawResults);
178+
$this->assertArrayHasKey('hits', $rawResults);
179+
$this->assertArrayHasKey('processingTimeMS', $rawResults);
180+
}
181+
182+
public function test_it_can_use_simple_paginated_search_with_after_raw_search_callback()
183+
{
184+
$rawResults = $this->itCanAccessRawSearchResultsOfSimplePaginateUsingAfterRawSearchCallback();
185+
186+
$this->assertIsArray($rawResults);
187+
$this->assertArrayHasKey('hits', $rawResults);
188+
$this->assertArrayHasKey('processingTimeMS', $rawResults);
189+
}
190+
191+
public function test_it_can_use_raw_simple_paginated_search_with_after_raw_search_callback()
192+
{
193+
$rawResults = $this->itCanAccessRawSearchResultsOfSimplePaginateRawUsingAfterRawSearchCallback();
194+
195+
$this->assertIsArray($rawResults);
196+
$this->assertArrayHasKey('hits', $rawResults);
197+
$this->assertArrayHasKey('processingTimeMS', $rawResults);
198+
}
199+
200+
public function test_it_can_use_raw_get_search_with_after_raw_search_callback()
201+
{
202+
$rawResults = $this->itCanAccessRawSearchResultsOfGetUsingAfterRawSearchCallback();
203+
204+
$this->assertIsArray($rawResults);
205+
$this->assertArrayHasKey('hits', $rawResults);
206+
$this->assertArrayHasKey('processingTimeMS', $rawResults);
207+
}
208+
209+
public function test_it_can_use_raw_cursor_search_with_after_raw_search_callback()
210+
{
211+
$rawResults = $this->itCanAccessRawSearchResultsOfCursorUsingAfterRawSearchCallback();
212+
213+
$this->assertIsArray($rawResults);
214+
$this->assertArrayHasKey('hits', $rawResults);
215+
$this->assertArrayHasKey('processingTimeMS', $rawResults);
216+
}
217+
164218
protected static function scoutDriver(): string
165219
{
166220
return 'algolia';

tests/Integration/MeilisearchSearchableTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,60 @@ public function test_it_can_use_paginated_search_with_query_callback()
185185
], $page2->pluck('name', 'id')->all());
186186
}
187187

188+
public function test_it_can_use_paginated_search_with_after_raw_search_callback()
189+
{
190+
$rawResults = $this->itCanAccessRawSearchResultsOfPaginateUsingAfterRawSearchCallback();
191+
192+
$this->assertIsArray($rawResults);
193+
$this->assertArrayHasKey('hits', $rawResults);
194+
$this->assertArrayHasKey('processingTimeMs', $rawResults);
195+
}
196+
197+
public function test_it_can_use_raw_paginated_search_with_after_raw_search_callback()
198+
{
199+
$rawResults = $this->itCanAccessRawSearchResultsOfPaginateRawUsingAfterRawSearchCallback();
200+
201+
$this->assertIsArray($rawResults);
202+
$this->assertArrayHasKey('hits', $rawResults);
203+
$this->assertArrayHasKey('processingTimeMs', $rawResults);
204+
}
205+
206+
public function test_it_can_use_simple_paginated_search_with_after_raw_search_callback()
207+
{
208+
$rawResults = $this->itCanAccessRawSearchResultsOfSimplePaginateUsingAfterRawSearchCallback();
209+
210+
$this->assertIsArray($rawResults);
211+
$this->assertArrayHasKey('hits', $rawResults);
212+
$this->assertArrayHasKey('processingTimeMs', $rawResults);
213+
}
214+
215+
public function test_it_can_use_raw_simple_paginated_search_with_after_raw_search_callback()
216+
{
217+
$rawResults = $this->itCanAccessRawSearchResultsOfSimplePaginateRawUsingAfterRawSearchCallback();
218+
219+
$this->assertIsArray($rawResults);
220+
$this->assertArrayHasKey('hits', $rawResults);
221+
$this->assertArrayHasKey('processingTimeMs', $rawResults);
222+
}
223+
224+
public function test_it_can_use_raw_get_search_with_after_raw_search_callback()
225+
{
226+
$rawResults = $this->itCanAccessRawSearchResultsOfGetUsingAfterRawSearchCallback();
227+
228+
$this->assertIsArray($rawResults);
229+
$this->assertArrayHasKey('hits', $rawResults);
230+
$this->assertArrayHasKey('processingTimeMs', $rawResults);
231+
}
232+
233+
public function test_it_can_use_raw_cursor_search_with_after_raw_search_callback()
234+
{
235+
$rawResults = $this->itCanAccessRawSearchResultsOfCursorUsingAfterRawSearchCallback();
236+
237+
$this->assertIsArray($rawResults);
238+
$this->assertArrayHasKey('hits', $rawResults);
239+
$this->assertArrayHasKey('processingTimeMs', $rawResults);
240+
}
241+
188242
protected static function scoutDriver(): string
189243
{
190244
return 'meilisearch';

tests/Integration/SearchableTests.php

+78
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,82 @@ protected function itCanUsePaginatedSearchWithEmptyQueryCallback()
120120

121121
return SearchableUser::search('*')->query($queryCallback)->paginate();
122122
}
123+
124+
protected function itCanAccessRawSearchResultsOfPaginateUsingAfterRawSearchCallback()
125+
{
126+
$result = null;
127+
128+
SearchableUser::search('*')
129+
->withRawResults(function ($rawSearchResult) use (&$result) {
130+
$result = $rawSearchResult;
131+
})
132+
->paginate();
133+
134+
return $result;
135+
}
136+
137+
protected function itCanAccessRawSearchResultsOfPaginateRawUsingAfterRawSearchCallback()
138+
{
139+
$result = null;
140+
141+
SearchableUser::search('*')
142+
->withRawResults(function ($rawSearchResult) use (&$result) {
143+
$result = $rawSearchResult;
144+
})
145+
->paginateRaw();
146+
147+
return $result;
148+
}
149+
150+
protected function itCanAccessRawSearchResultsOfSimplePaginateUsingAfterRawSearchCallback()
151+
{
152+
$result = null;
153+
154+
SearchableUser::search('*')
155+
->withRawResults(function ($rawSearchResult) use (&$result) {
156+
$result = $rawSearchResult;
157+
})
158+
->simplePaginate();
159+
160+
return $result;
161+
}
162+
163+
protected function itCanAccessRawSearchResultsOfSimplePaginateRawUsingAfterRawSearchCallback()
164+
{
165+
$result = null;
166+
167+
SearchableUser::search('*')
168+
->withRawResults(function ($rawSearchResult) use (&$result) {
169+
$result = $rawSearchResult;
170+
})
171+
->simplePaginateRaw();
172+
173+
return $result;
174+
}
175+
176+
protected function itCanAccessRawSearchResultsOfGetUsingAfterRawSearchCallback()
177+
{
178+
$result = null;
179+
180+
SearchableUser::search('*')
181+
->withRawResults(function ($rawSearchResult) use (&$result) {
182+
$result = $rawSearchResult;
183+
})
184+
->get();
185+
186+
return $result;
187+
}
188+
189+
protected function itCanAccessRawSearchResultsOfCursorUsingAfterRawSearchCallback()
190+
{
191+
$result = null;
192+
193+
SearchableUser::search('*')
194+
->withRawResults(function ($rawSearchResult) use (&$result) {
195+
$result = $rawSearchResult;
196+
})
197+
->cursor();
198+
199+
return $result;
200+
}
123201
}

tests/Integration/TypesenseSearchableTest.php

+54
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,60 @@ public function test_it_can_usePaginatedSearchWithEmptyQueryCallback()
169169
$this->assertSame($res->lastPage(), 3);
170170
}
171171

172+
public function test_it_can_use_paginated_search_with_after_raw_search_callback()
173+
{
174+
$rawResults = $this->itCanAccessRawSearchResultsOfPaginateUsingAfterRawSearchCallback();
175+
176+
$this->assertIsArray($rawResults);
177+
$this->assertArrayHasKey('hits', $rawResults);
178+
$this->assertArrayHasKey('search_time_ms', $rawResults);
179+
}
180+
181+
public function test_it_can_use_raw_paginated_search_with_after_raw_search_callback()
182+
{
183+
$rawResults = $this->itCanAccessRawSearchResultsOfPaginateRawUsingAfterRawSearchCallback();
184+
185+
$this->assertIsArray($rawResults);
186+
$this->assertArrayHasKey('hits', $rawResults);
187+
$this->assertArrayHasKey('search_time_ms', $rawResults);
188+
}
189+
190+
public function test_it_can_use_simple_paginated_search_with_after_raw_search_callback()
191+
{
192+
$rawResults = $this->itCanAccessRawSearchResultsOfSimplePaginateUsingAfterRawSearchCallback();
193+
194+
$this->assertIsArray($rawResults);
195+
$this->assertArrayHasKey('hits', $rawResults);
196+
$this->assertArrayHasKey('search_time_ms', $rawResults);
197+
}
198+
199+
public function test_it_can_use_raw_simple_paginated_search_with_after_raw_search_callback()
200+
{
201+
$rawResults = $this->itCanAccessRawSearchResultsOfSimplePaginateRawUsingAfterRawSearchCallback();
202+
203+
$this->assertIsArray($rawResults);
204+
$this->assertArrayHasKey('hits', $rawResults);
205+
$this->assertArrayHasKey('search_time_ms', $rawResults);
206+
}
207+
208+
public function test_it_can_use_raw_get_search_with_after_raw_search_callback()
209+
{
210+
$rawResults = $this->itCanAccessRawSearchResultsOfGetUsingAfterRawSearchCallback();
211+
212+
$this->assertIsArray($rawResults);
213+
$this->assertArrayHasKey('hits', $rawResults);
214+
$this->assertArrayHasKey('search_time_ms', $rawResults);
215+
}
216+
217+
public function test_it_can_use_raw_cursor_search_with_after_raw_search_callback()
218+
{
219+
$rawResults = $this->itCanAccessRawSearchResultsOfCursorUsingAfterRawSearchCallback();
220+
221+
$this->assertIsArray($rawResults);
222+
$this->assertArrayHasKey('hits', $rawResults);
223+
$this->assertArrayHasKey('search_time_ms', $rawResults);
224+
}
225+
172226
protected static function scoutDriver(): string
173227
{
174228
return 'typesense';

0 commit comments

Comments
 (0)