pdphilip / laravel-elasticsearch

Laravel Elasticsearch: An Elasticsearch implementation of Laravel's Eloquent ORM
MIT License
86 stars 16 forks source link

i cant use groupBy please help me #10

Closed davoodf1995 closed 8 months ago

davoodf1995 commented 8 months ago

my error 400 Bad Request: {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Fielddata is disabled on [vendor_id] in [report_vendors]. Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [vendor_id] in order to load field data by uninverting the inverted index. Note that this can use significant memory."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":

my model `<?php

namespace App\ElasticModels;

use PDPhilip\Elasticsearch\Eloquent\Model as EloquentModel;

class ReportVendor extends EloquentModel { public $connection = 'elasticsearch'; protected $mapping = [ 'properties' => [ 'vendor_name' => ['type' => 'text'], 'vendor_id' => [ 'type' => 'text', 'fielddata' => true, ], 'availability_id' => ['type' => 'keyword'], 'manufacturer_name' => ['type' => 'text'], 'availability_created_at' => ['type' => 'date'], 'vendor_rfq_id' => ['type' => 'keyword'], 'rfq_id' => ['type' => 'keyword'], 'vendor_rfq_created_at' => ['type' => 'date'], 'rfq_created_at' => ['type' => 'date'], ], ];

protected $guarded = [];

}`

?>

my query $result = ReportVendor::where('availability_source', 'Skype / WeChat'); $result = $result->groupBy('vendor_id');

pdphilip commented 8 months ago

As the error states, Elasticsearch cannot sort on text fields, index vendor_id as a keyword type.

and/or try

ReportVendor::where('availability_source', 'Skype / WeChat')->groupBy('vendor_id.keyword')->get()
pdphilip commented 8 months ago

Haven't heard back and assuming you fixed your issue, no bug and closing this