Implements the search part

This commit is contained in:
2016-01-26 12:31:03 +01:00
parent 5f2208839d
commit f6a884b188
6 changed files with 100 additions and 15 deletions

15
main.go
View File

@@ -57,13 +57,14 @@ func newAppData(opts Options) (*appData, error) {
res := &appData{
opts: opts,
errors: make(chan error, 10),
errors: make(chan error),
}
blevePath := filepath.Join(basepath, "index")
res.index, err = bleve.Open(blevePath)
if err == bleve.ErrorIndexPathDoesNotExist {
log.Printf("Creating a new index in %s", blevePath)
res.index, err = bleve.New(blevePath, buildAlbumMapping())
if err != nil {
return nil, err
@@ -151,8 +152,8 @@ func (a *appData) readCsv(stopChan <-chan struct{},
func (a *appData) submitBatchToIndex(batch *bleve.Batch) {
s := batch.Size()
log.Printf("[INDEX] start indexing of %d albums", s)
err := a.index.Batch(batch)
start := time.Now()
err := a.index.Batch(batch)
if err != nil {
a.errors <- fmt.Errorf("[INDEX] batch indexing failed: %s", err)
return
@@ -181,12 +182,13 @@ func (a *appData) indexAlbum(stopChan <-chan struct{},
break
}
}
err := batch.Index(AlbumIDString(album.ID), a)
err := batch.Index(AlbumIDString(album.ID), album)
if err != nil {
a.errors <- fmt.Errorf("[INDEX] could not batch indexion of %s: %s", album, err)
break
} else {
count++
}
count++
if count%a.opts.BatchSize == 0 {
a.submitBatchToIndex(batch)
batch = a.index.NewBatch()
@@ -196,7 +198,7 @@ func (a *appData) indexAlbum(stopChan <-chan struct{},
albumToReIndex = nil
break
}
err := a.index.Index(AlbumIDString(album.ID), a)
err := a.index.Index(AlbumIDString(album.ID), album)
if err != nil {
a.errors <- fmt.Errorf("[INDEX] re-indexing %s failed: %s", album, err)
}
@@ -215,6 +217,7 @@ func (a *appData) indexAlbum(stopChan <-chan struct{},
}
func (a *appData) cacheAlbumDescription(getAlbum <-chan *Album, toIndex chan<- *Album) {
defer close(toIndex)
nbAlbums := 0
for album := range getAlbum {
nbAlbums++
@@ -323,7 +326,7 @@ func (a *appData) updateCache(stopChan <-chan struct{}, periode time.Duration, c
func (a *appData) logErrors() {
for e := range a.errors {
log.Printf("%s", e)
log.Printf("[errors] ]%s", e)
}
}