Fixed pager
This commit is contained in:
@@ -57,8 +57,7 @@ type Context struct {
|
||||
CachedTitle string
|
||||
CachedContent string
|
||||
|
||||
Matches uint64
|
||||
Duration time.Duration
|
||||
SearchTook time.Duration
|
||||
Results Results
|
||||
Pager *paginator.Paginator
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/james4k/fmatter"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/vcraescu/go-paginator"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -196,7 +197,9 @@ func (s *Server) SearchHandler() httprouter.Handle {
|
||||
return
|
||||
}
|
||||
|
||||
searchResults, err := s.indexer.Search(q)
|
||||
p := SafeParseInt(r.FormValue("p"), 1)
|
||||
|
||||
searchResults, err := s.indexer.Search(q, p)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("error performing search")
|
||||
ctx.Error = true
|
||||
@@ -220,10 +223,13 @@ func (s *Server) SearchHandler() httprouter.Handle {
|
||||
results = append(results, result)
|
||||
}
|
||||
|
||||
pager := paginator.New(IntAdapter{N: int(searchResults.Total)}, s.config.ResultsPerPage)
|
||||
pager.SetPage(p)
|
||||
|
||||
ctx.Pager = &pager
|
||||
ctx.SearchQuery = q
|
||||
ctx.Results = results
|
||||
ctx.Matches = searchResults.Total
|
||||
ctx.Duration = searchResults.Took.Truncate(time.Nanosecond)
|
||||
ctx.SearchTook = searchResults.Took.Truncate(time.Nanosecond)
|
||||
|
||||
s.render("search", w, ctx)
|
||||
}
|
||||
|
||||
@@ -10,10 +10,11 @@ import (
|
||||
|
||||
type Indexer interface {
|
||||
Index(entry *Entry) error
|
||||
Search(q string) (*bleve.SearchResult, error)
|
||||
Search(q string, p int) (*bleve.SearchResult, error)
|
||||
}
|
||||
|
||||
type indexer struct {
|
||||
conf *Config
|
||||
idx bleve.Index
|
||||
}
|
||||
|
||||
@@ -36,18 +37,20 @@ func NewIndexer(conf *Config) (Indexer, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &indexer{idx: idx}, nil
|
||||
return &indexer{conf: conf, idx: idx}, nil
|
||||
}
|
||||
|
||||
func (i *indexer) Index(entry *Entry) error {
|
||||
return i.idx.Index(entry.Hash(), entry)
|
||||
}
|
||||
|
||||
func (i *indexer) Search(q string) (*bleve.SearchResult, error) {
|
||||
func (i *indexer) Search(q string, p int) (*bleve.SearchResult, error) {
|
||||
query := bleve.NewMatchQuery(q)
|
||||
searchRequest := bleve.NewSearchRequest(query)
|
||||
searchRequest.Fields = []string{"URL", "Title", "Author", "Summary", "Length"}
|
||||
searchRequest.Highlight = bleve.NewHighlight()
|
||||
searchRequest.Size = i.conf.ResultsPerPage
|
||||
searchRequest.From = searchRequest.Size * p
|
||||
searchResults, err := i.idx.Search(searchRequest)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("error searching index")
|
||||
|
||||
8
internal/paginator_adapters.go
Normal file
8
internal/paginator_adapters.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package internal
|
||||
|
||||
type IntAdapter struct {
|
||||
N int
|
||||
}
|
||||
|
||||
func (a IntAdapter) Nums() int { return a.N }
|
||||
func (a IntAdapter) Slice(offset, length int, data interface{}) error { return nil }
|
||||
@@ -1,24 +1,24 @@
|
||||
{{ define "pager" }}
|
||||
{{ if .HasPages }}
|
||||
{{ if $.Pager.HasPages }}
|
||||
<nav class="pagination-nav">
|
||||
<ul>
|
||||
<li>
|
||||
{{ if .HasPrev }}
|
||||
<a href="?p={{ .PrevPage }}">Prev</a>
|
||||
{{ if $.Pager.HasPrev }}
|
||||
<a href="?q={{ $.SearchQuery }}&p={{ $.Pager.PrevPage }}">Prev</a>
|
||||
{{ else }}
|
||||
<a href="#" data-tooltip="No previous page">Prev</a>
|
||||
<a href="#" data-tooltip="No more results">Prev</a>
|
||||
{{ end }}
|
||||
</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><small>Page {{ .Page }}/{{ .PageNums }} of {{ .Nums }} Results</small></li>
|
||||
<li><small>Viewing page {{ $.Pager.Page }}/{{ $.Pager.PageNums }} of {{ $.Pager.Nums }} results (Search took: {{ $.SearchTook }})</small></li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>
|
||||
{{ if .HasNext }}
|
||||
<a href="?p={{ .NextPage }}">Next</a>
|
||||
{{ if $.Pager.HasNext }}
|
||||
<a href="?q={{ $.SearchQuery }}&p={{ $.Pager.NextPage }}">Next</a>
|
||||
{{ else }}
|
||||
<a href="#" data-tooltip="No next page">Next</a>
|
||||
<a href="#" data-tooltip="No more results">Next</a>
|
||||
{{ end }}
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
{{ template "pager" (dict "Pager" $.Pager "SearchQuery" $.SearchQuery "SearchTook" $.SearchTook) }}
|
||||
<div class="container">
|
||||
<p>Found {{ $.Matches }} matches in {{ $.Duration }}</p>
|
||||
{{ with $.Results }}
|
||||
{{ range . }}
|
||||
<article>
|
||||
@@ -22,4 +22,5 @@
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
</div>
|
||||
{{ template "pager" (dict "Pager" $.Pager "SearchQuery" $.SearchQuery) }}
|
||||
{{end}}
|
||||
|
||||
Reference in New Issue
Block a user