Add reset for search (and fields)
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-07-12 18:01:32 +02:00
parent e5e388d61e
commit 376ad6b625
2 changed files with 120 additions and 8 deletions

View File

@ -138,9 +138,11 @@ func (fp *FilePages) save(msg saveMessage) error {
if err != nil {
return fmt.Errorf("while creating search object %s: %w", page.Name, err)
}
err = fp.index.Index(page.Name, so)
if err != nil {
return fmt.Errorf("while indexing %s: %w", page.Name, err)
if fp.index != nil {
err = fp.index.Index(page.Name, so)
if err != nil {
return fmt.Errorf("while indexing %s: %w", page.Name, err)
}
}
sw.Stop()
sw.Start("links")

120
search.go
View File

@ -5,6 +5,7 @@ import (
"fmt"
"log"
"net/http"
"os"
"strings"
"github.com/blevesearch/bleve"
@ -17,14 +18,14 @@ import (
// TODO: search(query) command
type searchHandler struct {
documents PagesRepository
indexMapping mapping.IndexMapping
searchIndex bleve.Index
}
type nameLine struct {
Name string `json:"name"`
Line string `json:"line"`
Name string `json:"name"`
Title string `json:"title"`
Line string `json:"line"`
}
type searchObject struct {
@ -58,6 +59,114 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
if r.URL.Query().Get("fields") == "1" {
fields, err := s.searchIndex.Fields()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
err = enc.Encode(&fields)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
return
} else if r.Method == "GET" && r.URL.Query().Get("reset") == "1" {
w.Header().Add("Content-Type", "text/html")
fmt.Fprint(w, `<form action="/search/" method="post"><input type=hidden name=reset value=1><input type=submit value="Reset Index"></form>`)
return
} else if r.Method == "POST" {
err = r.ParseForm()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
if r.PostForm.Get("reset") == "1" {
refs := make(Refs)
mp := NewFilePages("data", nil)
pages, err := mp.AllPages()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
for _, page := range pages {
err = processBackrefsForPage(page, refs)
if err != nil {
log.Println("error while processing backrefs: ", err)
continue
}
}
err = saveBackrefs("data/backrefs.json", refs)
if err != nil {
log.Printf("error while saving backrefs %w", err)
http.Error(w, err.Error(), 500)
return
}
err = os.RemoveAll("data/_tmp_index")
if err != nil {
log.Printf("error while remove old index %w", err)
http.Error(w, err.Error(), 500)
return
}
indexMapping := bleve.NewIndexMapping()
index, err := bleve.New("data/_tmp_index", indexMapping)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
for _, page := range pages {
so, err := createSearchObject(page)
if err != nil {
log.Printf("error while createing search object %s: %w", page.Title, err)
continue
}
err = index.Index(page.Name, so)
if err != nil {
log.Printf("error while indexing %s: %w", page.Title, err)
continue
}
}
err = os.Rename("data/_page-index", "data/_page-index-old")
if err != nil {
log.Printf("error while resetting index: %w", err)
http.Error(w, err.Error(), 500)
return
}
err = os.Rename("data/_tmp_index", "data/_page-index")
if err != nil {
log.Printf("error while putthing new index in place: %w", err)
http.Error(w, err.Error(), 500)
return
}
err = os.RemoveAll("data/_page-index-old")
if err != nil {
log.Printf("error while remove old index %w", err)
http.Error(w, err.Error(), 500)
return
}
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
err = enc.Encode(struct {
Ok bool `json:"ok"`
}{Ok: true})
if err != nil {
http.Error(w, err.Error(), 500)
return
}
}
return
}
q := bleve.NewQueryStringQuery(r.URL.Query().Get("q"))
sr := bleve.NewSearchRequest(q)
results, err := s.searchIndex.Search(sr)
@ -94,10 +203,11 @@ func createSearchObject(page Page) (searchObject, error) {
}
}
for k, refs := range page.Refs {
for _, refs := range page.Refs {
for _, ref := range refs {
so.Refs = append(so.Refs, nameLine{
k,
ref.Name,
ref.Title,
ref.Line,
})
}