Display num of results when searching/filtering tags
Merge request reports
Activity
I believe this isn't the completely correct way to do it because the results are limited to the amount of topics that are paginated. So if you set the query parameter
per_page=1
and try the search again, it will show as "1 result" with a "Next" button at the bottom.Ideally it would show in total how many there are, so if there are 1000 topics matching the search but only 20 are shown on the page, it still says "1000 results".
Thanks, I missed that the queries were performed on a paginated basis, instead of paginating the full set of results after.
To fix it, I introduced a method to remove all before and after restrictions from the query and use it to retrieve the total number of results. I think it's the cleanest way to do it, rather than trying to keep track of the page number.
I'm not too familiar with sqlalchemy and Pyramid so please let me know if I neglected anything.
Edited by kenc- Resolved by Deimos
229 236 self.has_next_page = False 230 237 self.has_prev_page = False 231 238 239 # fetch total number of results without pagination 240 self.total_count = ( 241 query.request.db_session.query(func.count()) 242 .select_from(query.remove_before_after().statement) The SQL query this produces looks good and should be efficient, but I think this is going to actually modify the query by removing the before/after restrictions, right?
That feels wrong to me - if you do anything that generates results (such as calling
.get_page()
on the query), if you were to use the query again afterwards it would produce different results. Is there some way that this could work that doesn't actually modify the query? For example, it might be possible to make a copy of it somehow and only modify the copy so it doesn't alter the original query (I don't know offhand how to do this).
229 236 self.has_next_page = False 230 237 self.has_prev_page = False 231 238 239 # fetch total number of results without pagination 240 self.total_count = ( I think it would be best for this to be done in a separate function that needs to be called explicitly when you're going to use the count of results. Currently, everything that goes through
PaginatedResults
(including its subclasses) will start doing an extra query to fetch the count of results, but it's not used or displayed in almost all cases. That means that many pages (user pages, notifications page, all topic listings, etc.) will increase their database traffic for no reason, so we should only do this extra query when it's actually needed.