summaryrefslogtreecommitdiff
path: root/handlerFuncs.go
diff options
context:
space:
mode:
authornavanchauhan <navanchauhan@gmail.com>2023-01-18 19:36:38 -0700
committernavanchauhan <navanchauhan@gmail.com>2023-01-18 19:36:38 -0700
commit708066b7998a43ca47f6537a1208492efed4e99c (patch)
tree6897f9fd68032e5798c5f906ce28eb678796e4dc /handlerFuncs.go
parent2d88576b4085b8226be11b412c979a6ccdd5cdb3 (diff)
added support for sorting
Diffstat (limited to 'handlerFuncs.go')
-rw-r--r--handlerFuncs.go74
1 files changed, 56 insertions, 18 deletions
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 {