Skip to content

Commit

Permalink
Merge branch 'master' into elastic8
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen-G authored Dec 25, 2024
2 parents ab74ab6 + 2fd73a6 commit f935042
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 43 deletions.
13 changes: 8 additions & 5 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@ All notable changes to `Explorer` will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased


## Unreleased

## [4.0.0]

### Added

- Support for elastic 8
- Support for ElasticSearch 8.

### Removed

- Elastic 7 support. The elastic 8 client is not backwards compatible
- ElasticSearch 7 support: the elastic 8 client is not backwards compatible. See 'changed' for the breaking changes.
- Support for Laravel 9 and below to make package maintenance easier.

### Changed

- The import path for `selector` has changed from `Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector` to `Elastic\Transport\NodePool\Selector\RoundRobin`

## [3.15.0]

### Fixed
- Avoid overriding minimumShouldMatch on compound queries.

## [3.14.0]

### Added
Expand Down
15 changes: 0 additions & 15 deletions src/Infrastructure/Scout/ScoutSearchCommandBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class ScoutSearchCommandBuilder implements SearchCommandInterface

private array $fields = [];

private ?string $minimumShouldMatch = null;

/** @var Sort[] */
private array $sort = [];

Expand Down Expand Up @@ -72,7 +70,6 @@ public static function wrap(Builder $builder): ScoutSearchCommandBuilder
$normalizedBuilder->setFields($builder->fields ?? []);
$normalizedBuilder->setBoolQuery($builder->compound ?? new BoolQuery());
$normalizedBuilder->setLimit($builder->limit);
$normalizedBuilder->setMinimumShouldMatch($builder->minimumShouldMatch ?? null);
$normalizedBuilder->queryProperties = $builder->queryProperties ?? [];

$index = $builder->index ?: $builder->model->searchableAs();
Expand Down Expand Up @@ -221,16 +218,6 @@ public function setFields(array $fields): void
$this->fields = $fields;
}

public function setMinimumShouldMatch(?string $value): void
{
$this->minimumShouldMatch = $value;
}

public function getMinimumShouldMatch(): ?string
{
return $this->minimumShouldMatch;
}

public function hasFields(): bool
{
return !empty($this->fields);
Expand Down Expand Up @@ -269,8 +256,6 @@ public function buildQuery(): array
$compound->add('must', new MultiMatch($this->query, $this->getDefaultSearchFields()));
}

$compound->minimumShouldMatch($this->getMinimumShouldMatch());

foreach ($this->wheres as $field => $value) {
$compound->add('filter', new Term($field, $value));
}
Expand Down
52 changes: 29 additions & 23 deletions tests/Unit/ScoutSearchCommandBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,21 +261,6 @@ public function test_it_accepts_a_custom_compound(): void
self::assertSame($compound, $command->getBoolQuery());
}

public function test_it_accepts_minimum_should_match(): void
{
$subject = new ScoutSearchCommandBuilder();

$subject->setMinimumShouldMatch('50%');

$query = $subject->buildQuery();

$expectedQuery = [
'query' => ['bool' => ['must' => [], 'should' => [], 'filter' => [], 'minimum_should_match' => '50%']],
];

self::assertEquals($expectedQuery, $query);
}

public function test_it_wraps_with_a_custom_compound(): void
{
$compound = Mockery::mock(BoolQuery::class);
Expand All @@ -289,6 +274,23 @@ public function test_it_wraps_with_a_custom_compound(): void
self::assertSame($compound, $subject->getBoolQuery());
}

public function test_it_respects_minimum_should_match_on_custom_compound(): void
{
$command = new ScoutSearchCommandBuilder();
$compound = new BoolQuery();
$compound->minimumShouldMatch('50%');

$command->setBoolQuery($compound);

$query = $command->buildQuery();

$expectedQuery = [
'query' => ['bool' => ['must' => [], 'should' => [], 'filter' => [], 'minimum_should_match' => '50%']],
];

self::assertEquals($expectedQuery, $query);
}

public function test_it_has_bool_query_as_default_compound(): void
{
$builder = Mockery::mock(Builder::class);
Expand Down Expand Up @@ -351,13 +353,11 @@ public function test_it_adds_scout_properties_to_boolquery(): void
$subject->setShould([$term]);
$subject->setBoolQuery($boolQuery);
$subject->setWheres([ $whereField => $whereValue ]);
$subject->setMinimumShouldMatch('50%');

$boolQuery->expects('clone')->andReturn($boolQuery);
$boolQuery->expects('addMany')->with(QueryType::MUST, [$term]);
$boolQuery->expects('addMany')->with(QueryType::SHOULD, [$term]);
$boolQuery->expects('addMany')->with(QueryType::FILTER, [$term]);
$boolQuery->expects('minimumShouldMatch')->with('50%');
$boolQuery->expects('build')->andReturn($returnQuery);

$boolQuery->expects('add')
Expand Down Expand Up @@ -425,19 +425,25 @@ public function test_it_wraps_scout_builder_aggregations(): void
self::assertSame($input, $subject->getAggregations());
}

public function test_it_wraps_scout_builder_minimum_should_match(): void
public function test_it_wraps_a_compound_using_minimum_should_match(): void
{
$compound = new BoolQuery();
$compound->minimumShouldMatch('50%');
$builder = Mockery::mock(Builder::class);
$builder->model = Mockery::mock(Model::class);
$minimumShouldMatch = '50%';

$builder->index = self::TEST_INDEX;
$builder->minimumShouldMatch = $minimumShouldMatch;
$builder->compound = $compound;

$subject = ScoutSearchCommandBuilder::wrap($builder);

self::assertSame($minimumShouldMatch, $subject->getMinimumShouldMatch());
}
$query = $subject->buildQuery();

$expectedQuery = [
'query' => ['bool' => ['must' => [], 'should' => [], 'filter' => [], 'minimum_should_match' => '50%']],
];

self::assertEquals($expectedQuery, $query);
}

public function test_it_wraps_scout_builder_query_properties(): void
{
Expand Down

0 comments on commit f935042

Please sign in to comment.