Add tags to indices for searching

This commit is contained in:
Justin Hawkins 2022-06-04 11:03:26 +09:30
parent 4cac81aab0
commit 2ba4066643
3 changed files with 50 additions and 3 deletions

View File

@ -70,5 +70,4 @@ will not work.
* delete
* sorting
* More tag options
* search for tags
* bookmarklet with pre-filled tags

View File

@ -131,8 +131,6 @@ func (m *BookmarkManager) Search(query string) ([]entity.Bookmark, error) {
}
}
// log.Printf("counts: %#v", counts)
for k, v := range counts {
if v == uint8(len(words)) {
rets = append(rets, k)
@ -157,6 +155,7 @@ func (m *BookmarkManager) ScrapeAndIndex(bm *entity.Bookmark) error {
}
words := content.Words(bm)
words = append(words, bm.Tags...)
log.Printf("index for %d %s (%d words)", bm.ID, bm.URL, len(words))
m.db.UpdateIndexForWordsByID(words, bm.ID)

View File

@ -92,3 +92,52 @@ func TestAddRemove(t *testing.T) {
}
}
func TestTagIndexing(t *testing.T) {
ts := newTestServer()
defer ts.Close()
serverResponse = "<p>the quick brown fox</p>"
db := DB{}
f, _ := os.CreateTemp("", "test_boltdb_*")
f.Close()
defer os.Remove(f.Name())
db.Open(f.Name())
bmm := NewBookmarkManager(&db)
bm := entity.Bookmark{URL: ts.URL}
err := bmm.AddBookmark(&bm)
if err != nil {
t.Fatalf("error adding: %s", err)
}
if bm.ID == 0 {
t.Error("bookmark did not get an id")
}
err = bmm.ScrapeAndIndex(&bm)
if err != nil {
t.Errorf("scrape index returned %s", err)
}
searchRes, err := bmm.Search("fox")
if err != nil {
t.Errorf("search returned %s", err)
}
if len(searchRes) != 1 {
t.Error("did not get one id")
}
// add a tag
bm.Tags = []string{"sloth"}
err = bmm.ScrapeAndIndex(&bm)
if err != nil {
t.Errorf("scrape index returned %s", err)
}
searchRes, err = bmm.Search("sloth")
if err != nil {
t.Errorf("search returned %s", err)
}
if len(searchRes) != 1 {
t.Error("did not get one id for sloth")
}
}