1

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?

0

1 Answer 1

2

From the Elasticquent documentation, the searchByQuery function takes the following parameters (see source here):

  • query - Your ElasticSearch Query
  • aggregations - The Aggregations you wish to return.
  • sourceFields - Limits returned set to the selected fields only
  • limit - Number of records to return
  • offset - 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"]]
     ]);
Sign up to request clarification or add additional context in comments.

7 Comments

Yes that solved that error, but I still get another error BadRequest400Exception in GuzzleConnection.php line 277: {"error":{"root_cause":[{"type":"search_parse_exception","reason":"Could not find aggregator type [group_by_law_year] in [aggs]","line":1,"col":92}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"default","node":"BcRQOVhkS1SwTlvYPCEfHg","reason":{"type":"search_parse_exception","reason":"Could not find aggregator type [group_by_law_year] in [aggs]","line":1,"col":92}}]},"status":400}
Are you including the aggs token in your second parameter?
This is the result when I run dd($years): "2013 2012 2013 ". Can that help?
This is the mapping property: protected $mappingProperties = array( 'law_year' => [ 'type' => 'string', "analyzer" => "standard", ], ); In the database law_year field is an integer, maybe that is the problem?
No, it's probably not the issue. Are you using the exact same code as I've written in my answer?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.