diff --git a/changelog.md b/changelog.md index 797e4ef..8b32c88 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php b/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php index d674c01..fd420dc 100644 --- a/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php +++ b/src/Infrastructure/Scout/ScoutSearchCommandBuilder.php @@ -31,8 +31,6 @@ class ScoutSearchCommandBuilder implements SearchCommandInterface private array $fields = []; - private ?string $minimumShouldMatch = null; - /** @var Sort[] */ private array $sort = []; @@ -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(); @@ -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); @@ -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)); } diff --git a/tests/Unit/ScoutSearchCommandBuilderTest.php b/tests/Unit/ScoutSearchCommandBuilderTest.php index 4e4f1a1..4935841 100644 --- a/tests/Unit/ScoutSearchCommandBuilderTest.php +++ b/tests/Unit/ScoutSearchCommandBuilderTest.php @@ -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); @@ -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); @@ -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') @@ -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 {