diff options
author | Steve Manuel <nilslice@gmail.com> | 2017-04-06 12:27:12 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2017-04-06 12:27:12 -0700 |
commit | 38a6c611da7d5f4aa3a1d4fbb94ab5371b9cae5f (patch) | |
tree | e6341ddc2c2dd508217fa414de39c434b7c96c03 /system/db | |
parent | a7bdae0fb109f6fbe42afda6800338b76244a82c (diff) |
adding json values to search index on insert and update
Diffstat (limited to 'system/db')
-rw-r--r-- | system/db/content.go | 20 | ||||
-rw-r--r-- | system/db/search.go | 25 |
2 files changed, 39 insertions, 6 deletions
diff --git a/system/db/content.go b/system/db/content.go index 97ea9b4..cc3382f 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -121,6 +121,13 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error return 0, err } + // add data to search index + target := fmt.Sprintf("%s:%s", ns, id) + err = Search[ns].Index(target, j) + if err != nil { + return 0, err + } + return cid, nil } @@ -169,6 +176,8 @@ func insert(ns string, data url.Values) (int, error) { specifier = "__" + spec[1] } + var j []byte + var cid string err := store.Update(func(tx *bolt.Tx) error { b, err := tx.CreateBucketIfNotExists([]byte(ns + specifier)) if err != nil { @@ -181,7 +190,7 @@ func insert(ns string, data url.Values) (int, error) { if err != nil { return err } - cid := strconv.FormatUint(id, 10) + cid = strconv.FormatUint(id, 10) effectedID, err = strconv.Atoi(cid) if err != nil { return err @@ -197,7 +206,7 @@ func insert(ns string, data url.Values) (int, error) { data.Set("__specifier", specifier) } - j, err := postToJSON(ns, data) + j, err = postToJSON(ns, data) if err != nil { return err } @@ -238,6 +247,13 @@ func insert(ns string, data url.Values) (int, error) { return 0, err } + // add data to search index + target := fmt.Sprintf("%s:%s", ns, cid) + err = Search[ns].Index(target, j) + if err != nil { + return 0, err + } + return effectedID, nil } diff --git a/system/db/search.go b/system/db/search.go index d5fc9f5..8b642ff 100644 --- a/system/db/search.go +++ b/system/db/search.go @@ -5,11 +5,17 @@ import ( "path/filepath" "github.com/blevesearch/bleve" + "github.com/blevesearch/bleve/mapping" ) // Search tracks all search indices to use throughout system var Search map[string]bleve.Index +// Searchable ... +type Searchable interface { + SearchMapping() *mapping.IndexMappingImpl +} + func init() { Search = make(map[string]bleve.Index) } @@ -17,9 +23,12 @@ func init() { // 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 { + // TODO: type assert for Searchable, get configuration (which can be overridden) + // by Ponzu user if defines own SearchMapping() + mapping := bleve.NewIndexMapping() mapping.StoreDynamic = false - idxFile := typeName + ".index" + idxPath := typeName + ".index" var idx bleve.Index // check if index exists, use it or create new one @@ -27,13 +36,21 @@ func MapIndex(typeName string) error { if err != nil { return err } - if _, err = os.Stat(filepath.Join(pwd, idxFile)); os.IsNotExist(err) { - idx, err = bleve.New(idxFile, mapping) + + searchPath := filepath.Join(pwd, "search") + + err = os.MkdirAll(searchPath, os.ModeDir|os.ModePerm) + if err != nil { + return err + } + + if _, err = os.Stat(filepath.Join(searchPath, idxPath)); os.IsNotExist(err) { + idx, err = bleve.New(idxPath, mapping) if err != nil { return err } } else { - idx, err = bleve.Open(idxFile) + idx, err = bleve.Open(idxPath) if err != nil { return err } |