Skip to content

Commit

Permalink
Fix Format fields with underscore in Loupe Adapter (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
ker0x authored Mar 20, 2024
1 parent 01d3b24 commit c0b3df6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/seal-loupe-adapter/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"require": {
"php": "^8.1",
"schranz-search/seal": "^0.4",
"loupe/loupe": "^0.5",
"loupe/loupe": "^0.6",
"psr/container": "^1.0 || ^2.0"
},
"require-dev": {
Expand Down
16 changes: 12 additions & 4 deletions packages/seal-loupe-adapter/src/LoupeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
final class LoupeHelper
{
public const SEPERATOR = '_';
public const SEPARATOR = '_';
public const SOURCE_FIELD = 'l_source'; // fields are not allowed to begin with `_`

/**
Expand Down Expand Up @@ -98,6 +98,14 @@ public function reset(): void
$this->loupe = [];
}

/**
* @internal
*/
public function formatField(string $field): string
{
return \str_replace('.', self::SEPARATOR, $field);
}

private function createLoupe(Index $index, Configuration|null $configuration = null): Loupe
{
if (!$configuration instanceof Configuration) {
Expand Down Expand Up @@ -125,9 +133,9 @@ private function createConfiguration(Index $index): Configuration
{
return Configuration::create()
->withPrimaryKey($index->getIdentifierField()->name)
->withSearchableAttributes(\array_map(fn (string $field) => \str_replace('.', self::SEPERATOR, $field), $index->searchableFields))
->withFilterableAttributes(\array_map(fn (string $field) => \str_replace('.', self::SEPERATOR, $field), $index->filterableFields))
->withSortableAttributes(\array_map(fn (string $field) => \str_replace('.', self::SEPERATOR, $field), $index->sortableFields));
->withSearchableAttributes(\array_map(fn (string $field) => $this->formatField($field), $index->searchableFields))
->withFilterableAttributes(\array_map(fn (string $field) => $this->formatField($field), $index->filterableFields))
->withSortableAttributes(\array_map(fn (string $field) => $this->formatField($field), $index->sortableFields));
}

private function getIndexDirectory(Index $index): string
Expand Down
2 changes: 1 addition & 1 deletion packages/seal-loupe-adapter/src/LoupeIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(
) {
$this->marshaller = new FlattenMarshaller(
dateAsInteger: true,
separator: LoupeHelper::SEPERATOR,
separator: LoupeHelper::SEPARATOR,
sourceField: LoupeHelper::SOURCE_FIELD,
);
}
Expand Down
26 changes: 10 additions & 16 deletions packages/seal-loupe-adapter/src/LoupeSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(
) {
$this->marshaller = new FlattenMarshaller(
dateAsInteger: true,
separator: LoupeHelper::SEPERATOR,
separator: LoupeHelper::SEPARATOR,
sourceField: LoupeHelper::SOURCE_FIELD,
);
}
Expand Down Expand Up @@ -62,7 +62,7 @@ public function search(Search $search): Result
}

if (1 !== \count($search->indexes)) {
throw new \RuntimeException('Meilisearch does not yet support search multiple indexes: https://github.com/schranz-search/schranz-search/issues/28');
throw new \RuntimeException('Loupe does not yet support search across multiple indexes: https://github.com/schranz-search/schranz-search/issues/28');
}

$index = $search->indexes[\array_key_first($search->indexes)];
Expand All @@ -77,12 +77,12 @@ public function search(Search $search): Result
match (true) {
$filter instanceof Condition\IdentifierCondition => $filters[] = $index->getIdentifierField()->name . ' = ' . $this->escapeFilterValue($filter->identifier),
$filter instanceof Condition\SearchCondition => $query = $filter->query,
$filter instanceof Condition\EqualCondition => $filters[] = $filter->field . ' = ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\NotEqualCondition => $filters[] = $filter->field . ' != ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\GreaterThanCondition => $filters[] = $filter->field . ' > ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\GreaterThanEqualCondition => $filters[] = $filter->field . ' >= ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\LessThanCondition => $filters[] = $filter->field . ' < ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\LessThanEqualCondition => $filters[] = $filter->field . ' <= ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\EqualCondition => $filters[] = $this->loupeHelper->formatField($filter->field) . ' = ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\NotEqualCondition => $filters[] = $this->loupeHelper->formatField($filter->field) . ' != ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\GreaterThanCondition => $filters[] = $this->loupeHelper->formatField($filter->field) . ' > ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\GreaterThanEqualCondition => $filters[] = $this->loupeHelper->formatField($filter->field) . ' >= ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\LessThanCondition => $filters[] = $this->loupeHelper->formatField($filter->field) . ' < ' . $this->escapeFilterValue($filter->value),
$filter instanceof Condition\LessThanEqualCondition => $filters[] = $this->loupeHelper->formatField($filter->field) . ' <= ' . $this->escapeFilterValue($filter->value),
default => throw new \LogicException($filter::class . ' filter not implemented.'),
};
}
Expand All @@ -107,7 +107,7 @@ public function search(Search $search): Result

$sorts = [];
foreach ($search->sortBys as $field => $direction) {
$sorts[] = $field . ':' . $direction;
$sorts[] = $this->loupeHelper->formatField($field) . ':' . $direction;
}

if ([] !== $sorts) {
Expand All @@ -124,13 +124,7 @@ public function search(Search $search): Result

private function escapeFilterValue(string|int|float|bool $value): string
{
// TODO replace with SearchParameters::escapeFilterValue once updated Loupe to 0.5
// see also https://github.com/loupe-php/loupe/pull/54
return match (true) {
\is_bool($value) => $value ? '1' : '0',
\is_int($value), \is_float($value) => (string) $value,
default => "'" . \str_replace("'", "''", $value) . "'"
};
return SearchParameters::escapeFilterValue($value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function search(Search $search): Result
}

if (1 !== \count($search->indexes)) {
throw new \RuntimeException('Meilisearch does not yet support search multiple indexes: https://github.com/schranz-search/schranz-search/issues/28');
throw new \RuntimeException('Meilisearch does not yet support search across multiple indexes: https://github.com/schranz-search/schranz-search/issues/28');
}

$index = $search->indexes[\array_key_first($search->indexes)];
Expand Down

0 comments on commit c0b3df6

Please sign in to comment.