summaryrefslogtreecommitdiff
path: root/system/db/search.go
diff options
context:
space:
mode:
Diffstat (limited to 'system/db/search.go')
-rw-r--r--system/db/search.go47
1 files changed, 43 insertions, 4 deletions
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
+}