# Get the number of people who have bought twice

**URL:** <https://discuss.elastic.co/t/get-the-number-of-people-who-have-bought-twice/302211>\
**Category:** Elasticsearch\
**Created:** [April 12, 2022, 10:17am UTC](https://discuss.elastic.co/t/get-the-number-of-people-who-have-bought-twice/302211 "2022-04-12T10:17:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amir\_Faramarzi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amir_faramarzi/32/92919_2.png) [@Amir\_Faramarzi](https://discuss.elastic.co/u/Amir_Faramarzi)\
**Post date:** [April 12, 2022, 10:17am UTC](https://discuss.elastic.co/t/get-the-number-of-people-who-have-bought-twice/302211/1 "2022-04-12T10:17:27Z")

</div>

Hello friends

I have an index of orders as follows

```auto
{
        "offer" : {
          "type" : "unsigned_long"
        },
        "order_number" : {
          "type" : "keyword"
        },
        "payable" : {
          "type" : "unsigned_long"
        },    
        "status" : {
          "type" : "byte"
        },
        "total" : {
          "type" : "unsigned_long"
        },
        "user_id" : {
          "type" : "keyword"
        }
}

```

How can I get the number of people who have bought twice ??

Relevant field user\_id

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 12, 2022, 1:16pm UTC](https://discuss.elastic.co/t/get-the-number-of-people-who-have-bought-twice/302211/2 "2022-04-12T13:16:55Z")

</div>

Hi.

Maybe this:

```auto
{
  "size": 0,
  "aggs": {
    "purchases-twice": {
      "terms": {
        "field": "user_id",
        "size": 10
      },
      "aggs": {
        "count_purchases": {
          "value_count": {
            "field": "total"
          }
        },
        "sales_bucket_filter": {
          "bucket_selector": {
            "buckets_path": {
              "countPurchases": "count_purchases"
            },
            "script": "params.countPurchases == 2"
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Amir\_Faramarzi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amir_faramarzi/32/92919_2.png) [@Amir\_Faramarzi](https://discuss.elastic.co/u/Amir_Faramarzi)\
**Post date:** [April 12, 2022, 1:40pm UTC](https://discuss.elastic.co/t/get-the-number-of-people-who-have-bought-twice/302211/3 "2022-04-12T13:40:48Z")

</div>

hi

not working!

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 12, 2022, 1:52pm UTC](https://discuss.elastic.co/t/get-the-number-of-people-who-have-bought-twice/302211/4 "2022-04-12T13:52:28Z")

</div>

Empty result? I used the same mapping you reported.

---

<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 10, 2022, 1:53pm UTC](https://discuss.elastic.co/t/get-the-number-of-people-who-have-bought-twice/302211/5 "2022-05-10T13:53:14Z")

</div>

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