diff options
-rw-r--r-- | definitions.go | 1 | ||||
-rw-r--r-- | handlerFuncs.go | 74 | ||||
-rw-r--r-- | templates/search_results.html | 14 |
3 files changed, 64 insertions, 25 deletions
diff --git a/definitions.go b/definitions.go index 6a7b9d1..c9c0c7c 100644 --- a/definitions.go +++ b/definitions.go @@ -26,6 +26,7 @@ type SearchResponse struct { ShowPrev bool PrevPage int64 NextPage int64 + Sort string } type DocumentResponse struct { diff --git a/handlerFuncs.go b/handlerFuncs.go index 01488c2..dae3bfb 100644 --- a/handlerFuncs.go +++ b/handlerFuncs.go @@ -16,6 +16,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request, index *meilisearch.In //fmt.Println("query:", r.Form["query"]) var pageNo int64 var maxHits int64 + var sortBy string if r.Form["page"] != nil { convertedInt, _ := strconv.ParseInt(r.FormValue("page"), 10, 64) if convertedInt > 0 { @@ -37,6 +38,23 @@ func searchHandler(w http.ResponseWriter, r *http.Request, index *meilisearch.In maxHits = 20 } + if r.Form["sort"] != nil { + if r.FormValue("sort") == "subdesc" { + sortBy = "submission_date_timestamp:desc" + } else if r.FormValue("sort") == "subasc" { + sortBy = "submission_date_timestamp:asc" + } else if r.FormValue("sort") == "decasc" { + sortBy = "decision_date_timestamp:asc" + } else if r.FormValue("sort") == "decdesc" { + sortBy = "decision_date_timestamp:desc" + } else { + sortBy = "" + } + + } else { + sortBy = "" + } + if maxHits > 100 { maxHits = 100 } @@ -46,25 +64,44 @@ func searchHandler(w http.ResponseWriter, r *http.Request, index *meilisearch.In Page: pageNo, } - res, err := index.Search(query.Query, &meilisearch.SearchRequest{ - HitsPerPage: query.MaxResults, - Page: query.Page, - AttributesToRetrieve: []string{ - "title", - "applicant", - "submission_date", - "predicates", - "id", - }, - AttributesToCrop: []string{"full_text"}, - AttributesToHighlight: []string{"full_text"}, - HighlightPreTag: "<mark>", - HighlightPostTag: "</mark>", - }) + var res *meilisearch.SearchResponse + + fmt.Println("sort:", sortBy) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return + if sortBy != "" { + res, _ = index.Search(query.Query, &meilisearch.SearchRequest{ + HitsPerPage: query.MaxResults, + Page: query.Page, + AttributesToRetrieve: []string{ + "title", + "applicant", + "submission_date", + "predicates", + "id", + }, + AttributesToCrop: []string{"full_text"}, + AttributesToHighlight: []string{"full_text"}, + HighlightPreTag: "<mark>", + HighlightPostTag: "</mark>", + Sort: []string{ + sortBy}, + }) + } else { + res, _ = index.Search(query.Query, &meilisearch.SearchRequest{ + HitsPerPage: query.MaxResults, + Page: query.Page, + AttributesToRetrieve: []string{ + "title", + "applicant", + "submission_date", + "predicates", + "id", + }, + AttributesToCrop: []string{"full_text"}, + AttributesToHighlight: []string{"full_text"}, + HighlightPreTag: "<mark>", + HighlightPostTag: "</mark>", + }) } searchTemplate.Execute(w, SearchResponse{ @@ -81,6 +118,7 @@ func searchHandler(w http.ResponseWriter, r *http.Request, index *meilisearch.In ShowPrev: res.Page > 1, PrevPage: res.Page - 1, NextPage: res.Page + 1, + Sort: r.FormValue("sort"), }) } else { diff --git a/templates/search_results.html b/templates/search_results.html index 4e9b9a4..9060025 100644 --- a/templates/search_results.html +++ b/templates/search_results.html @@ -37,11 +37,11 @@ </section> <div class="container is-fluid"> <a href="#" class="button is-link" disabled>Export Results</a> - <a href="#" class="button is-link" disabled>Sort by Decision Date (ASC) </a> - <a href="#" class="button is-link" disabled>Sort by Decision Date (DESC) </a> - <a href="#" class="button is-link" disabled>Sort by Submission Date (ASC) </a> - <a href="#" class="button is-link" disabled>Sort by Submission Date (DESC) </a> - <a href="#" class="button is-link" disabled>Sort by Relevancy (Default) </a> + <a href="/search?query={{.OriginalQuery.Query}}&maxHits={{.MaxResults}}&sort=subasc" class="button is-link">Sort by Submission Date (ASC) </a> + <a href="/search?query={{.OriginalQuery.Query}}&maxHits={{.MaxResults}}&sort=subdesc" class="button is-link">Sort by Submission Date (DESC) </a> + <a href="/search?query={{.OriginalQuery.Query}}&maxHits={{.MaxResults}}&sort=decasc" class="button is-link">Sort by Decision Date (ASC) </a> + <a href="/search?query={{.OriginalQuery.Query}}&maxHits={{.MaxResults}}&sort=decdesc" class="button is-link">Sort by Decision Date (DESC) </a> + <a href="/search?query={{.OriginalQuery.Query}}&maxHits={{.MaxResults}}" class="button is-link">Sort by Relevancy (Default) </a> </div> <section class="section"> @@ -81,7 +81,7 @@ <div class="column"> <div class="field has-addons"> - <a class="button is-primary is-light is-medium is-fullwidth" href="/search?query={{.OriginalQuery.Query}}&page={{.PrevPage}}&maxHits={{.MaxResults}}" title="Previous Page"> + <a class="button is-primary is-light is-medium is-fullwidth" href="/search?query={{.OriginalQuery.Query}}&page={{.PrevPage}}&maxHits={{.MaxResults}}&sort={{.Sort}}" title="Previous Page"> <span class="icon is-small"> <i class="fa-solid fa-left-long"></i> @@ -94,7 +94,7 @@ {{ if .MoreResults }} <div class="column"> <div class="field has-addons"> - <a class="button is-primary is-medium is-fullwidth" href="/search?query={{.OriginalQuery.Query}}&page={{.NextPage}}&maxHits={{.MaxResults}}" title="Next Page"> + <a class="button is-primary is-medium is-fullwidth" href="/search?query={{.OriginalQuery.Query}}&page={{.NextPage}}&maxHits={{.MaxResults}}&sort={{.Sort}}" title="Next Page"> <span> Next Page </span> |