# Pagination - calculating the page number

**URL:** https://discuss.elastic.co/t/pagination-calculating-the-page-number/775
**Category:** Elasticsearch
**Created:** [May 15, 2015, 9:33pm UTC](https://discuss.elastic.co/t/pagination-calculating-the-page-number/775 "2015-05-15T21:33:38Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![bbehling](https://avatars.discourse-cdn.com/v4/letter/b/f1d935/32.png) [@bbehling](https://discuss.elastic.co/u/bbehling)
#### Post date: [May 15, 2015, 9:33pm UTC](https://discuss.elastic.co/t/pagination-calculating-the-page-number/775/1 "2015-05-15T21:33:38Z")

</div>

Hello. I posted this over at the old mailing list, but thought I'd try here and the new forum as well....

I have issue where I'm retying to calculate the page number of a given record. I can do this using a script field

"script\_fields":{

```
  "pageNum":{

        "script":"incId++; floor(incId/10)",

        "params":{

              "incId":0

        }

  }

```

}

After calculating this field, then I need to return only the record that I am searching for. So something like this -

{

"query": {

```
"filtered": {

  "filter": {

    "and": [

      {

        "or": [

          {

            "terms": {

              "counties": [

                "05001"

              ]

            }

          }

        ]

      }

    ]

  }

}

```

},

"script\_fields":{

```
  "pageNum":{

        "script":"incId++; floor(incId/10)",

        "params":{

              "incId":0

        }

  }

```

},

"filter":{

```
  "script":{

       "script":"doc['id'].value == 28796"

  }

```

},

"size":100

}

The thought is that ES will calculate the pageNum field for all  
documents that match the given county value, then run the script filter  
to only return the document that matches the ID.

This returns the document I'm looking for, but the pageNum is 0.  
It appears ES is running the script\_field script after it runs the  
filter script.

What are some other ways I can pass in a query along with an ID,  
and get the correct page, along with all the other documents for that  
page, that a particular document is in?

I realize I can just calculate the page number for each document  
in the filter query, then post process the results from the application  
making this request. However, that is efficient and I would prefer to  
accomplish all this within ES.

Is it possible to somehow cache the results of the filter query  
looking for all documents matching the county value, run another query  
to find the ID, get that page number, then run a third query to return  
all documents that have that page number, and do all this from within  
ES?

Being able to calculate a field on an entire result set, then  
being able to filter on that dynamically created field would be awesome,  
especially if trying to calculate some kind of frequency or trend  
statistic.

---

<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: [July 6, 2017, 12:13am UTC](https://discuss.elastic.co/t/pagination-calculating-the-page-number/775/2 "2017-07-06T00:13:47Z")

</div>


