Skip to content

Commit

Permalink
added count and offset to search api (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
torniker authored and nilslice committed Mar 6, 2018
1 parent c2d1bb0 commit 4ce9a46
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
23 changes: 22 additions & 1 deletion system/api/search.go
Expand Up @@ -5,6 +5,7 @@ import (
"log"
"net/http"
"net/url"
"strconv"

"github.com/ponzu-cms/ponzu/system/db"
"github.com/ponzu-cms/ponzu/system/item"
Expand Down Expand Up @@ -42,8 +43,28 @@ func searchContentHandler(res http.ResponseWriter, req *http.Request) {
return
}

count, err := strconv.Atoi(qs.Get("count")) // int: determines number of posts to return (10 default, -1 is all)
if err != nil {
if qs.Get("count") == "" {
count = 10
} else {
res.WriteHeader(http.StatusInternalServerError)
return
}
}

offset, err := strconv.Atoi(qs.Get("offset")) // int: multiplier of count for pagination (0 default)
if err != nil {
if qs.Get("offset") == "" {
offset = 0
} else {
res.WriteHeader(http.StatusInternalServerError)
return
}
}

// execute search for query provided, if no index for type send 404
matches, err := search.TypeQuery(t, q)
matches, err := search.TypeQuery(t, q, count, offset)
if err == search.ErrNoIndex {
res.WriteHeader(http.StatusNotFound)
return
Expand Down
4 changes: 2 additions & 2 deletions system/search/search.go
Expand Up @@ -142,14 +142,14 @@ func DeleteIndex(id string) error {
// TypeQuery conducts a search and returns a set of Ponzu "targets", Type:ID pairs,
// and an error. If there is no search index for the typeName (Type) provided,
// db.ErrNoIndex will be returned as the error
func TypeQuery(typeName, query string) ([]string, error) {
func TypeQuery(typeName, query string, count, offset int) ([]string, error) {
idx, ok := Search[typeName]
if !ok {
return nil, ErrNoIndex
}

q := bleve.NewQueryStringQuery(query)
req := bleve.NewSearchRequest(q)
req := bleve.NewSearchRequestOptions(q, count, offset, false)
res, err := idx.Search(req)
if err != nil {
return nil, err
Expand Down

1 comment on commit 4ce9a46

@nilslice
Copy link
Contributor

@nilslice nilslice commented on 4ce9a46 Mar 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@torniker - I wanted to merge this in as-is, but I'll still comment here. We could also support the order param by switching on its strings.ToLower(offset) value for asc and desc, then using the existing range for desc, or in reverse for the asc direction. By default, Bleve has a desc sort order. I don't know if Bleve's ordering is done by time, but we would simply be reversing the order, which is what a user typically wants anyways.

This way both the content and search APIs have parity in their selection options.

What do you think?

Please sign in to comment.