summaryrefslogtreecommitdiff
path: root/system/db/search.go
blob: d5fc9f586dcfcf5410423c81dbf2d7dc046df8e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package db

import (
	"os"
	"path/filepath"

	"github.com/blevesearch/bleve"
)

// Search tracks all search indices to use throughout system
var Search map[string]bleve.Index

func init() {
	Search = make(map[string]bleve.Index)
}

// MapIndex creates the mapping for a type and tracks the index to be used within
// the system for adding/deleting/checking data
func MapIndex(typeName string) error {
	mapping := bleve.NewIndexMapping()
	mapping.StoreDynamic = false
	idxFile := typeName + ".index"
	var idx bleve.Index

	// check if index exists, use it or create new one
	pwd, err := os.Getwd()
	if err != nil {
		return err
	}
	if _, err = os.Stat(filepath.Join(pwd, idxFile)); os.IsNotExist(err) {
		idx, err = bleve.New(idxFile, mapping)
		if err != nil {
			return err
		}
	} else {
		idx, err = bleve.Open(idxFile)
		if err != nil {
			return err
		}
	}

	// add the type name to the index and track the index
	Search[typeName] = idx

	return nil
}