package main import ( "log" "path/filepath" "strconv" "time" "github.com/blevesearch/bleve" . "gopkg.in/check.v1" ) type BleveIndexerSuite struct { i bleve.Index } var _ = Suite(&BleveIndexerSuite{}) func (s *BleveIndexerSuite) SetUpSuite(c *C) { var err error start := time.Now() s.i, err = bleve.New(filepath.Join(c.MkDir(), "satbd-test.bar.satellite"), buildAlbumMapping()) c.Assert(err, IsNil) batch := s.i.NewBatch() for _, a := range albumsDataTest { c.Assert(batch.Index(AlbumIDString(a.ID), &a), IsNil) } c.Assert(s.i.Batch(batch), IsNil) log.Printf("Indexing took %s", time.Since(start)) } func (s *BleveIndexerSuite) TestCanSearch(c *C) { data := map[string]map[AlbumID]bool{ // Research by amny keyword (here in the description "frégate": map[AlbumID]bool{84448: true}, // Research by editor "éditeur:Dupuis": map[AlbumID]bool{8179: true, 9935: true, 160366: true}, // Research by Title "titre:\"Mortes saisons\"": map[AlbumID]bool{8179: true}, // Keyword can appear in both Title and Description "pour mourir": map[AlbumID]bool{44989: true, 84448: true, 160366: true, 19762: true}, // Or in the collection "écho savanes": map[AlbumID]bool{15875: true}, // May not find anything, but never fails "FOOOOOOOBAAAAAR": map[AlbumID]bool{}, // Or we can only look for collection field "collection:Dupuis": map[AlbumID]bool{160366: true}, // Maybe we are only inderested in scenarist "scenario:plantu": map[AlbumID]bool{58595: true}, // Or designer "dessins:berthet": map[AlbumID]bool{8179: true}, // or colorist, but Trondheim does not does color ;) "couleurs:trondheim": map[AlbumID]bool{}, // or any of that fields, yes Trondheim do draw some comics ;) "trondheim": map[AlbumID]bool{160366: true}, // or we just want to look up a reference "CHI-23": map[AlbumID]bool{46005: true}, // or more explicitely "ref:BERT-9": map[AlbumID]bool{8179: true}, // or we are just interested in good comics "Note:>4": map[AlbumID]bool{52100: true, 19762: true, 160366: true}, // or bad ones "Note:<2": map[AlbumID]bool{754: true, 32043: true}, } start := time.Now() for q, expected := range data { query := bleve.NewQueryStringQuery(q) search := bleve.NewSearchRequest(query) //make sure we can have all data at once search.Size = len(albumsDataTest) searchResults, err := s.i.Search(search) if c.Check(err, IsNil) == true { if c.Check(int(searchResults.Total), Equals, len(expected), Commentf("Query: %s", q)) == true { for _, resAlbum := range searchResults.Hits { aID, err := strconv.ParseUint(resAlbum.ID, 0, 64) c.Check(err, IsNil) _, ok := expected[AlbumID(aID)] c.Check(ok, Equals, true, Commentf("Query: %s, got %d instead of %v", q, resAlbum.ID, expected)) } } else { log.Printf("Got %d result(s)", len(searchResults.Hits)) for i, a := range searchResults.Hits { log.Printf(" - %d: %s", i, a.ID) } } } } log.Printf("%s: %s", c.TestName(), time.Since(start)) }