2
votes
$params2 = [
        'index' => 'index',
        'type'  => "items",
        'body' => [
            'aggs'  => [
                "types" => [
                    "filter" => [
                        "bool" => [
                            "should" => [
                                ["term" => ["type_id" => 1]],
                                ["term" => ["type_id" => 2]]

                            ]
                        ]
                    ],
                    "aggs" => [
                        "types" =>[
                            ["terms" => ["field" => "type_id","size" => 4]],
                            "aggs" =>[
                                "top" => [
                                    ["top_hits" => ["size" => 2]]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
        ]
    ];

when i pass this params to $elastic->search($params2);

its return me this exception

{"error":{"root_cause":[{"type":"unknown_named_object_exception","reason":"Unknown BaseAggregationBuilder [0]","line":1,"col":117}],"type":"unknown_named_object_exception","reason":"Unknown BaseAggregationBuilder [0]","line":1,"col":117},"status":400}

i am using ErickTamayo/laravel-scout-elastic package

1
Remove the square brackets around ["terms" => ["field" => "type_id","size" => 4]], => "terms" => ["field" => "type_id","size" => 4], - Val
thanks, now i have new exception {"error":{"root_cause":[{"type":"parsing_exception","reason":"Aggregation definition for [top starts with a [START_ARRAY], expected a [START_OBJECT].","line":1,"col":164}],"type":"parsing_exception","reason":"Aggregation definition for [top starts with a [START_ARRAY], expected a [START_OBJECT].","line":1,"col":164},"status":400} - lekinio

1 Answers

1
votes

You need to remove the square brackets around terms and top_hits

$params2 = [
        'index' => 'index',
        'type'  => "items",
        'body' => [
            'aggs'  => [
                "types" => [
                    "filter" => [
                        "bool" => [
                            "should" => [
                                ["term" => ["type_id" => 1]],
                                ["term" => ["type_id" => 2]]

                            ]
                        ]
                    ],
                    "aggs" => [
                        "types" =>[
                            "terms" => ["field" => "type_id","size" => 4],
                            "aggs" =>[
                                "top" => [
                                    "top_hits" => ["size" => 2]
                                ]
                            ]
                        ]
                    ]
                ]
            ],
        ]
    ];