From 8046db92354b2b1d5dd6d62097bd7ab3ce682e7c Mon Sep 17 00:00:00 2001 From: Jaanus Vapper Date: Wed, 18 Dec 2024 13:39:32 +0200 Subject: [PATCH 1/2] 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()); + } } From c00d8a16ca7334c9a1536d9c8d354d0bb1f5813c Mon Sep 17 00:00:00 2001 From: Jaanus Vapper Date: Tue, 24 Dec 2024 11:38:41 +0200 Subject: [PATCH 2/2] Update readme and changelog --- CHANGELOG.md | 2 ++ README.md | 6 +++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 011328f3..62955815 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/) ## [Unreleased] +### Added +- Use [`source` in options](https://github.com/matchish/laravel-scout-elasticsearch/pull/293) to set returned fields ## [7.9.0] - 2024-11-14 ### Fixed diff --git a/README.md b/README.md index adb97ebe..aed38058 100644 --- a/README.md +++ b/README.md @@ -276,6 +276,10 @@ Product::search() Full list of ElasticSearch terms is in `vendor/handcraftedinthealps/elasticsearch-dsl/src/Query/TermLevel`. +### Limiting returned fields +Sometimes your indexed models have fields that should not appear in returned result. +You can set returned fields in `source` option `->options(['source' => ['this', 'that', 'something', 'else']])` + ### Pagination The engine supports [Elasticsearch pagination](https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html) with [Scout Builder pagination](https://laravel.com/docs/11.x/scout#pagination) or by setting page sizes @@ -309,7 +313,7 @@ In this example you will get collection of `Ticket` and `Book` models where tick book title is `Barcelona` ### Working with results -Often your response isn't collection of models but aggregations or models with higlights an so on. +Often your response isn't collection of models but aggregations or models with higlights and so on. In this case you need to implement your own implementation of `HitsIteratorAggregate` and bind it in your service provider [Here is a case](https://github.com/matchish/laravel-scout-elasticsearch/issues/28)