laravel-scout-elasticsearch
laravel-scout-elasticsearch copied to clipboard
Adding parameters to query
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.
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
I added an idea in #85 but it is a breaking change
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)