diff options
author | navanchauhan <navanchauhan@gmail.com> | 2023-01-18 02:41:10 -0700 |
---|---|---|
committer | navanchauhan <navanchauhan@gmail.com> | 2023-01-18 02:41:10 -0700 |
commit | 790ca38644b8c37c9feb7c433143a6b49b4f137e (patch) | |
tree | f809dc4ef486090648686a4c4769a08034a462d0 | |
parent | 46f32a7dc26dad46ffa974de56bf1541b8578e12 (diff) |
added pagination and results per page
-rw-r--r-- | definitions.go | 7 | ||||
-rw-r--r-- | handlerFuncs.go | 74 | ||||
-rw-r--r-- | main.go | 5 | ||||
-rw-r--r-- | templates/results.gtpl | 6 | ||||
-rw-r--r-- | templates/search.gtpl | 4 | ||||
-rw-r--r-- | templates/search_results.html | 14 |
6 files changed, 74 insertions, 36 deletions
diff --git a/definitions.go b/definitions.go index 2289865..6a7b9d1 100644 --- a/definitions.go +++ b/definitions.go @@ -3,7 +3,7 @@ package main type SearchQuery struct { Query string MaxResults int64 - Offset int64 + Page int64 } type BaseResponse struct { @@ -21,6 +21,11 @@ type SearchResponse struct { Offset int64 LastOffset int64 NumPages int + MaxResults int64 + CurPage int64 + ShowPrev bool + PrevPage int64 + NextPage int64 } type DocumentResponse struct { diff --git a/handlerFuncs.go b/handlerFuncs.go index d6542e0..01488c2 100644 --- a/handlerFuncs.go +++ b/handlerFuncs.go @@ -14,26 +14,41 @@ func searchHandler(w http.ResponseWriter, r *http.Request, index *meilisearch.In fmt.Println(r.Form) if r.Form["query"] != nil || r.FormValue("query") != "" { //fmt.Println("query:", r.Form["query"]) - var myOffset int64 - if r.Form["offset"] != nil { - offset, _ := strconv.ParseInt(r.FormValue("offset"), 10, 64) - myOffset = offset - if offset < 0 { - myOffset = 0 + var pageNo int64 + var maxHits int64 + if r.Form["page"] != nil { + convertedInt, _ := strconv.ParseInt(r.FormValue("page"), 10, 64) + if convertedInt > 0 { + pageNo = convertedInt + } else { + pageNo = 1 } } else { - offset := int64(0) - myOffset = offset + pageNo = 1 + } + if r.Form["maxHits"] != nil { + convertedInt, _ := strconv.ParseInt(r.FormValue("maxHits"), 10, 64) + if convertedInt > 0 { + maxHits = convertedInt + } else { + maxHits = 20 + } + } else { + maxHits = 20 + } + + if maxHits > 100 { + maxHits = 100 } query := SearchQuery{ Query: r.FormValue("query"), - MaxResults: 100, - Offset: myOffset, + MaxResults: maxHits, + Page: pageNo, } res, err := index.Search(query.Query, &meilisearch.SearchRequest{ - Limit: query.MaxResults, - Offset: query.Offset, + HitsPerPage: query.MaxResults, + Page: query.Page, AttributesToRetrieve: []string{ "title", "applicant", @@ -52,22 +67,39 @@ func searchHandler(w http.ResponseWriter, r *http.Request, index *meilisearch.In return } - numPages := pageCount(int(res.EstimatedTotalHits), int(query.MaxResults)) - searchTemplate.Execute(w, SearchResponse{ GlobalVars: globalVariables, Success: true, SearchResults: res.Hits, - NumResults: len(res.Hits) + int(query.Offset), - TotalResults: res.EstimatedTotalHits, - MoreResults: res.EstimatedTotalHits > query.MaxResults, + NumResults: int(res.TotalHits) - (int(res.TotalPages)-int(res.Page))*int(res.HitsPerPage), + TotalResults: res.TotalHits, + MoreResults: res.Page < res.TotalPages, OriginalQuery: query, - Offset: query.Offset + query.MaxResults, - LastOffset: query.Offset - query.MaxResults, - NumPages: numPages, + NumPages: int(res.TotalPages), + MaxResults: maxHits, + CurPage: res.Page, + ShowPrev: res.Page > 1, + PrevPage: res.Page - 1, + NextPage: res.Page + 1, }) + } else { - fmt.Println("query is empty") + // Return empty search results + searchTemplate.Execute(w, SearchResponse{ + GlobalVars: globalVariables, + Success: true, + SearchResults: []interface{}{}, + NumResults: 0, + TotalResults: 0, + MoreResults: false, + OriginalQuery: SearchQuery{}, + NumPages: 0, + MaxResults: 0, + CurPage: 0, + ShowPrev: false, + PrevPage: 0, + NextPage: 0, + }) } } @@ -5,7 +5,6 @@ import ( "fmt" "html/template" "io/fs" - "math" "net/http" "os" "strconv" @@ -31,10 +30,6 @@ func create_pdf_url(year string, knumber string) string { } } -func pageCount(total int, perPage int) int { - return int(math.Ceil(float64(total) / float64(perPage))) -} - func main() { err := godotenv.Load(".env") if err != nil { diff --git a/templates/results.gtpl b/templates/results.gtpl index 151a7d3..ce2b99d 100644 --- a/templates/results.gtpl +++ b/templates/results.gtpl @@ -28,9 +28,11 @@ </tr> {{ end }} </table> + {{if .ShowPrev }} + <a href="/classic/search?query={{.OriginalQuery.Query}}&page={{.PrevPage}}&maxHits={{.MaxResults}}"> <p>Previous Page</p></a> + {{ end }} {{ if .MoreResults }} - <a href="/classic/search?query={{.OriginalQuery.Query}}&offset={{.LastOffset}}"> <p>Previous Page</p></a> - <a href="/classic/search?query={{.OriginalQuery.Query}}&offset={{.Offset}}"> <p>Next Page</p></a> + <a href="/classic/search?query={{.OriginalQuery.Query}}&page={{.PrevPage}}&maxHits={{.MaxResults}}"> <p>Next Page</p></a> {{ end }} </body> </html>
\ No newline at end of file diff --git a/templates/search.gtpl b/templates/search.gtpl index b892f19..841b068 100644 --- a/templates/search.gtpl +++ b/templates/search.gtpl @@ -1,9 +1,9 @@ <!DOCTYPE html> <html> <body> - <h1>{{.GlobalVars.Name}}</h1> + <h1>{{.GlobalVars.Name}} - Classic Mode</h1> <form action="/classic/search" method="GET"> - <input type="text" name="query" value="{{.OriginalQuery.Query}}" placeholder="Search Query" spellcheck="false"> + <input type="text" name="query" placeholder="Search Query" spellcheck="false"> <input type="submit"> </form> </body> diff --git a/templates/search_results.html b/templates/search_results.html index 4c053ab..4e9b9a4 100644 --- a/templates/search_results.html +++ b/templates/search_results.html @@ -73,25 +73,28 @@ {{ end }} </section> - {{ if .MoreResults }} + <section class="section"> <div class="container"> <div class="columns"> + {{if .ShowPrev }} <div class="column"> <div class="field has-addons"> - <button class="button is-primary is-light is-medium is-fullwidth" href="/search?query={{.OriginalQuery.Query}}&offset={{.LastOffset}}" title="Previous Page"> + <a class="button is-primary is-light is-medium is-fullwidth" href="/search?query={{.OriginalQuery.Query}}&page={{.PrevPage}}&maxHits={{.MaxResults}}" title="Previous Page"> <span class="icon is-small"> <i class="fa-solid fa-left-long"></i> </span> <span>Previous Page</span> - </button> + </a> </div> </div> + {{ end }} + {{ if .MoreResults }} <div class="column"> <div class="field has-addons"> - <a class="button is-primary is-medium is-fullwidth" href="/search?query={{.OriginalQuery.Query}}&offset={{.Offset}}" title="Next Page"> + <a class="button is-primary is-medium is-fullwidth" href="/search?query={{.OriginalQuery.Query}}&page={{.NextPage}}&maxHits={{.MaxResults}}" title="Next Page"> <span> Next Page </span> @@ -100,10 +103,11 @@ </span> </a></div> </div> + {{ end }} </div></div> </section> - {{ end }} + <script> if (typeof navigator.serviceWorker !== 'undefined') { navigator.serviceWorker.register('/static/pwabuilder-sw.js') |