# Elastica PHP Sum Aggregation error

**URL:** <https://discuss.elastic.co/t/elastica-php-sum-aggregation-error/128493>\
**Category:** Elasticsearch\
**Created:** [April 18, 2018, 9:32am UTC](https://discuss.elastic.co/t/elastica-php-sum-aggregation-error/128493 "2018-04-18T09:32:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [April 18, 2018, 9:32am UTC](https://discuss.elastic.co/t/elastica-php-sum-aggregation-error/128493/1 "2018-04-18T09:32:12Z")

</div>

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 :

```auto
    $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 :

```auto
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?

---

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [April 18, 2018, 2:01pm UTC](https://discuss.elastic.co/t/elastica-php-sum-aggregation-error/128493/2 "2018-04-18T14:01:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 16, 2018, 2:01pm UTC](https://discuss.elastic.co/t/elastica-php-sum-aggregation-error/128493/3 "2018-05-16T14:01:39Z")

</div>

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