summaryrefslogtreecommitdiff
path: root/system/db/content.go
diff options
context:
space:
mode:
authorSteve <nilslice@gmail.com>2017-04-11 19:51:23 -0700
committerGitHub <noreply@github.com>2017-04-11 19:51:23 -0700
commit3c277a7c41aed0fe3e59fa1ccfd19b48d810b545 (patch)
tree7467eaae0e4ce120529d1984f7a0792a7216d5af /system/db/content.go
parent31ba833f6cf0ac7bce42e8b9b8b44a3020e140b9 (diff)
parent53e6675bc24ff356bc6cc35600fe4e7a8d068996 (diff)
Merge pull request #116 from ponzu-cms/ponzu-dev
[core] Configurable full-text search with Bleve + search API endpoint
Diffstat (limited to 'system/db/content.go')
-rw-r--r--system/db/content.go54
1 files changed, 51 insertions, 3 deletions
diff --git a/system/db/content.go b/system/db/content.go
index 97ea9b4..5db2896 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -121,6 +121,15 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error
return 0, err
}
+ go func() {
+ // update data in search index
+ target := fmt.Sprintf("%s:%s", ns, id)
+ err = UpdateSearchIndex(target, j)
+ if err != nil {
+ log.Println("[search] UpdateSearchIndex Error:", err)
+ }
+ }()
+
return cid, nil
}
@@ -128,7 +137,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
@@ -169,6 +178,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 +192,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 +208,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 +249,15 @@ func insert(ns string, data url.Values) (int, error) {
return 0, err
}
+ go func() {
+ // add data to seach index
+ target := fmt.Sprintf("%s:%s", ns, cid)
+ err = UpdateSearchIndex(target, j)
+ if err != nil {
+ log.Println("[search] UpdateSearchIndex Error:", err)
+ }
+ }()
+
return effectedID, nil
}
@@ -297,6 +317,17 @@ func DeleteContent(target string) error {
return err
}
+ go func() {
+ // delete indexed data from search index
+ if !strings.Contains(ns, "__") {
+ target = fmt.Sprintf("%s:%s", ns, id)
+ err = DeleteSearchIndex(target)
+ if err != nil {
+ log.Println("[search] DeleteSearchIndex Error:", 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,
@@ -334,6 +365,23 @@ func Content(target string) ([]byte, error) {
return val.Bytes(), nil
}
+// ContentMulti returns a set of content based on the the targets / identifiers
+// provided in Ponzu target string format: Type:ID
+// NOTE: All targets should be of the same type
+func ContentMulti(targets []string) ([][]byte, error) {
+ var contents [][]byte
+ for i := range targets {
+ b, err := Content(targets[i])
+ if err != nil {
+ return nil, err
+ }
+
+ contents = append(contents, b)
+ }
+
+ return contents, nil
+}
+
// ContentBySlug does a lookup in the content index to find the type and id of
// the requested content. Subsequently, issues the lookup in the type bucket and
// returns the the type and data at that ID or nil if nothing exists.