diff options
author | Steve Manuel <nilslice@gmail.com> | 2017-04-06 17:53:34 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2017-04-06 17:53:34 -0700 |
commit | ba201f640c883d5ed2f3e1f68daff3a7ee4c8b9f (patch) | |
tree | f3640ff2fabd1b45b62a0f045ba19289ebb5625c | |
parent | bf15dcfa48a46991f3c6d90f50aa491b5a15d5df (diff) |
renaming and add Delete operation for search index
-rw-r--r-- | system/db/content.go | 17 | ||||
-rw-r--r-- | system/db/init.go | 2 | ||||
-rw-r--r-- | system/db/search.go | 47 | ||||
-rw-r--r-- | system/item/item.go | 4 | ||||
-rw-r--r-- | system/item/types.go | 3 |
5 files changed, 61 insertions, 12 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 +} diff --git a/system/item/item.go b/system/item/item.go index 5babf35..4750ef5 100644 --- a/system/item/item.go +++ b/system/item/item.go @@ -212,11 +212,11 @@ func (i Item) AfterReject(res http.ResponseWriter, req *http.Request) error { // SearchMapping returns a default implementation of a Bleve IndexMappingImpl // partially implements db.Searchable -func (i Item) SearchMapping() *mapping.IndexMappingImpl { +func (i Item) SearchMapping() (*mapping.IndexMappingImpl, error) { mapping := bleve.NewIndexMapping() mapping.StoreDynamic = false - return mapping + return mapping, nil } // Slug returns a URL friendly string from the title of a post item diff --git a/system/item/types.go b/system/item/types.go index bcae58a..dbf13af 100644 --- a/system/item/types.go +++ b/system/item/types.go @@ -26,6 +26,9 @@ var ( // if requested by a valid admin or user ErrAllowHiddenItem = errors.New(`Allow hidden item`) + // ErrNoSearchMapping can be used to tell the system not to create an index mapping + ErrNoSearchMapping = errors.New(`No search mapping for item`) + // Types is a map used to reference a type name to its actual Editable type // mainly for lookups in /admin route based utilities Types map[string]func() interface{} |