From 708066b7998a43ca47f6537a1208492efed4e99c Mon Sep 17 00:00:00 2001 From: navanchauhan Date: Wed, 18 Jan 2023 19:36:38 -0700 Subject: added support for sorting --- handlerFuncs.go | 74 +++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 18 deletions(-) (limited to 'handlerFuncs.go') 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: "", - HighlightPostTag: "", - }) + 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: "", + HighlightPostTag: "", + 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: "", + HighlightPostTag: "", + }) } 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 { -- cgit v1.2.3