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

Allow setting source (fields) from options #293

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
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());
}
}
Loading