babenkoivan / elastic-scout-driver-plus

Extension for Elastic Scout Driver
MIT License
267 stars 52 forks source link

Ability to use when() with compound queries #136

Closed chrisgrim closed 2 years ago

chrisgrim commented 2 years ago
Software Version
PHP 7.4.23
Elasticsearch 8.4
Laravel 8
Laravel Scout 9.4.10
Elastic Scout Driver 3.0.1

In a previous version, I could use the when statement for my compound queries like

 ->when($request->price, function ($builder) use ($request) {
            return $builder->must('range', ['priceranges.price' => [ 'gte' => $request->price[0],'lte' => $request->price[1]]]);
        })
->when($request->category, function ($builder) use ($request) {
            return $builder->filter('terms', ['category_id' => $request->category]);
        })

however, with the new update, it seems the when() has been moved to the SearchParametersBuilder which just allows sorting etc.. Is there a way to do a when statement with a query?

chrisgrim commented 2 years ago

I figured out I had to create seperate queries

$prices = Query::range()->field('priceranges.price')->gte($request->price[0])->lte($request->price[1]);

and then load that into the when() builder

->when($request->price, function ($builder) use ($prices) { return $builder->must($prices); })