Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Oct 16, 2024
1 parent 0a36b6d commit 2aec861
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
5 changes: 3 additions & 2 deletions packages/search/src/Data/SearchResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lunar\Search\Data;

use Illuminate\Contracts\Pagination\Paginator;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Attributes\MapName;
use Spatie\LaravelData\Data;
Expand All @@ -19,7 +20,7 @@ public function __construct(
#[DataCollectionOf(SearchHit::class)]
public array $hits,
#[DataCollectionOf(SearchFacet::class)]
public array $facets = [],
public array $links = [],
public array $facets,
public Paginator $paginator,
) {}
}
58 changes: 50 additions & 8 deletions packages/search/src/Engines/TypesenseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function get(): SearchResults
{
$paginator = $this->getRawResults(function (Documents $documents, string $query, array $options) {

$filters = collect();
$filters = collect($options['filter_by']);

foreach ($this->filters as $key => $value) {
$filters->push($key.':'.collect($value)->join(','));
Expand All @@ -39,7 +39,8 @@ public function get(): SearchResults
$options['facet_by'] = implode(',', array_keys($facets));
$options['max_facet_values'] = 50;
$options['per_page'] = $this->perPage;
$options['sort_by'] = $this->sort;

$options['sort_by'] = $this->sortByIsValid() ? $this->sort : '';

if ($filters->count()) {
$options['filter_by'] = $filters->join(' && ');
Expand Down Expand Up @@ -77,11 +78,17 @@ public function get(): SearchResults
foreach ($facets as $facet) {
$facetConfig = $this->getFacetConfig($facet->field);

foreach ($facet->values as $faceValue) {
if (empty($facetConfig[$faceValue->value])) {
foreach ($facet->values as $facetValue) {
$valueConfig = $facetConfig['values'][$facetValue->value] ?? null;

if (! $valueConfig) {
continue;
}
$faceValue->additional($facetConfig[$faceValue->value]);

$facetValue->value = $valueConfig['label'] ?? $facetValue->value;
unset($valueConfig['label']);

$facetValue->additional($valueConfig);
}
}

Expand All @@ -93,12 +100,47 @@ public function get(): SearchResults
'per_page' => $paginator->perPage(),
'hits' => $documents,
'facets' => $facets,
'links' => $paginator->appends([
'perPage' => $this->perPage,
'paginator' => $paginator->appends([
'facets' => http_build_query($this->facets),
])->linkCollection()->toArray(),
]),
];

return SearchResults::from($data);
}

protected function sortByIsValid(): bool
{
$sort = $this->sort;

if (! $sort) {
return true;
}

$parts = explode(':', $sort);

if (! isset($parts[1])) {
return false;
}

if (! in_array($parts[1], ['asc', 'desc'])) {
return false;
}

$config = $this->getFieldConfig();

if (empty($config)) {
return false;
}

$field = collect($config)->first(
fn ($field) => $field['name'] == $parts[0]
);

return $field && ($field['sort'] ?? false);
}

protected function getFieldConfig(): array
{
return config('scout.typesense.model-settings.'.$this->modelType.'.collection-schema.fields', []);
}
}

0 comments on commit 2aec861

Please sign in to comment.