about summary refs log tree commit diff stats
path: root/internal
diff options
context:
space:
mode:
authorAlan Pearce2024-05-11 13:30:41 +0200
committerAlan Pearce2024-05-11 13:30:41 +0200
commit48423548f4718886d32a87ddd65d5ee2620fd8c8 (patch)
tree9b3923cfc178fbec967264fdd292eab19ad98878 /internal
parent38c96a03c347395af9afe2c0e19266d6caa0ae0c (diff)
downloadsearchix-48423548f4718886d32a87ddd65d5ee2620fd8c8.tar.lz
searchix-48423548f4718886d32a87ddd65d5ee2620fd8c8.tar.zst
searchix-48423548f4718886d32a87ddd65d5ee2620fd8c8.zip
refactor: split up NewIndexer function
Diffstat (limited to 'internal')
-rw-r--r--internal/search/indexer.go102
1 files changed, 64 insertions, 38 deletions
diff --git a/internal/search/indexer.go b/internal/search/indexer.go
index 0ff041f..c00c358 100644
--- a/internal/search/indexer.go
+++ b/internal/search/indexer.go
@@ -25,44 +25,12 @@ import (
 )
 
 type WriteIndex struct {
-	index        bleve.Index
-	indexMapping *mapping.IndexMappingImpl
+	index bleve.Index
 }
 
 const ExpectedIndexExtension = ".bleve"
 
-func NewIndexer(indexPath string, force bool) (*WriteIndex, error) {
-	var err error
-	bleve.SetLog(log.Default())
-
-	exists, err := file.Exists(indexPath)
-	if err != nil {
-		return nil, errors.WithMessagef(
-			err,
-			"could not check if index exists at path %s",
-			indexPath,
-		)
-	}
-	if exists {
-		if !force {
-			return nil, errors.Errorf(
-				"index file %s already exists (use --force to replace)",
-				indexPath,
-			)
-		}
-		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)
-		}
-	}
-
+func createIndexMapping() (mapping.IndexMapping, error) {
 	indexMapping := bleve.NewIndexMapping()
 	indexMapping.StoreDynamic = false
 	indexMapping.IndexDynamic = false
@@ -75,7 +43,7 @@ func NewIndexer(indexPath string, force bool) (*WriteIndex, error) {
 	descriptionFieldMapping.Store = false
 	descriptionFieldMapping.Analyzer = web.Name
 
-	err = indexMapping.AddCustomAnalyzer("option_name", map[string]interface{}{
+	err := indexMapping.AddCustomAnalyzer("option_name", map[string]interface{}{
 		"type":      custom.Name,
 		"tokenizer": letter.Name,
 		"token_filters": []string{
@@ -133,14 +101,71 @@ func NewIndexer(indexPath string, force bool) (*WriteIndex, error) {
 
 	indexMapping.AddDocumentMapping("option", optionMapping)
 
-	idx, err := bleve.New(indexPath, indexMapping)
+	return indexMapping, nil
+}
+
+func createIndex(indexPath string) (bleve.Index, error) {
+	indexMapping, err := createIndexMapping()
+	if err != nil {
+		return nil, err
+	}
+	idx, err := bleve.NewUsing(
+		indexPath,
+		indexMapping,
+		bleve.Config.DefaultIndexType,
+		bleve.Config.DefaultKVStore,
+		map[string]interface{}{
+			"nosync": true,
+		},
+	)
 	if err != nil {
 		return nil, errors.WithMessagef(err, "unable to create index at path %s", indexPath)
 	}
 
+	return idx, nil
+}
+
+func NewIndexer(indexPath string, force bool) (*WriteIndex, error) {
+	var err error
+	bleve.SetLog(log.Default())
+
+	exists, err := file.Exists(indexPath)
+	if err != nil {
+		return nil, errors.WithMessagef(
+			err,
+			"could not check if index exists at path %s",
+			indexPath,
+		)
+	}
+
+	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)
+			}
+		}
+		idx, err = createIndex(indexPath)
+	} else {
+		idx, err = bleve.Open(indexPath)
+		if err != nil {
+			err = errors.WithMessagef(err, "could not open index at path %s", indexPath)
+		}
+	}
+	if err != nil {
+		return nil, err
+	}
+
 	return &WriteIndex{
 		idx,
-		indexMapping,
 	}, nil
 }
 
@@ -154,6 +179,7 @@ func (i *WriteIndex) ImportOptions(
 	go func() {
 		defer close(errs)
 		batch := i.index.NewBatch()
+		indexMapping := i.index.Mapping()
 
 	outer:
 		for opt := range objects {
@@ -166,7 +192,7 @@ func (i *WriteIndex) ImportOptions(
 			}
 
 			doc := document.NewDocument(opt.Source + "/" + opt.Name)
-			err = i.indexMapping.MapDocument(doc, opt)
+			err = indexMapping.MapDocument(doc, opt)
 			if err != nil {
 				errs <- errors.WithMessagef(err, "could not map document for option: %s", opt.Name)