Skip to content

Commit

Permalink
fix max results for for all data
Browse files Browse the repository at this point in the history
ft.search return 10 results by default
  • Loading branch information
Marc Lemay committed Sep 5, 2024
1 parent 651e5a9 commit ef73773
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Client/PredisClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,11 @@ public function count(string $prefixKey, array $criterias = []): int
foreach ($criterias as $property => $value) {
$arguments[] = "@$property:$value";
}
if ($criterias === []) {
$arguments[] = '*';
}

$rawResult = call_user_func_array([$this->redis, 'executeRaw'], $arguments);
$rawResult = call_user_func_array([$this->redis, 'executeRaw'], [$arguments]);

if (!$rawResult) {
$this->handleError(__METHOD__, $this->getLastError());
Expand Down
25 changes: 19 additions & 6 deletions src/Om/Repository/AbstractObjectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ abstract public function getPropertyValue($identifier, string $property): mixed;
*/
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = 0): array
{
$limit = $this->defineLimit($limit);
$this->convertDates($criteria);
$this->convertSpecial($criteria);
$data = $this->redisClient->search(
Expand All @@ -61,14 +62,15 @@ public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = nu
*/
public function findByLike(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = 0): array
{
$limit = $this->defineLimit($limit);
$this->convertDates($criteria);
$this->convertSpecial($criteria);
foreach ($criteria as $property => $value) {
$criteria[$property.'_text'] = "*$value*";
$criteria[$property . '_text'] = "*$value*";
unset($criteria[$property]);
}

$data = $this->redisClient->search(prefixKey: $this->prefix, search: $criteria, orderBy: $orderBy ?? [], format: $this->format, numberOfResults: $limit, offset: $offset, searchType: Property::INDEX_TEXT);
$data = $this->redisClient->search(prefixKey: $this->prefix, search: $criteria, orderBy: $orderBy ?? [], format: $this->format, numberOfResults: $limit, offset: $offset, searchType: Property::INDEX_TEXT);

$collection = [];
foreach ($data as $item) {
Expand All @@ -78,11 +80,20 @@ public function findByLike(array $criteria, ?array $orderBy = null, ?int $limit
return $collection;
}

private function defineLimit(?int $limit = null)
{
if ($limit === null) {
$limit = $this->count([]);
}
return $limit;
}
/**
* @inheritdoc
*/
public function findLike(string $search, ?int $limit = null): array
{
$limit = $this->defineLimit($limit);

$data = $this->redisClient->searchLike($this->prefix, $search, $this->format, $limit);

$collection = [];
Expand All @@ -98,7 +109,9 @@ public function findLike(string $search, ?int $limit = null): array
*/
public function findAll(): array
{
return $this->findBy([]);
$count = $this->count([]);

return $this->findBy([], limit: $count);
}

/**
Expand All @@ -125,11 +138,11 @@ public function findOneByLike(array $criteria, ?array $orderBy = null): ?object
$this->convertDates($criteria);
$this->convertSpecial($criteria);
foreach ($criteria as $property => $value) {
$criteria[$property.'_text'] = "*$value*";
$criteria[$property . '_text'] = "*$value*";
unset($criteria[$property]);
}

$data = $this->redisClient->search(prefixKey: $this->prefix, search: $criteria, orderBy: $orderBy ?? [], format: $this->format, numberOfResults: 1, searchType: Property::INDEX_TEXT);
$data = $this->redisClient->search(prefixKey: $this->prefix, search: $criteria, orderBy: $orderBy ?? [], format: $this->format, numberOfResults: 1, searchType: Property::INDEX_TEXT);

if ($data === []) {
return null;
Expand All @@ -152,7 +165,7 @@ public function count(array $criteria = []): int
public function createQueryBuilder(): QueryBuilder
{
return new QueryBuilder(
redisClient: $this->redisClient,
redisClient: $this->redisClient,
converter: $this->converter,
className: $this->className,
redisKey: $this->prefix,
Expand Down

0 comments on commit ef73773

Please sign in to comment.