Skip to content

Commit

Permalink
Implement multi search
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Oct 21, 2024
1 parent 637f857 commit 0b2594e
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Log;
use Laravel\Scout\EngineManager;
use Laravel\Scout\Scout;
use Lunar\Search\Data\SearchFacet;
use Lunar\Search\Data\SearchHit;
use Lunar\Search\Data\SearchResults;
Expand Down Expand Up @@ -69,23 +71,33 @@ protected function buildSearchOptions(array $options, string $query, $useFacetFi
$options['filter_by'] = $filters->join(' && ');
}

return $options;
return [
'collection' => 'voltimum_products',
...$options
];
}

public function get(): SearchResults
{
try {
$preResults = $this->getRawResults(function (Documents $documents, string $query, array $options) {
return $documents->search(
$this->buildSearchOptions($options, $query, useFacetFilters: false)
);
});

$paginator = $this->getRawResults(function (Documents $documents, string $query, array $options) {
return $documents->search(
$this->buildSearchOptions($options, $query),
);
$engine = app(EngineManager::class)->engine('typesense');

$searchRequests = [
'searches' => [
$this->buildSearchOptions($options, $query),
$this->buildSearchOptions($options, $query, useFacetFilters: false)
]
];

$response = $engine->getMultiSearch()->perform($searchRequests);

return [
...$response['results'][0],
'unfaceted_response' => $response['results'][1],
];
});

} catch (\GuzzleHttp\Exception\ConnectException|ServiceUnavailable $e) {
Log::error($e->getMessage());
$paginator = new LengthAwarePaginator(
Expand Down Expand Up @@ -142,7 +154,7 @@ public function get(): SearchResults
// }


$facets = collect($preResults['facet_counts'] ?? [])->map(
$facets = collect($paginator['unfaceted_response']['facet_counts'] ?? [])->map(
fn ($facet) => SearchFacet::from([
'label' => $this->getFacetConfig($facet['field_name'])['label'] ?? '',
'field' => $facet['field_name'],
Expand Down

0 comments on commit 0b2594e

Please sign in to comment.