summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--system/api/search.go7
-rw-r--r--system/db/content.go4
-rw-r--r--system/db/search.go23
-rw-r--r--system/item/item.go6
-rw-r--r--system/item/types.go3
6 files changed, 35 insertions, 9 deletions
diff --git a/README.md b/README.md
index c79c6c6..0aa531a 100644
--- a/README.md
+++ b/README.md
@@ -295,6 +295,7 @@ $ ponzu --dev -fork=github.com/nilslice/ponzu new /path/to/new/project
- [golang.org/x/text/transform](https://golang.org/x/text/transform)
- [golang.org/x/crypto/bcrypt](https://golang.org/x/crypto/bcrypt)
- [golang.org/x/net/http2](https://golang.org/x/net/http2)
+- [github.com/blevesearch/bleve](https://github.com/blevesearch/bleve)
- [github.com/nilslice/jwt](https://github.com/nilslice/jwt)
- [github.com/nilslice/email](https://github.com/nilslice/email)
- [github.com/gorilla/schema](https://github.com/gorilla/schema)
diff --git a/system/api/search.go b/system/api/search.go
index ae6ac1c..c743d9f 100644
--- a/system/api/search.go
+++ b/system/api/search.go
@@ -44,7 +44,7 @@ func searchContentHandler(res http.ResponseWriter, req *http.Request) {
// execute search for query provided, if no index for type send 404
matches, err := db.SearchType(t, q)
if err == db.ErrNoSearchIndex {
- res.WriteHeader(http.StatusBadRequest)
+ res.WriteHeader(http.StatusNotFound)
return
}
if err != nil {
@@ -61,6 +61,11 @@ func searchContentHandler(res http.ResponseWriter, req *http.Request) {
return
}
+ // if we have matches, push the first as its matched by relevance
+ if len(bb) > 0 {
+ push(res, req, it, bb[0])
+ }
+
var result = []json.RawMessage{}
for i := range bb {
result = append(result, bb[i])
diff --git a/system/db/content.go b/system/db/content.go
index 23e488f..fbdd8dd 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -125,7 +125,7 @@ func update(ns, id string, data url.Values, existingContent *[]byte) (int, error
go func() {
// update data in search index
target := fmt.Sprintf("%s:%s", ns, id)
- err = UpdateSearchIndex(target, string(j))
+ err = UpdateSearchIndex(target, j)
if err != nil {
log.Println("[search] UpdateSearchIndex Error:", err)
}
@@ -253,7 +253,7 @@ func insert(ns string, data url.Values) (int, error) {
go func() {
// add data to seach index
target := fmt.Sprintf("%s:%s", ns, cid)
- err = UpdateSearchIndex(target, string(j))
+ err = UpdateSearchIndex(target, j)
if err != nil {
log.Println("[search] UpdateSearchIndex Error:", err)
}
diff --git a/system/db/search.go b/system/db/search.go
index 3e7a9d6..b3d5fa1 100644
--- a/system/db/search.go
+++ b/system/db/search.go
@@ -9,6 +9,8 @@ import (
"github.com/ponzu-cms/ponzu/system/item"
+ "encoding/json"
+
"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/mapping"
)
@@ -24,6 +26,7 @@ var (
// Searchable ...
type Searchable interface {
SearchMapping() (*mapping.IndexMappingImpl, error)
+ IndexContent() bool
}
func init() {
@@ -44,10 +47,12 @@ func MapSearchIndex(typeName string) error {
return fmt.Errorf("[search] MapSearchIndex Error: Item type %s doesn't implement db.Searchable", typeName)
}
- mapping, err := s.SearchMapping()
- if err == item.ErrNoSearchMapping {
+ // skip setting or using index for types that shouldn't be indexed
+ if !s.IndexContent() {
return nil
}
+
+ mapping, err := s.SearchMapping()
if err != nil {
return err
}
@@ -97,8 +102,20 @@ func UpdateSearchIndex(id string, data interface{}) error {
idx, ok := Search[ns]
if ok {
+ // unmarshal json to struct, error if not registered
+ it, ok := item.Types[ns]
+ if !ok {
+ return fmt.Errorf("[search] UpdateSearchIndex Error: type '%s' doesn't exist", ns)
+ }
+
+ p := it()
+ err := json.Unmarshal(data.([]byte), &p)
+ if err != nil {
+ return err
+ }
+
// add data to search index
- return idx.Index(id, data)
+ return idx.Index(id, p)
}
return nil
diff --git a/system/item/item.go b/system/item/item.go
index 4750ef5..8f392ca 100644
--- a/system/item/item.go
+++ b/system/item/item.go
@@ -219,6 +219,12 @@ func (i Item) SearchMapping() (*mapping.IndexMappingImpl, error) {
return mapping, nil
}
+// IndexContent determines if a type should be indexed for searching
+// partially implements db.Searchable
+func (i Item) IndexContent() bool {
+ return false
+}
+
// Slug returns a URL friendly string from the title of a post item
func Slug(i Identifiable) (string, error) {
// get the name of the post item
diff --git a/system/item/types.go b/system/item/types.go
index dbf13af..bcae58a 100644
--- a/system/item/types.go
+++ b/system/item/types.go
@@ -26,9 +26,6 @@ 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{}