Getting every 2nd element from _source

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:

/coin_charts/testcoin1/_search?filter_path=hits.hits._source
{
    "hits": {
        "hits": [
            {
                "_source": {
                    "time": 1356991200000,
                    "priceUsd": 5
                }
            },
            {
                "_source": {
                    "time": 1356991500000,
                    "priceUsd": 10
                }
            },
            {
                "_source": {
                    "time": 1356991800000,
                    "priceUsd": 6
                }
            },
...
        ]
    }
}

And i want to get this:

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

Can you please tell me how to implement this?

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?

1 Like

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.

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 with Top hits aggregation | Elasticsearch Guide [7.16] | Elastic.
Top hit with size=1 and sort by date, you can get last (or first) documents in the date-histo buckets.

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

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.

1 Like

Ok. Thank you.

1 Like

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