code for a search engine front end
I am implementing lucene on my site, i wrote some code that shows something like this:
displaying results 11-20 of 3434
1. url / description
2. url / description
....
page 1, 2, _3_, 4 .. 25 [next] [prev]
for some reason i found this not so easy to write as i thought. calculating all those ranges etc. (the results per page is user defined). Now i am looking at my code and it seems ugly.
so im wondering if anyone knows of an example implementation of that in java / jsp?
i looked at nutch but its search front end is a mess of jsp java includes and is plain ugly.
[623 byte] By [
mkoryaka] at [2007-11-26 12:20:11]

# 2
> for some reason i found this not so easy to write as
> i thought. calculating all those ranges etc. (the
> results per page is user defined).
You might just be able to do some refactoring and remove the uglyiness?
What is giving you trouble with the calculations?
When I have seen it done before, all you really need to know is the number of results per page, the current page, and the total number of results.
# 3
here is the code that figures out the first and last result on the page based on how many results are found, how many are on a page, and what page we are on.
totalResults = results.length();
double totalPagesDbl = ((double)totalResults)/((double)resultsPerPage);
totalPages = (int)Math.ceil(totalPagesDbl);
if(currentPage >= totalPages) {
currentPage = totalPages;
}
resultsPerPage = (resultsPerPage <= totalResults)?resultsPerPage:totalResults;//how many on this page
endingResult = resultsPerPage * currentPage;
if(endingResult > totalResults) {
startingResult = endingResult - resultsPerPage;
endingResult = totalResults;
} else {
startingResult = endingResult - resultsPerPage;
}
if(startingResult > endingResult) {
startingResult = endingResult -1;
}
it used to be worse, i rewrote it