Skip to content

Commit

Permalink
Allow setting source (fields) from options
Browse files Browse the repository at this point in the history
  • Loading branch information
hulkur committed Dec 18, 2024
1 parent 030a394 commit 1a4de24
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/ElasticSearch/SearchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public static function create(Builder $builder, array $enforceOptions = []): Sea
if (array_key_exists('size', $options)) {
$search->setSize($options['size']);
}
if (array_key_exists('source', $options)) {
$search->setSource($options['source']);
}
if (! empty($builder->orders)) {
foreach ($builder->orders as $order) {
$search->addSort(new FieldSort($order['column'], $order['direction']));
Expand Down Expand Up @@ -153,6 +156,7 @@ private static function supportedOptions(Builder $builder): array
{
return Arr::only($builder->options, [
'from',
'source',
]);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function setUp(): void

$this->withFactories(database_path('factories'));

Artisan::call('migrate:fresh', ['--database' => 'mysql']);
Artisan::call('migrate:fresh', ['--database' => env('DB_CONNECTION', 'mysql')]);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/ElasticSearch/SearchFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,16 @@ public function test_both_parameters_dont_take_effect_on_pagination(): void
$this->assertEquals($expectedSize, $search->getSize());
$this->assertEquals($expectedFrom, $search->getFrom());
}

public function test_source_can_be_set_from_options(): void
{
$builder = new Builder(new Product(), '*');
$builder->options([
'source' => $expectedFields = ['title', 'price'],
]);

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

$this->assertEquals($expectedFields, $search->isSource());
}
}

0 comments on commit 1a4de24

Please sign in to comment.