about summary refs log tree commit diff stats
path: root/internal/index/indexer.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-17 15:51:27 +0200
committerAlan Pearce2024-05-17 15:51:27 +0200
commit9f241bff227608dd53a250d012116077dce6dab6 (patch)
treec8f1f19788f866548dd42baaf7fedfd0560a47b7 /internal/index/indexer.go
parent43fe431c3aff36d52a630ca670f9fc4ee161962f (diff)
downloadsearchix-9f241bff227608dd53a250d012116077dce6dab6.tar.lz
searchix-9f241bff227608dd53a250d012116077dce6dab6.tar.zst
searchix-9f241bff227608dd53a250d012116077dce6dab6.zip
feat: automatically add/remove sources to/from index
Diffstat (limited to 'internal/index/indexer.go')
-rw-r--r--internal/index/indexer.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/internal/index/indexer.go b/internal/index/indexer.go
index a661b61..4a6a9d8 100644
--- a/internal/index/indexer.go
+++ b/internal/index/indexer.go
@@ -7,6 +7,7 @@ import (
 	"io/fs"
 	"log"
 	"log/slog"
+	"math"
 	"os"
 	"path"
 	"searchix/internal/file"
@@ -326,3 +327,37 @@ func (i *WriteIndex) Close() error {
 
 	return nil
 }
+
+func (i *WriteIndex) DeleteBySource(source string) error {
+	query := bleve.NewTermQuery(source)
+	search := bleve.NewSearchRequest(query)
+	search.Size = math.MaxInt
+	search.Fields = []string{"_id"}
+
+	results, err := i.index.Search(search)
+	if err != nil {
+		return errors.WithMessagef(err, "failed to query documents of retired index %s", source)
+	}
+
+	batch := i.index.NewBatch()
+	var k uint
+	for _, hit := range results.Hits {
+		batch.Delete(hit.ID)
+		if k++; k%batchSize == 0 {
+			err := i.Flush(batch)
+			if err != nil {
+				return err
+			}
+		}
+	}
+	err = i.Flush(batch)
+	if err != nil {
+		return err
+	}
+
+	if uint64(search.Size) < results.Total {
+		return i.DeleteBySource(source) // unlikely :^)
+	}
+
+	return nil
+}