diff options
author | Steve Manuel <nilslice@gmail.com> | 2017-04-07 20:36:07 -0700 |
---|---|---|
committer | Steve Manuel <nilslice@gmail.com> | 2017-04-07 20:36:07 -0700 |
commit | afe92b2bcd3b8e914496006a414ad9b95dba3f0b (patch) | |
tree | 40aca87652d696db10d37eb19c4049528b8d7519 /system/db/search.go | |
parent | ba201f640c883d5ed2f3e1f68daff3a7ee4c8b9f (diff) |
add initial implementation of api search handler with full-text search by type
Diffstat (limited to 'system/db/search.go')
-rw-r--r-- | system/db/search.go | 39 |
1 files changed, 35 insertions, 4 deletions
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 +} |