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