# Getting every 2nd element from \_source

**URL:** https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123
**Category:** Elasticsearch
**Created:** [January 23, 2022, 1:59am UTC](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123 "2022-01-23T01:59:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 23, 2022, 1:59am UTC](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123/1 "2022-01-23T01:59:25Z")

</div>

Hello dear community.  
I have been looking for information for a very long time.

I need to make mapping from search for every 2nd element. For example, I have "size": 1000, while I want to get every second element as a result (with a gap of one), in result 500 elements.

For example, now the output is:

```auto
/coin_charts/testcoin1/_search?filter_path=hits.hits._source

```

```auto
{
    "hits": {
        "hits": [
            {
                "_source": {
                    "time": 1356991200000,
                    "priceUsd": 5
                }
            },
            {
                "_source": {
                    "time": 1356991500000,
                    "priceUsd": 10
                }
            },
            {
                "_source": {
                    "time": 1356991800000,
                    "priceUsd": 6
                }
            },
...
        ]
    }
}

```

And i want to get this:

```auto
{
    "hits": {
        "hits": [
            {
                "_source": {
                    "time": 1356991200000,
                    "priceUsd": 5
                }
            },
            {
                "_source": {
                    "time": 1356991800000,
                    "priceUsd": 6
                }
            },
...
        ]
    }
}

```

Can you please tell me how to implement this?

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [January 23, 2022, 3:23am UTC](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123/2 "2022-01-23T03:23:48Z")

</div>

Hi,

as far as I know, there is no such parameter to choose every 2nd hits. What is your purpose of that filter, and what is the use of the output? Why not pick up every 2nd hits by client side?

---

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 23, 2022, 3:15pm UTC](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123/3 "2022-01-23T15:15:14Z")

</div>

The database is planned to have 5 million records of the cryptocurrency price with an interval of 5 minutes. I need to display a maximum of 1000 records, for example, so I need to take records from the database in the interval of several hundred records. Since it is unrealistically long and resource-intensive to process 5 million records on the backend.

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [January 23, 2022, 3:27pm UTC](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123/4 "2022-01-23T15:27:23Z")

</div>

So the true problem is the necessary `size` for query could be hundreds of thousands and too wasteful.

Then you can use [Date histogram aggregation | Elasticsearch Guide [7.16] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html) with [Top hits aggregation | Elasticsearch Guide [7.16] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html).  
Top hit with size=1 and sort by date, you can get last (or first) documents in the date-histo buckets.

---

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 23, 2022, 5:00pm UTC](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123/5 "2022-01-23T17:00:41Z")

</div>

Thank you. Tell me, please, can I use this if I have custom time fields that indicate milliseconds?

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [January 23, 2022, 5:18pm UTC](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123/6 "2022-01-23T17:18:37Z")

</div>

Sorry I'm not sure. Please try and share the solution.

If it is a numeric field, you should use histogram aggregation other than date histogram aggregation.

> **[Histogram aggregation | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-histogram-aggregation.html)**

---

<div class="post-metadata">

### Author: ![EnJay](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enjay/32/100715_2.png) [@EnJay](https://discuss.elastic.co/u/EnJay)
#### Post date: [January 23, 2022, 6:19pm UTC](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123/7 "2022-01-23T18:19:25Z")

</div>

Ok. Thank you.

---

<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: [February 20, 2022, 6:19pm UTC](https://discuss.elastic.co/t/getting-every-2nd-element-from-source/295123/8 "2022-02-20T18:19:28Z")

</div>

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