about summary refs log tree commit diff stats
path: root/internal/search
diff options
context:
space:
mode:
authorAlan Pearce2024-05-11 14:34:15 +0200
committerAlan Pearce2024-05-11 14:37:30 +0200
commit1aa991ae1f1a426424549c92060b204114c8b3c2 (patch)
tree59dc9e67d6b0c5604d9a70f52260ae7d85411e6f /internal/search
parent48423548f4718886d32a87ddd65d5ee2620fd8c8 (diff)
downloadsearchix-1aa991ae1f1a426424549c92060b204114c8b3c2.tar.lz
searchix-1aa991ae1f1a426424549c92060b204114c8b3c2.tar.zst
searchix-1aa991ae1f1a426424549c92060b204114c8b3c2.zip
refactor: deduce index path automatically from config.DataPath
Diffstat (limited to 'internal/search')
-rw-r--r--internal/search/indexer.go15
-rw-r--r--internal/search/search.go5
2 files changed, 9 insertions, 11 deletions
diff --git a/internal/search/indexer.go b/internal/search/indexer.go
index c00c358..87cb12f 100644
--- a/internal/search/indexer.go
+++ b/internal/search/indexer.go
@@ -28,8 +28,6 @@ type WriteIndex struct {
 	index bleve.Index
 }
 
-const ExpectedIndexExtension = ".bleve"
-
 func createIndexMapping() (mapping.IndexMapping, error) {
 	indexMapping := bleve.NewIndexMapping()
 	indexMapping.StoreDynamic = false
@@ -125,10 +123,14 @@ func createIndex(indexPath string) (bleve.Index, error) {
 	return idx, nil
 }
 
-func NewIndexer(indexPath string, force bool) (*WriteIndex, error) {
+const indexBaseName = "index.bleve"
+
+func NewIndexer(dataRoot string, force bool) (*WriteIndex, error) {
 	var err error
 	bleve.SetLog(log.Default())
 
+	indexPath := path.Join(dataRoot, indexBaseName)
+
 	exists, err := file.Exists(indexPath)
 	if err != nil {
 		return nil, errors.WithMessagef(
@@ -141,13 +143,6 @@ func NewIndexer(indexPath string, force bool) (*WriteIndex, error) {
 	var idx bleve.Index
 	if !exists || force {
 		if force {
-			if path.Ext(indexPath) != ExpectedIndexExtension {
-				return nil, errors.Errorf(
-					"cowardly refusing to delete path %s (it doesn't end in '%s')",
-					indexPath,
-					ExpectedIndexExtension,
-				)
-			}
 			err := os.RemoveAll(indexPath)
 			if err != nil {
 				return nil, errors.WithMessagef(err, "could not remove index file %s", indexPath)
diff --git a/internal/search/search.go b/internal/search/search.go
index bc77cea..357698c 100644
--- a/internal/search/search.go
+++ b/internal/search/search.go
@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"context"
 	"encoding/gob"
+	"path"
 	"searchix/internal/options"
 
 	"github.com/blevesearch/bleve/v2"
@@ -27,7 +28,9 @@ type ReadIndex struct {
 	index bleve.Index
 }
 
-func Open(indexPath string) (*ReadIndex, error) {
+func Open(dataRoot string) (*ReadIndex, error) {
+	indexPath := path.Join(dataRoot, indexBaseName)
+
 	idx, err := bleve.Open(indexPath)
 	if err != nil {
 		return nil, errors.WithMessagef(err, "unable to open index at path %s", indexPath)