Add link operator to search
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-10-31 00:29:41 +01:00
parent 39dd45b65d
commit 47d504903d
2 changed files with 31 additions and 6 deletions

View File

@ -954,6 +954,10 @@ func createSearchIndex(dataDir, indexName string) (bleve.Index, error) {
titleFieldMapping.Store = true
documentMapping.AddFieldMappingsAt("title", titleFieldMapping)
linkFieldMapping := bleve.NewTextFieldMapping()
linkFieldMapping.Store = true
documentMapping.AddFieldMappingsAt("link", linkFieldMapping)
indexMapping.AddDocumentMapping("block", documentMapping)
searchIndex, err := bleve.New(indexDir, indexMapping)

View File

@ -199,6 +199,8 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
q := bleve.NewQueryStringQuery(r.URL.Query().Get("q"))
sr := bleve.NewSearchRequest(q)
sr.IncludeLocations = false
sr.Size = 25
sr.Fields = []string{"page", "title", "text"}
sr.Highlight = bleve.NewHighlightWithStyle("html")
sr.Highlight.AddField("text")
@ -219,6 +221,7 @@ type pageBlock struct {
Title string `json:"title"`
Page string `json:"page"`
Text string `json:"text"`
Link string `json:"link"`
}
func (p pageBlock) Type() string {
@ -239,12 +242,30 @@ func createSearchObjects(rootBlockID string) ([]pageBlock, error) {
current := queue[0]
queue = queue[1:]
links, err := ParseLinks(current, blocks.Texts[current])
if err != nil {
continue
}
if len(links) == 0 {
pageBlocks = append(pageBlocks, pageBlock{
ID: current,
Title: blocks.Texts[blocks.PageID],
Page: blocks.PageID,
Text: blocks.Texts[current],
Link: "",
})
} else {
for _, link := range links {
pageBlocks = append(pageBlocks, pageBlock{
ID: current,
Title: blocks.Texts[blocks.PageID],
Page: blocks.PageID,
Text: blocks.Texts[current],
Link: link.Name,
})
}
}
queue = append(queue, blocks.Children[current]...)
}