Hervorhebung der gesuchten Begriffe in der Volltextsuche eingebaut

This commit is contained in:
Sven Schmalle 2019-12-24 13:37:28 +01:00
parent 70ae601fb2
commit db17ac42be
2 changed files with 47 additions and 5 deletions

43
main.go
View File

@ -15,6 +15,7 @@ import (
"time" "time"
"github.com/blevesearch/bleve" "github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/search/highlight/highlighter/html"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/mandolyte/mdtopdf" "github.com/mandolyte/mdtopdf"
"gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4"
@ -87,7 +88,14 @@ func main() {
if !info.IsDir() && !strings.HasPrefix(tmpPath, ".git") { if !info.IsDir() && !strings.HasPrefix(tmpPath, ".git") {
b, err := ioutil.ReadFile(path.Join(config.DataPath, tmpPath)) b, err := ioutil.ReadFile(path.Join(config.DataPath, tmpPath))
check(err) check(err)
err = index.Index(tmpPath, string(b))
indexData := struct {
Text string
}{
string(b),
}
err = index.Index(tmpPath, indexData)
check(err) check(err)
fmt.Println("Indexed: " + info.Name()) fmt.Println("Indexed: " + info.Name())
} }
@ -99,6 +107,29 @@ func main() {
} }
} }
/*
repo, err := git.PlainOpen(config.DataPath)
check(err)
ref, err := repo.Head()
check(err)
// ... retrieves the commit history
//since := time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC)
//until := time.Date(2019, 7, 30, 0, 0, 0, 0, time.UTC)
var filename = "playground.md"
cIter, err := repo.Log(&git.LogOptions{From: ref.Hash(), FileName: &filename}) //, Since: &since, Until: &until})
check(err)
// ... just iterates over the commits, printing it
err = cIter.ForEach(func(c *object.Commit) error {
fmt.Println(c.Files())
fmt.Println(c.Committer.When)
return nil
})
*/
router := mux.NewRouter() router := mux.NewRouter()
router.HandleFunc("/_api/md/{pagename:.*}", getRawPage).Methods("GET") router.HandleFunc("/_api/md/{pagename:.*}", getRawPage).Methods("GET")
router.HandleFunc("/_api/pdf/{pagename:.*}", getPDFPage).Methods("GET") router.HandleFunc("/_api/pdf/{pagename:.*}", getPDFPage).Methods("GET")
@ -259,7 +290,7 @@ func getFTS(w http.ResponseWriter, r *http.Request) {
query := bleve.NewQueryStringQuery(pSearchterm) query := bleve.NewQueryStringQuery(pSearchterm)
search := bleve.NewSearchRequest(query) search := bleve.NewSearchRequest(query)
search.Highlight = bleve.NewHighlight() search.Highlight = bleve.NewHighlightWithStyle(html.Name)
search.Size = 30 search.Size = 30
searchResults, err := index.Search(search) searchResults, err := index.Search(search)
index.Close() index.Close()
@ -389,7 +420,13 @@ func BleveIndex(PageName string) {
b, err := ioutil.ReadFile(path.Join(config.DataPath, PageName)) b, err := ioutil.ReadFile(path.Join(config.DataPath, PageName))
check(err) check(err)
err = index.Index(PageName, string(b))
indexData := struct {
Text string
}{
string(b),
}
err = index.Index(PageName, indexData)
check(err) check(err)
index.Close() index.Close()
} }

View File

@ -94,7 +94,10 @@
list-style-type: square; list-style-type: square;
} }
mark {
background-color: lightblue;
color: black;
}
</style> </style>
</head> </head>
<body> <body>
@ -261,7 +264,9 @@
content.forEach(function (item, index) { content.forEach(function (item, index) {
console.log(item.id); console.log(item.id);
var page = item.id.replace(/.md/, ''); var page = item.id.replace(/.md/, '');
$("#outputdiv").append("<a href='/"+page+"'>/"+page+"</a><br>"); $("#outputdiv").append((index+1)+". Seite: <a href='/"+page+"'>/"+page+"</a><br>");
$("#outputdiv").append(item.fragments.Text+"<hr>");
}); });
} }
}); });