Skip to content

Commit

Permalink
Merge pull request #269 from Fayne/feature/support-range-search
Browse files Browse the repository at this point in the history
Fix query_string issue and range search issue
  • Loading branch information
matchish authored Feb 23, 2024
2 parents 00e3caf + 47c8b97 commit 3985ffa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/)

## [Unreleased]

## [7.6.0] - 2024-02-23
### Added
- Add one more condition. If the search() method does not pass any parameter, there is no need to add QueryStringQuery object.

## [7.5.0] - 2023-11-30
### Added
- [Added support for php 8.3](https://github.com/matchish/laravel-scout-elasticsearch/pull/266)
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,18 @@ Product::search('(title:this OR description:this) AND (title:that OR description
]);
```

And if you just want to search using RangeQuery without any query_string, you can call the search() method directly and leave the param empty.

```php

use ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery;

Product::search()
->where('price', new RangeQuery('price', [
RangeQuery::GTE => 100,
]);
```

Full list of ElasticSearch terms is in `vendor/handcraftedinthealps/elasticsearch-dsl/src/Query/TermLevel`.

### Search amongst multiple models
Expand Down
4 changes: 3 additions & 1 deletion src/ElasticSearch/SearchFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public static function create(Builder $builder, array $options = []): Search
$boolQuery = new BoolQuery();
$boolQuery = static::addWheres($builder, $boolQuery);
$boolQuery = static::addWhereIns($builder, $boolQuery);
$boolQuery->add($query, BoolQuery::MUST);
if (! empty($builder->query)) {
$boolQuery->add($query, BoolQuery::MUST);
}
$search->addQuery($boolQuery);
} else {
$search->addQuery($query);
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,4 +232,37 @@ public function test_cursor_no_results()
$this->assertEquals(LazyCollection::class, get_class($kindle));
$this->assertEquals(0, $kindle->count());
}

public function test_empty_query_string()
{
$dispatcher = Product::getEventDispatcher();
Product::unsetEventDispatcher();

$greaterCount = rand(10, 100);

factory(Product::class, $greaterCount)->create(['price' => rand(200, 300)]);

Product::setEventDispatcher($dispatcher);

Artisan::call('scout:import');

$notCheapProducts = Product::search()
->where('price', new RangeQuery('price', [
RangeQuery::GTE => 190,
]))->paginate(100);

$cheapProducts = Product::search()
->where('price', new RangeQuery('price', [
RangeQuery::LTE => 190,
]))->get();

$expensiveProducts = Product::search()
->where('price', new RangeQuery('price', [
RangeQuery::GTE => 310,
]))->get();

$this->assertEquals($greaterCount, $notCheapProducts->total());
$this->assertEquals(0, $cheapProducts->count());
$this->assertEquals(0, $expensiveProducts->count());
}
}

0 comments on commit 3985ffa

Please sign in to comment.