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

Avoid overriding minimumShouldMatch on compound queries #256

Merged
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
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
Loading