diff --git a/CHANGELOG.md b/CHANGELOG.md index 61aeaf9..a0963aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index 9f2f820..5e5b446 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/ElasticSearch/SearchFactory.php b/src/ElasticSearch/SearchFactory.php index b60d0cd..7e56084 100644 --- a/src/ElasticSearch/SearchFactory.php +++ b/src/ElasticSearch/SearchFactory.php @@ -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); diff --git a/tests/Feature/SearchTest.php b/tests/Feature/SearchTest.php index 5dfee01..a6cef7b 100644 --- a/tests/Feature/SearchTest.php +++ b/tests/Feature/SearchTest.php @@ -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()); + } }