# Scroll with script sorting return endless data

**URL:** <https://discuss.elastic.co/t/scroll-with-script-sorting-return-endless-data/208142>\
**Category:** Elasticsearch\
**Created:** [November 16, 2019, 1:57am UTC](https://discuss.elastic.co/t/scroll-with-script-sorting-return-endless-data/208142 "2019-11-16T01:57:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chen\_steven](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chen_steven/32/50311_2.png) [@chen\_steven](https://discuss.elastic.co/u/chen_steven)\
**Post date:** [November 16, 2019, 1:57am UTC](https://discuss.elastic.co/t/scroll-with-script-sorting-return-endless-data/208142/1 "2019-11-16T01:57:46Z")

</div>

ES 7.4.0  
Centos 7

ref github [issue](https://github.com/elastic/elasticsearch/issues/49146)

I would like to do a scroll operation for some data with the sorting of the abs value of the diff between publishTime VS. current system time.

In my opinion, the scroll might be:

1. find out and make a snapshot for the query conditions
2. cache them by the sort formula
3. return the data paged by size

but for this query, each time i query for scroll page by scroll id, es result has these 2 results:

1. data with messed up sequence
2. return the same data list

I think this issue should be caused by `System.currentTimeMillis()`? I don't know, maybe ?

```auto
{
    "query": {
"some filters"
},
    "sort":[
        {
            "_script":{
                "script":{
                    "inline":"Math.abs(doc['publishTime'] - System.currentTimeMillis())",
                    "lang":"painless"
                },
                "type":"number",
                "order":"desc"
            }
        }
    ]
}

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [November 16, 2019, 7:07am UTC](https://discuss.elastic.co/t/scroll-with-script-sorting-return-endless-data/208142/2 "2019-11-16T07:07:10Z")

</div>

I suspect that the full result set is not cached but rather the offsets into the result set. As you are using a parameter in the script that varies over time this probably gets re-evaluated. As you are returning the full result set, why not either just sort on publishTime or pass the system time in as a parameter defined at the start of the query instead?

---

<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 14, 2019, 7:07am UTC](https://discuss.elastic.co/t/scroll-with-script-sorting-return-endless-data/208142/3 "2019-12-14T07:07:20Z")

</div>

Closed on December 14, 2019, 7:07am UTC

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