matchish / laravel-scout-elasticsearch

Search among multiple models with ElasticSearch and Laravel Scout
MIT License
702 stars 113 forks source link

[Feature] Add possibility to use RangeQuery and other terms instances as value for $scoutQuery->where() #259

Closed swayok closed 10 months ago

swayok commented 11 months ago

Is your feature request related to a problem? Please describe. Problem: cannot use range condition through ->where() in scout query builder. And any other conditions except equation (ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery)

It is expected that query should look like this:

Ad::search($request->keywords)
    ->when($request->minPrice || $request->maxPrice, function (ScoutBuilder $query) use ($request) {
        $params = [];
        if ($request->minPrice) {
            $params[RangeQuery::GTE] = $request->minPrice;
        }
        if ($request->maxPrice) {
            $params[RangeQuery::LTE] = $request->maxPrice;
        }
        $priceRange = new RangeQuery('price', $params);
        $query->where('price', $priceRange);
    });

Describe the solution you'd like There is an interface ONGR\ElasticsearchDSL\BuilderInterface which is used to implement conditions like ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery and ONGR\ElasticsearchDSL\Query\TermLevel\RangeQuery so we can use instances of these classes as a value for ->where() query when we need something special.

Describe alternatives you've considered Currently to solve this task we need to use callback passed to Model::search() and do some magic which looks like overkill and also it is very complex.

Additional context I've already tested the solution and will submit PR. It is actually very simple.

hkulekci commented 10 months ago

Hey @swayok and @matchish , I guess we can close the ticket :)

swayok commented 10 months ago

Yes, sure =)