From 1a4de2476904e914941a00b1e296d9b523f51574 Mon Sep 17 00:00:00 2001 From: Jaanus Vapper Date: Wed, 18 Dec 2024 13:39:32 +0200 Subject: [PATCH] Allow setting source (fields) from options --- src/ElasticSearch/SearchFactory.php | 4 ++++ tests/TestCase.php | 2 +- tests/Unit/ElasticSearch/SearchFactoryTest.php | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ElasticSearch/SearchFactory.php b/src/ElasticSearch/SearchFactory.php index b0ba367b..274b0260 100644 --- a/src/ElasticSearch/SearchFactory.php +++ b/src/ElasticSearch/SearchFactory.php @@ -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'])); @@ -153,6 +156,7 @@ private static function supportedOptions(Builder $builder): array { return Arr::only($builder->options, [ 'from', + 'source', ]); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index d0fb948f..b6778f65 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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')]); } /** diff --git a/tests/Unit/ElasticSearch/SearchFactoryTest.php b/tests/Unit/ElasticSearch/SearchFactoryTest.php index 731d7a13..c5c71982 100644 --- a/tests/Unit/ElasticSearch/SearchFactoryTest.php +++ b/tests/Unit/ElasticSearch/SearchFactoryTest.php @@ -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()); + } }