Pagination - calculating the page number


(brian) #1

Hello.

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.


(system) #2