# ES-5.0.0- PHP aggregations

**URL:** <https://discuss.elastic.co/t/es-5-0-0-php-aggregations/67626>\
**Category:** Elasticsearch\
**Created:** [November 30, 2016, 2:05pm UTC](https://discuss.elastic.co/t/es-5-0-0-php-aggregations/67626 "2016-11-30T14:05:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hilal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hilal/32/7684_2.png) [@Hilal](https://discuss.elastic.co/u/Hilal)\
**Post date:** [November 30, 2016, 2:05pm UTC](https://discuss.elastic.co/t/es-5-0-0-php-aggregations/67626/1 "2016-11-30T14:05:07Z")

</div>

hi, I want to do aggregation with PHP and ES-5.0.0.

my JSON CODES:

```
GET titubb56k/ihale/_search
{
  "size" : 0,
  "query": {
    "match": {
      "IdareKodu": "11340073"
      }
    },
"aggs":{
    "group_by_state":{
      "terms":{
        "field": "IstekliKodu",
        "order":{
          "_count":"desc"
        } } } } }

```

It works true:

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1083,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "group_by_state": {
      "doc_count_error_upper_bound": 8,
      "sum_other_doc_count": 399,
      "buckets": [
        {
          "key": 8637772198013,
          "doc_count": 269
        },
        {
          "key": 8637772026090,
          "doc_count": 118
        },
        {
          "key": 8637772254300,
          "doc_count": 98
        },
        {
          "key": 2667269006804,
          "doc_count": 40
        },
        {
          "key": 2667269001340,
          "doc_count": 39
        },
        {
          "key": 2667269000712,
          "doc_count": 27
        },
        {
          "key": 2667269010870,
          "doc_count": 25
        },
        {
          "key": 3400375,
          "doc_count": 24
        },
        {
          "key": 8637772000408,
          "doc_count": 23
        },
        {
          "key": 2667269000730,
          "doc_count": 21
        } ] } }}

```

and When I did with php codes:

```
if($IdareKodu){
    
    $searchParams['body']['query']['match']['IdareKodu'] = $IdareKodu;
    $searchParams['body']['aggs']['group_by_state']['terms']['field']= 'IstekliKodu';
    $searchParams['body']['aggs']['group_by_state']['terms']['size']= 0;
    $searchParams['body']['aggs']['group_by_state']['terms']['order']['_count'] = "desc";
}

$searchParams['body']['sort']['id']['order'] = 'asc';

pa($searchParams);
//exit;
//pa($searchParams['body']['filter']);
br();br();br();
$sorgus = mc();br();

$queryResponse = $client->search($searchParams);

```

when I did query by API : [http://192.168.1.36/filtreleme.php?IdareKodu=11340073](http://192.168.1.36/filtreleme.php?IdareKodu=11340073)

RESULT:

```
Fatal error: Uncaught exception 
'Elasticsearch\Common\Exceptions\BadRequest400Exception' with message 
'{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"size
 must be positive, got 
0"}],"type":"search_phase_execution_exception","reason":"all shards 
failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"titubb56k","node":"NchqMHFlRZqnHALL4pRzUA","reason":{"type":"illegal_argument_exception","reason":"size
 must be positive, got 
0"}}],"caused_by":{"type":"illegal_argument_exception","reason":"size 
must be positive, got 0"}},"status":400}' in 
/home/admin/web/localhost.example.com/public_html/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php:681
Stack
 trace:
#0 
/home/admin/web/localhost.example.com/public_html/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php(659):
 Elasticsearch\Connections\Connection->tryDeserializeError(Array, 
'Elasticsearch\\C...')
#1 
/home/admin/web/localhost.example.com/public_html/vendor/elasticsearch/ela
 in /home/admin/web/localhost.example.com/public_html/vendor/elasticsearch/elasticsearch/src/Elasticsearch/Connections/Connection.php on line 682

```

What is the problem?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 30, 2016, 2:17pm UTC](https://discuss.elastic.co/t/es-5-0-0-php-aggregations/67626/2 "2016-11-30T14:17:42Z")

</div>

> "reason":"size must be positive, got 0"

Isn't that obvious?

You put `size` on the agg, not on the search request itself.

---

<div class="post-metadata">

**Author:** ![Hilal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hilal/32/7684_2.png) [@Hilal](https://discuss.elastic.co/u/Hilal)\
**Post date:** [November 30, 2016, 3:02pm UTC](https://discuss.elastic.co/t/es-5-0-0-php-aggregations/67626/3 "2016-11-30T15:02:26Z")

</div>

I did:  
if($IdareKodu){  
$searchParams['body']['size']= 0;  
$searchParams['body']['query']['match']['IdareKodu'] = $IdareKodu;  
$searchParams['body']['aggs']['group\_by\_state']['terms']['field']= 'IstekliKodu';  
$searchParams['body']['aggs']['group\_by\_state']['terms']['order']['\_count'] = "desc";  
}

However,  
Eventually only records related to the $IdareKodu It is not doing aggregation.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 30, 2016, 3:18pm UTC](https://discuss.elastic.co/t/es-5-0-0-php-aggregations/67626/4 "2016-11-30T15:18:22Z")

</div>

`size` does not have any effect on the aggregations.

---

<div class="post-metadata">

**Author:** ![Hilal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hilal/32/7684_2.png) [@Hilal](https://discuss.elastic.co/u/Hilal)\
**Post date:** [December 1, 2016, 6:07am UTC](https://discuss.elastic.co/t/es-5-0-0-php-aggregations/67626/5 "2016-12-01T06:07:39Z")

</div>

Ok I understand. Thank you.

I removed ['body'] and it worked true. (`$searchParams['body']['size']= 0;`)

```
if($IdareKodu){
    $searchParams['size']= 0;
    $searchParams['body']['query']['match']['IdareKodu'] = $IdareKodu;
    $searchParams['body']['aggs']['group_by_state']['terms']['field']= 'IstekliKodu';
    $searchParams['body']['aggs']['group_by_state']['terms']['order']['_count'] = "desc";
}
```

---

<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:** [December 29, 2016, 6:07am UTC](https://discuss.elastic.co/t/es-5-0-0-php-aggregations/67626/6 "2016-12-29T06:07:40Z")

</div>

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