Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for using pagination with custom query in Scout Builder. #290

Merged
merged 6 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/ElasticSearch/SearchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Matchish\ScoutElasticSearch\ElasticSearch;

use Illuminate\Support\Arr;
use Laravel\Scout\Builder;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\Query\Compound\BoolQuery;
Expand All @@ -15,11 +16,12 @@ final class SearchFactory
{
/**
* @param Builder $builder
* @param array $options
* @param array $enforceOptions
* @return Search
*/
public static function create(Builder $builder, array $options = []): Search
public static function create(Builder $builder, array $enforceOptions = []): Search
{
$options = static::prepareOptions($builder, $enforceOptions);
$search = new Search();
$query = new QueryStringQuery($builder->query);
if (static::hasWhereFilters($builder)) {
Expand Down Expand Up @@ -135,4 +137,22 @@ private static function hasWhereNotIns($builder): bool
{
return isset($builder->whereNotIns) && ! empty($builder->whereNotIns);
}

private static function prepareOptions(Builder $builder, array $enforceOptions = []): array
{
$options = [];

if (isset($builder->limit)) {
$options['size'] = $builder->limit;
}

return array_merge($options, self::supportedOptions($builder), $enforceOptions);
}

private static function supportedOptions(Builder $builder): array
{
return Arr::only($builder->options, [
'from',
]);
}
}
78 changes: 78 additions & 0 deletions tests/Unit/ElasticSearch/SearchFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\ElasticSearch;

use App\Product;
use Laravel\Scout\Builder;
use Matchish\ScoutElasticSearch\ElasticSearch\SearchFactory;
use Tests\TestCase;

class SearchFactoryTest extends TestCase
{
public function test_limit_set_in_builder(): void
{
$builder = new Builder(new Product(), '*');
$builder->take($expectedSize = 50);

$search = SearchFactory::create($builder);

$this->assertEquals($expectedSize, $search->getSize());
}

public function test_limit_compatible_with_pagination(): void
{
$builder = new Builder(new Product(), '*');
$builder->take(30);

$search = SearchFactory::create($builder, [
'from' => 0,
'size' => $expectedSize = 50,
]);

$this->assertEquals($expectedSize, $search->getSize());
}

public function test_size_set_in_options_dont_take_effect(): void
{
$builder = new Builder(new Product(), '*');
$builder->take($expectedSize = 30)
->options([
'size' => 100,
]);

$search = SearchFactory::create($builder);

$this->assertEquals($expectedSize, $search->getSize());
}

public function test_from_set_in_options_take_effect(): void
{
$builder = new Builder(new Product(), '*');
$builder->options([
'from' => $expectedFrom = 100,
]);

$search = SearchFactory::create($builder);

$this->assertEquals($expectedFrom, $search->getFrom());
}

public function test_both_parameters_dont_take_effect_on_pagination(): void
{
$builder = new Builder(new Product(), '*');
$builder->options([
'from' => 250,
])
->take(30);

$search = SearchFactory::create($builder, [
'from' => $expectedFrom = 100,
'size' => $expectedSize = 50,
]);

$this->assertEquals($expectedSize, $search->getSize());
$this->assertEquals($expectedFrom, $search->getFrom());
}
}
Loading