Skip to content

Commit

Permalink
Merge pull request #286 from swayok/master
Browse files Browse the repository at this point in the history
Added support for whereNotIn() conditions.
  • Loading branch information
matchish authored Oct 10, 2024
2 parents a1be537 + d3d5c6f commit ee07d74
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/ElasticSearch/SearchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function create(Builder $builder, array $options = []): Search
$boolQuery = new BoolQuery();
$boolQuery = static::addWheres($builder, $boolQuery);
$boolQuery = static::addWhereIns($builder, $boolQuery);
$boolQuery = static::addWhereNotIns($builder, $boolQuery);
if (! empty($builder->query)) {
$boolQuery->add($query, BoolQuery::MUST);
}
Expand Down Expand Up @@ -54,7 +55,7 @@ public static function create(Builder $builder, array $options = []): Search
*/
private static function hasWhereFilters($builder): bool
{
return static::hasWheres($builder) || static::hasWhereIns($builder);
return static::hasWheres($builder) || static::hasWhereIns($builder) || static::hasWhereNotIns($builder);
}

/**
Expand Down Expand Up @@ -92,6 +93,22 @@ private static function addWhereIns($builder, $boolQuery): BoolQuery
return $boolQuery;
}

/**
* @param Builder $builder
* @param BoolQuery $boolQuery
* @return BoolQuery
*/
private static function addWhereNotIns($builder, $boolQuery): BoolQuery
{
if (static::hasWhereNotIns($builder)) {
foreach ($builder->whereNotIns as $field => $arrayOfValues) {
$boolQuery->add(new TermsQuery((string) $field, $arrayOfValues), BoolQuery::MUST_NOT);
}
}

return $boolQuery;
}

/**
* @param Builder $builder
* @return bool
Expand All @@ -109,4 +126,13 @@ private static function hasWhereIns($builder): bool
{
return isset($builder->whereIns) && ! empty($builder->whereIns);
}

/**
* @param Builder $builder
* @return bool
*/
private static function hasWhereNotIns($builder): bool
{
return isset($builder->whereNotIns) && ! empty($builder->whereNotIns);
}
}
8 changes: 8 additions & 0 deletions tests/Feature/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function test_search_with_filters(): void

$this->assertEquals($iphonePromoUsedAndLikeNew->count(), $iphonePromoUsedAndLikeNewAmount);
$this->assertInstanceOf(Product::class, $iphonePromoUsedAndLikeNew->first());

$iphonePromoNotUsedAndNotLikeNew = Product::search('iphone')
->where('price', 100)
->whereNotIn('type', ['used', 'like new'])
->get();

$this->assertEquals($iphonePromoNotUsedAndNotLikeNew->count(), $iphonePromoNewAmount);
$this->assertInstanceOf(Product::class, $iphonePromoNotUsedAndNotLikeNew->first());
}

public function test_search_with_custom_filter()
Expand Down

0 comments on commit ee07d74

Please sign in to comment.