summaryrefslogtreecommitdiff
path: root/system/db
diff options
context:
space:
mode:
authorSteve Manuel <nilslice@gmail.com>2017-04-07 20:36:07 -0700
committerSteve Manuel <nilslice@gmail.com>2017-04-07 20:36:07 -0700
commitafe92b2bcd3b8e914496006a414ad9b95dba3f0b (patch)
tree40aca87652d696db10d37eb19c4049528b8d7519 /system/db
parentba201f640c883d5ed2f3e1f68daff3a7ee4c8b9f (diff)
add initial implementation of api search handler with full-text search by type
Diffstat (limited to 'system/db')
-rw-r--r--system/db/content.go62
-rw-r--r--system/db/init.go2
-rw-r--r--system/db/search.go39
3 files changed, 79 insertions, 24 deletions
diff --git a/system/db/content.go b/system/db/content.go
index a29ef20..a16c267 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -10,10 +10,9 @@ import (
"strconv"
"strings"
- "github.com/ponzu-cms/ponzu/system/item"
-
"github.com/boltdb/bolt"
"github.com/gorilla/schema"
+ "github.com/ponzu-cms/ponzu/system/item"
uuid "github.com/satori/go.uuid"
)
@@ -121,11 +120,14 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error
return 0, err
}
- target := fmt.Sprintf("%s:%s", ns, id)
- err = UpdateSearchIndex(target, string(j))
- if err != nil {
- return 0, err
- }
+ go func() {
+ // update data in search index
+ target := fmt.Sprintf("%s:%s", ns, id)
+ err = UpdateSearchIndex(target, string(j))
+ if err != nil {
+ log.Println("[search] UpdateSearchIndex Error:", err)
+ }
+ }()
return cid, nil
}
@@ -246,11 +248,14 @@ func insert(ns string, data url.Values) (int, error) {
return 0, err
}
- target := fmt.Sprintf("%s:%s", ns, cid)
- err = UpdateSearchIndex(target, string(j))
- if err != nil {
- return 0, err
- }
+ go func() {
+ // add data to seach index
+ target := fmt.Sprintf("%s:%s", ns, cid)
+ err = UpdateSearchIndex(target, string(j))
+ if err != nil {
+ log.Println("[search] UpdateSearchIndex Error:", err)
+ }
+ }()
return effectedID, nil
}
@@ -311,14 +316,16 @@ 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
+ 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
@@ -357,6 +364,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.
diff --git a/system/db/init.go b/system/db/init.go
index a598fa7..4e9c3cf 100644
--- a/system/db/init.go
+++ b/system/db/init.go
@@ -82,7 +82,7 @@ func Init() {
for t := range item.Types {
err := MapSearchIndex(t)
if err != nil {
- log.Fatalln("[search] Error:", err)
+ log.Fatalln(err)
return
}
diff --git a/system/db/search.go b/system/db/search.go
index 7b7a203..26e4321 100644
--- a/system/db/search.go
+++ b/system/db/search.go
@@ -1,6 +1,7 @@
package db
import (
+ "errors"
"fmt"
"os"
"path/filepath"
@@ -11,8 +12,13 @@ import (
"github.com/ponzu-cms/ponzu/system/item"
)
-// Search tracks all search indices to use throughout system
-var Search map[string]bleve.Index
+var (
+ // Search tracks all search indices to use throughout system
+ Search map[string]bleve.Index
+
+ // ErrNoSearchIndex is for failed checks for an index in Search map
+ ErrNoSearchIndex = errors.New("No search index found for type provided")
+)
// Searchable ...
type Searchable interface {
@@ -30,11 +36,11 @@ func MapSearchIndex(typeName string) error {
// by Ponzu user if defines own SearchMapping()
it, ok := item.Types[typeName]
if !ok {
- return fmt.Errorf("Failed to MapIndex for %s, type doesn't exist", typeName)
+ return fmt.Errorf("[search] MapSearchIndex Error: Failed to MapIndex for %s, type doesn't exist", typeName)
}
s, ok := it().(Searchable)
if !ok {
- return fmt.Errorf("Item type %s doesn't implement db.Searchable", typeName)
+ return fmt.Errorf("[search] MapSearchIndex Error: Item type %s doesn't implement db.Searchable", typeName)
}
mapping, err := s.SearchMapping()
@@ -67,6 +73,7 @@ func MapSearchIndex(typeName string) error {
if err != nil {
return err
}
+ idx.SetName(idxName)
} else {
idx, err = bleve.Open(idxPath)
if err != nil {
@@ -111,3 +118,27 @@ func DeleteSearchIndex(id string) error {
return nil
}
+
+// SearchType conducts a search and returns a set of Ponzu "targets", Type:ID pairs,
+// and an error. If there is no search index for the typeName (Type) provided,
+// db.ErrNoSearchIndex will be returned as the error
+func SearchType(typeName, query string) ([]string, error) {
+ idx, ok := Search[typeName]
+ if !ok {
+ return nil, ErrNoSearchIndex
+ }
+
+ q := bleve.NewQueryStringQuery(query)
+ req := bleve.NewSearchRequest(q)
+ res, err := idx.Search(req)
+ if err != nil {
+ return nil, err
+ }
+
+ var results []string
+ for _, hit := range res.Hits {
+ results = append(results, hit.ID)
+ }
+
+ return results, nil
+}