Skip to content

Commit

Permalink
Search by multiple terms
Browse files Browse the repository at this point in the history
Narrow search query by providing several search terms to filter by.
  • Loading branch information
msp301 committed May 31, 2023
1 parent 689f0d2 commit 1fa79ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (
var searchCmd = &cobra.Command{
Use: "search",
Short: "Search notes",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
renderResults(book().Search(args[0]))
renderResults(book().Search(args...))
},
}

Expand Down
20 changes: 15 additions & 5 deletions notebook/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
)

func (book *Notebook) Search(query string) []Result {
func (book *Notebook) Search(query ...string) []Result {
results := []Result{}
traversal := graph.Traversal(book.Notes)
for vertex := range traversal.V().HasLabel("note").Iterate() {
Expand All @@ -17,12 +17,22 @@ func (book *Notebook) Search(query string) []Result {
switch val := vertex.Properties["Value"].(type) {
case parser.Note:
paragraphs := extractParagraphs(val.Content)
PARAGRAPH:
for _, paragraph := range paragraphs {
if matches(paragraph, query) {
context = append(context, paragraph)
matched = true
break
termsMatched := 0
for _, term := range query {
if matches(paragraph, term) {
termsMatched++
}
}

if termsMatched != len(query) {
continue PARAGRAPH
}

context = append(context, paragraph)
matched = true
break
}

if !matched {
Expand Down

0 comments on commit 1fa79ae

Please sign in to comment.