Elasticsearch Aggregate Function Error On Laravel
Elasticsearch gives error when integrating with laravel using aggregations. This is my code:
$laws_y = Law::searchByQuery([
'multi_match' => [
'query' => $years,
'fields' => ["law_year"]
],
"aggs" => [
"group_by_law_year" => ["terms" => ['field' => ["law_year"]]]
]
]);
I get following error:
BadRequest400Exception in GuzzleConnection.php line 277: {"error":{"root_cause":[{"type":"parse_exception","reason":"failed to parse search source. expected field name but got [START_OBJECT]"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"default","node":"BcRQOVhkS1SwTlvYPCEfHg","reason":{"type":"parse_exception","reason":"failed to parse search source. expected field name but got [START_OBJECT]"}}]},"status":400}
Does anyone know the solution?
Answer
From the Elasticquent documentation, the searchByQuery
function takes the following parameters (see source here):
query
- Your ElasticSearch Queryaggregations
- The Aggregations you wish to return.sourceFields
- Limits returned set to the selected fields onlylimit
- Number of records to returnoffset
- Sets the record offset (use for paging results)sort
- Your sort query
In your call, you need to separate the query (first parameter) from the aggregations (second parameter). Do it like this instead:
$laws_y = Law::searchByQuery([
'multi_match' => [
'query' => $years,
'fields' => ["law_year"]
]
],
[
"group_by_law_year" => ["terms" => ['field' => "law_year"]]
]);
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?