laravel-scout-elasticsearch icon indicating copy to clipboard operation
laravel-scout-elasticsearch copied to clipboard

Adding parameters to query

Open booni3 opened this issue 5 years ago • 3 comments

I see the package is using the ONGR QueryStringQuery by default and a lot is already possible with the elasticsearch query syntax.

In my case, I simply want to set a searchable fields array to the query but cannot see an easy way to append parameters to the ONGR query.

{
    "query_string" : {
        "fields" : ["name", "email"],
        "query" : "this AND that OR thus"
    }
}

Is it possible to amend the query inside the scout search callback, or would this need amendment to the search factory to inject the additional paramaters here?

I know you can get the queries from within the callback with $body->getQueries() but when filters are being used I am not sure its possible to determine which query is from $builder->query... unless we assume there is only going to be a single QueryStringQuery but that does not feel safe.

booni3 avatar Feb 08 '20 21:02 booni3

Yes, it's an issue. Probably it's not a good idea to pass a body to a callback and maybe it's better to pass builder itself and build the body from scratch. But I'm not sure about the drawbacks of the solution now. https://github.com/matchish/laravel-scout-elasticsearch/blob/f2cf4d79fc8dfbe0e712dc57fd45a5bc43bd416e/src/Engines/ElasticSearchEngine.php#L141

matchish avatar Feb 09 '20 08:02 matchish

I added an idea in #85 but it is a breaking change

booni3 avatar Feb 09 '20 10:02 booni3

Here the same solution as here https://github.com/matchish/laravel-scout-elasticsearch/issues/88#issuecomment-585557200 If we have callback in builder just send all builder and elasticsearch client to the callback then callback can build the right query from scratch

$model::search($term, function ($builder, $elasticsearch) {
  $query = new ElasticsearchMQuery::fromBuilder();
  return $elasticsearch->msearch($query->toArray());
})->get();

Feel free to send PR if you want.

Comments and objections are welcome)

matchish avatar Feb 13 '20 05:02 matchish