Elastica PHP Sum Aggregation error

Hello all,

I have, say, an index called X, a type called Y, and a field called number.
There are 50 data.
I want to calculate the sum of the values of each number field.

I tried this but it doesn't work :

    $numberSum= new \Elastica\Aggregation\Sum();
    $numberSum->setField('number');
    $numberSum->setName('NumberSum');

    $query = new Elastica\Query();
    $query->addAggregation($numberSum);

    $index = $client->getIndex('X');
    $buckets = $index->search($query)->getAggregation("NumberSum");

    var_dump($buckets);

I get this error, achats_report is the index name :

Fatal error: Uncaught exception 'Elastica\Exception\ResponseException' with message 'SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][0]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][1]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][2]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][3]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][4]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][5]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][6]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][7]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][8]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][9]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}]' 
in /var/www/vendor/ruflin/elastica/lib/Elastica/Transport/Http.php:159 
Stack trace: #0 /var/www/vendor/ruflin/elastica/lib/Elastica/Request.php(171): Elastica\Transport\Http->exec(Object(Elastica\Request), Array) 
#1 /var/www/vendor/ruflin/elastica/lib/Elastica/Client.php(621): Elastica\Request->send() 
#2 /var/www/vendor/ruflin/elastica/lib/Elastica/Search.php(457): Elastica\Client->request('achats_report/_...', 'GET', Array, Array) 
#3 /var/www/vendor/ruflin/elastica/lib/Elastica/Index.php(329): Elastica\Search->search() 
#4 /var/www/htdocs/adm54140/achatsReport.php(77): Elastica\Index->search(Object(Elastica\Query)) 
#5 {main} thrown in /var/www/vendor/ruflin/elastica/lib/Elastica/Transport/Http.php on line 159 array(4) { ["code"]=> string(7) "E_ERROR" ["message"]=> string(2852) "Uncaught exception 'Elastica\Exception\ResponseException' with message 'SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][0]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][1]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][2]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][3]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][4]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][5]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][6]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][7]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][8]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}{[JG4-QsaASNmVM8rj0Wzn7g]
[achats_report][9]: ClassCastException[org.elasticsearch.index.fielddata.plain.PagedBytesIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexNumericFieldData]}]' 
in /var/www/vendor/ruflin/elastica/lib/Elastica/Transport/Http.php:159 
Stack trace: #0 /var/www/vendor/ruflin/elastica/lib/Elastica/Request.php(171): Elastica\Transport\Http->exec(Object(Elastica\Request), Array) 
#1 /var/www/vendor/ruflin/elastica/lib/Elastica/Client.php(621): Elastica\Request->send() 
#2 /var/www/vendor/ruflin/elastica/lib/Elastica/Search.php(457): Elastica\Client->request('achats_report/_...', 'GET', Array, Array) 
#3 /var/www/vendor/ruflin/elastica/lib/Elastica/Index.php(329): Elastica\Search->search() 
#4 /var/www/htdocs/adm54140/achatsReport.php(77): Elastica\Index->search(Object(Elastica\Query)) 
#5 {main} thrown" ["file"]=> string(63) "/var/www/vendor/ruflin/elastica/lib/Elastica/Transport/Http.php" ["line"]=> int(159) }

Any idea what does it mean or what am I doing wrong?

I found the problem, I had to define the the type should be a number, I had it as a string. Thanks!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.