diff --git a/internal/webserver/controller/document/search.go b/internal/webserver/controller/document/search.go index 1232629..0b2fa3b 100644 --- a/internal/webserver/controller/document/search.go +++ b/internal/webserver/controller/document/search.go @@ -58,28 +58,30 @@ func (d *Controller) Search(c *fiber.Ctx) error { return fiber.ErrInternalServerError } - highlights, err := d.hlRepository.Highlights(int(session.ID), page, 6) + docsSortedByHighlightedDate, err := d.hlRepository.Highlights(int(session.ID), page, 6) if err != nil { return fiber.ErrInternalServerError } - docs, err := d.idx.Documents(highlights.Hits()) + docs, err := d.idx.Documents(docsSortedByHighlightedDate.Hits()) if err != nil { return fiber.ErrInternalServerError } - docsSortedByHighlightedDate := make([]index.Document, len(docs)) - i := 0 - for path := range docs { - docsSortedByHighlightedDate[i] = docs[path] - docsSortedByHighlightedDate[i].Highlighted = true - i++ + highlights := make([]index.Document, 0, len(docs)) + for _, path := range docsSortedByHighlightedDate.Hits() { + if _, ok := docs[path]; !ok { + continue + } + doc := docs[path] + doc.Highlighted = true + highlights = append(highlights, doc) } return c.Render("index", fiber.Map{ "Count": count, "Title": "Coreander", - "Highlights": docsSortedByHighlightedDate, + "Highlights": highlights, "EmailSendingConfigured": emailSendingConfigured, "EmailFrom": d.sender.From(), }, "layout") diff --git a/internal/webserver/controller/highlight/highlights.go b/internal/webserver/controller/highlight/highlights.go index 0cfa0a2..3534d34 100644 --- a/internal/webserver/controller/highlight/highlights.go +++ b/internal/webserver/controller/highlight/highlights.go @@ -40,30 +40,31 @@ func (h *Controller) Highlights(c *fiber.Ctx) error { return fiber.ErrNotFound } - highlights, err := h.hlRepository.Highlights(int(user.ID), page, model.ResultsPerPage) + docsSortedByHighlightedDate, err := h.hlRepository.Highlights(int(user.ID), page, model.ResultsPerPage) if err != nil { return fiber.ErrInternalServerError } - docs, err := h.idx.Documents(highlights.Hits()) + docs, err := h.idx.Documents(docsSortedByHighlightedDate.Hits()) if err != nil { return fiber.ErrInternalServerError } - docsSortedByHighlightedDate := make([]index.Document, len(docs)) - - i := 0 - for path := range docs { - docsSortedByHighlightedDate[i] = docs[path] - docsSortedByHighlightedDate[i].Highlighted = true - i++ + highlights := make([]index.Document, 0, len(docs)) + for _, path := range docsSortedByHighlightedDate.Hits() { + if _, ok := docs[path]; !ok { + continue + } + doc := docs[path] + doc.Highlighted = true + highlights = append(highlights, doc) } paginatedResults := result.NewPaginated[[]index.Document]( model.ResultsPerPage, page, - highlights.TotalHits(), - docsSortedByHighlightedDate, + docsSortedByHighlightedDate.TotalHits(), + highlights, ) return c.Render("highlights", fiber.Map{