summaryrefslogtreecommitdiff
path: root/system/db
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2017-04-06 17:53:34 -0700
committerSteve Manuel <nilslice@gmail.com>2017-04-06 17:53:34 -0700
commitba201f640c883d5ed2f3e1f68daff3a7ee4c8b9f (patch)
treef3640ff2fabd1b45b62a0f045ba19289ebb5625c /system/db
parentbf15dcfa48a46991f3c6d90f50aa491b5a15d5df (diff)
renaming and add Delete operation for search index
Diffstat (limited to 'system/db')
-rw-r--r--system/db/content.go17
-rw-r--r--system/db/init.go2
-rw-r--r--system/db/search.go47
3 files changed, 56 insertions, 10 deletions
diff --git a/system/db/content.go b/system/db/content.go
index 7667831..a29ef20 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -121,9 +121,8 @@ 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, string(j))
+ err = UpdateSearchIndex(target, string(j))
if err != nil {
return 0, err
}
@@ -135,7 +134,7 @@ func mergeData(ns string, data url.Values, existingContent []byte) ([]byte, erro
var j []byte
t, ok := item.Types[ns]
if !ok {
- return nil, fmt.Errorf("namespace type not found:", ns)
+ return nil, fmt.Errorf("Namespace type not found: %s", ns)
}
// Unmarsal the existing values
@@ -247,9 +246,8 @@ 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, string(j))
+ err = UpdateSearchIndex(target, string(j))
if err != nil {
return 0, err
}
@@ -313,6 +311,15 @@ func DeleteContent(target string) error {
return err
}
+ // delete indexed data from search index
+ if !strings.Contains(ns, "__") {
+ target = fmt.Sprintf("%s:%s", ns, id)
+ err = DeleteSearchIndex(target)
+ if err != nil {
+ return err
+ }
+ }
+
// exception to typical "run in goroutine" pattern:
// we want to have an updated admin view as soon as this is deleted, so
// in some cases, the delete and redirect is faster than the sort,
diff --git a/system/db/init.go b/system/db/init.go
index e59e7c4..a598fa7 100644
--- a/system/db/init.go
+++ b/system/db/init.go
@@ -80,7 +80,7 @@ func Init() {
go func() {
for t := range item.Types {
- err := MapIndex(t)
+ err := MapSearchIndex(t)
if err != nil {
log.Fatalln("[search] Error:", err)
return
diff --git a/system/db/search.go b/system/db/search.go
index 743a904..7b7a203 100644
--- a/system/db/search.go
+++ b/system/db/search.go
@@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "strings"
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/mapping"
@@ -15,16 +16,16 @@ var Search map[string]bleve.Index
// Searchable ...
type Searchable interface {
- SearchMapping() *mapping.IndexMappingImpl
+ SearchMapping() (*mapping.IndexMappingImpl, error)
}
func init() {
Search = make(map[string]bleve.Index)
}
-// MapIndex creates the mapping for a type and tracks the index to be used within
+// MapSearchIndex 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 {
+func MapSearchIndex(typeName string) error {
// type assert for Searchable, get configuration (which can be overridden)
// by Ponzu user if defines own SearchMapping()
it, ok := item.Types[typeName]
@@ -36,7 +37,13 @@ func MapIndex(typeName string) error {
return fmt.Errorf("Item type %s doesn't implement db.Searchable", typeName)
}
- mapping := s.SearchMapping()
+ mapping, err := s.SearchMapping()
+ if err == item.ErrNoSearchMapping {
+ return nil
+ }
+ if err != nil {
+ return err
+ }
idxName := typeName + ".index"
var idx bleve.Index
@@ -72,3 +79,35 @@ func MapIndex(typeName string) error {
return nil
}
+
+// UpdateSearchIndex sets data into a content type's search index at the given
+// identifier
+func UpdateSearchIndex(id string, data interface{}) error {
+ // check if there is a search index to work with
+ target := strings.Split(id, ":")
+ ns := target[0]
+
+ idx, ok := Search[ns]
+ if ok {
+ // add data to search index
+ return idx.Index(id, data)
+ }
+
+ return nil
+}
+
+// DeleteSearchIndex removes data from a content type's search index at the
+// given identifier
+func DeleteSearchIndex(id string) error {
+ // check if there is a search index to work with
+ target := strings.Split(id, ":")
+ ns := target[0]
+
+ idx, ok := Search[ns]
+ if ok {
+ // add data to search index
+ return idx.Delete(id)
+ }
+
+ return nil
+}