diff options
author | Steve Manuel <nilslice@gmail.com> | 2017-04-11 14:51:22 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2017-04-11 14:51:22 -0700 |
commit | b961e74656b0db97d5b3c14295ca8c6ba16b4424 (patch) | |
tree | 3809b37535114c337452a3ab7320ca51ede02d06 | |
parent | 3f1d6b5df128973eb7c4c8a92c2b34fde2fb1ba0 (diff) |
change implementation of search to opt-in per type, less risky if sensative data is added
-rw-r--r-- | system/api/search.go | 2 | ||||
-rw-r--r-- | system/db/content.go | 4 | ||||
-rw-r--r-- | system/db/search.go | 22 | ||||
-rw-r--r-- | system/item/item.go | 6 |
4 files changed, 30 insertions, 4 deletions
diff --git a/system/api/search.go b/system/api/search.go index ae6ac1c..25a1bae 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 { diff --git a/system/db/content.go b/system/db/content.go index 49cba87..5db2896 100644 --- a/system/db/content.go +++ b/system/db/content.go @@ -124,7 +124,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) } @@ -252,7 +252,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..84a3828 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,6 +47,11 @@ func MapSearchIndex(typeName string) error { return fmt.Errorf("[search] MapSearchIndex Error: Item type %s doesn't implement db.Searchable", typeName) } + // skip setting or using index for types that shouldn't be indexed + if !s.IndexContent() { + return nil + } + mapping, err := s.SearchMapping() if err == item.ErrNoSearchMapping { return nil @@ -97,8 +105,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 |