Permalink
Browse files

Add ability to reindex search with knife's index command

  • Loading branch information...
ctdk committed Jan 10, 2014
1 parent 87b58af commit 7777fd82cb767007a22253c98c02e97f2f0a5bbf
Showing with 80 additions and 3 deletions.
  1. +1 −0 goiardi.go
  2. +26 −3 indexer/indexer.go
  3. +53 −0 search.go
View
@@ -86,6 +86,7 @@ func main(){
http.HandleFunc("/sandboxes/", sandbox_handler)
http.HandleFunc("/search", search_handler)
http.HandleFunc("/search/", search_handler)
http.HandleFunc("/search/reindex", reindexHandler)
http.HandleFunc("/users", list_handler)
http.HandleFunc("/users/", actor_handler)
http.HandleFunc("/file_store/", file_store_handler)
View
@@ -388,12 +388,19 @@ var indexMap = initializeIndex()
func initializeIndex() *Index {
/* We always want these indices at least. */
im := new(Index)
im.idxmap = make(map[string]*IdxCollection)
im.makeDefaultCollections()
return im
}
func (i *Index) makeDefaultCollections() {
defaults := [...]string{ "client", "environment", "node", "role" }
i.m.Lock()
i.idxmap = make(map[string]*IdxCollection)
i.m.Unlock()
for _, d := range defaults {
im.createCollection(d)
i.createCollection(d)
}
return im
}
//Process and add an object to the index.
@@ -559,3 +566,19 @@ func (i *Index) load() error {
}
return fp.Close()
}
// Clear index of all collections and documents
func ClearIndex() {
indexMap.makeDefaultCollections()
return
}
// Rebuild the search index from scratch
func ReIndex(objects []Indexable) error {
for _, o := range objects {
indexMap.saveIndex(o)
}
// We really ought to be able to return from an error, but at the moment
// there aren't any ways it does so in the index save bits.
return nil
}
View
@@ -23,6 +23,8 @@ import (
"github.com/ctdk/goiardi/search"
"github.com/ctdk/goiardi/data_bag"
"github.com/ctdk/goiardi/actor"
"github.com/ctdk/goiardi/indexer"
"github.com/ctdk/goiardi/data_store"
"net/http"
"encoding/json"
"fmt"
@@ -186,6 +188,57 @@ func search_handler(w http.ResponseWriter, r *http.Request){
}
}
func reindexHandler(w http.ResponseWriter, r *http.Request){
w.Header().Set("Content-Type", "application/json")
reindex_response := make(map[string]interface{})
switch r.Method {
case "POST":
reindexObjs := make([]indexer.Indexable, 0)
// We clear the index, *then* do the fetch because if
// something comes in between the time we fetch the
// objects to reindex and when it gets done, they'll
// just be added naturally
indexer.ClearIndex()
// default indices
defaults := [...]string{ "node", "client", "role", "env" }
ds := data_store.New()
for _, d := range defaults {
objList := ds.GetList(d)
for _, oname := range objList {
u, _ := ds.Get(d, oname)
if u != nil {
reindexObjs = append(reindexObjs, u.(indexer.Indexable))
}
}
}
// data bags have to be done separately
dbags := data_bag.GetList()
for _, db := range dbags {
dbag, err := data_bag.Get(db)
if err != nil {
continue
}
dbis := make([]indexer.Indexable, len(dbag.DataBagItems))
i := 0
for _, k := range dbag.DataBagItems {
n := k
dbis[i] = &n
i++
}
reindexObjs = append(reindexObjs, dbis...)
}
indexer.ReIndex(reindexObjs)
reindex_response["reindex"] = "OK"
default:
JsonErrorReport(w, r, "Method not allowed. If you're trying to do something with a data bag named 'reindex', it's not going to work I'm afraid.", http.StatusMethodNotAllowed)
return
}
enc := json.NewEncoder(w)
if err := enc.Encode(&reindex_response); err != nil {
JsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
}
}
func partialSearchFormat(results []map[string]interface{}, partialFormat map[string]interface{}) ([]map[string]interface{}, error) {
/* regularize partial search keys */
psearchKeys := make(map[string][]string, len(partialFormat))

0 comments on commit 7777fd8

Please sign in to comment.