# Aggregation 504 bad gateway

**URL:** <https://discuss.elastic.co/t/aggregation-504-bad-gateway/251740>\
**Category:** Elasticsearch\
**Created:** [October 12, 2020, 9:40am UTC](https://discuss.elastic.co/t/aggregation-504-bad-gateway/251740 "2020-10-12T09:40:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liyes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/liyes/32/64705_2.png) [@Liyes](https://discuss.elastic.co/u/Liyes)\
**Post date:** [October 12, 2020, 9:40am UTC](https://discuss.elastic.co/t/aggregation-504-bad-gateway/251740/1 "2020-10-12T09:40:57Z")

</div>

When I use script to do aggregation and want to find two documents with the same fields in es, 504 bad gateway.  
Maybe it is caused by too much data. Is there any solution?  
I used partition.

```auto
{
  "size": 0,
  "aggs": {
    "duplicateCount": {
      "terms": {
        "script": {
          "source":"doc['id'].value ",
          "lang": "painless"
        },
        "min_doc_count": 2,
        "size": 10,
        "include": {
          "partition": 0,
          "num_partitions": 10
        }
      },
      "aggs": {
        "duplicateDocuments": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

```

error

```auto
<html>
    <head>
        <title>504 Gateway Time-out</title>
    </head>
    <body>
        <center>
            <h1>504 Gateway Time-out</h1>
        </center>
        <hr>
        <center>nginx</center>
    </body>
</html>

```

---

<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:** [October 12, 2020, 11:50am UTC](https://discuss.elastic.co/t/aggregation-504-bad-gateway/251740/2 "2020-10-12T11:50:13Z")

</div>

Sounds like an error message from nginx not elasticsearch.

---

<div class="post-metadata">

**Author:** ![Liyes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/liyes/32/64705_2.png) [@Liyes](https://discuss.elastic.co/u/Liyes)\
**Post date:** [October 13, 2020, 2:36am UTC](https://discuss.elastic.co/t/aggregation-504-bad-gateway/251740/3 "2020-10-13T02:36:35Z")

</div>

> [@dadoonet](#):
>
> nginx

Thank you very much.

---

<div class="post-metadata">

**Author:** ![Liyes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/liyes/32/64705_2.png) [@Liyes](https://discuss.elastic.co/u/Liyes)\
**Post date:** [October 13, 2020, 2:51am UTC](https://discuss.elastic.co/t/aggregation-504-bad-gateway/251740/4 "2020-10-13T02:51:09Z")

</div>

Nginx error is caused by a timeout.  
How can I make this request return a partial result quickly?

---

<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:** [October 13, 2020, 9:31am UTC](https://discuss.elastic.co/t/aggregation-504-bad-gateway/251740/5 "2020-10-13T09:31:18Z")

</div>

May be have a look at [https://www.elastic.co/guide/en/elasticsearch/reference/7.x/async-search-intro.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/async-search-intro.html) or increase the nginx timeout?

Why are you using a painless script to run a `terms` aggregation? Why not a `terms` agg on the `id` field?

---

<div class="post-metadata">

**Author:** ![Liyes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/liyes/32/64705_2.png) [@Liyes](https://discuss.elastic.co/u/Liyes)\
**Post date:** [October 14, 2020, 2:13am UTC](https://discuss.elastic.co/t/aggregation-504-bad-gateway/251740/6 "2020-10-14T02:13:44Z")

</div>

Thank you very much. I'm just giving a simple example here. In our real use scenario, the type is an array. It is more complicated to sort each value in the array for aggregation, so script is used.

---

<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:** [November 11, 2020, 2:14am UTC](https://discuss.elastic.co/t/aggregation-504-bad-gateway/251740/7 "2020-11-11T02:14:01Z")

</div>

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