Skip to content

Commit 7777fd8

Browse files
committed
Add ability to reindex search with knife's index command
1 parent 87b58af commit 7777fd8

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

goiardi.go

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func main(){
8686
http.HandleFunc("/sandboxes/", sandbox_handler)
8787
http.HandleFunc("/search", search_handler)
8888
http.HandleFunc("/search/", search_handler)
89+
http.HandleFunc("/search/reindex", reindexHandler)
8990
http.HandleFunc("/users", list_handler)
9091
http.HandleFunc("/users/", actor_handler)
9192
http.HandleFunc("/file_store/", file_store_handler)

indexer/indexer.go

+26-3
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,19 @@ var indexMap = initializeIndex()
388388
func initializeIndex() *Index {
389389
/* We always want these indices at least. */
390390
im := new(Index)
391-
im.idxmap = make(map[string]*IdxCollection)
391+
im.makeDefaultCollections()
392+
393+
return im
394+
}
395+
396+
func (i *Index) makeDefaultCollections() {
392397
defaults := [...]string{ "client", "environment", "node", "role" }
398+
i.m.Lock()
399+
i.idxmap = make(map[string]*IdxCollection)
400+
i.m.Unlock()
393401
for _, d := range defaults {
394-
im.createCollection(d)
402+
i.createCollection(d)
395403
}
396-
return im
397404
}
398405

399406
//Process and add an object to the index.
@@ -559,3 +566,19 @@ func (i *Index) load() error {
559566
}
560567
return fp.Close()
561568
}
569+
570+
// Clear index of all collections and documents
571+
func ClearIndex() {
572+
indexMap.makeDefaultCollections()
573+
return
574+
}
575+
// Rebuild the search index from scratch
576+
func ReIndex(objects []Indexable) error {
577+
for _, o := range objects {
578+
indexMap.saveIndex(o)
579+
}
580+
// We really ought to be able to return from an error, but at the moment
581+
// there aren't any ways it does so in the index save bits.
582+
return nil
583+
}
584+

search.go

+53
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"github.com/ctdk/goiardi/search"
2424
"github.com/ctdk/goiardi/data_bag"
2525
"github.com/ctdk/goiardi/actor"
26+
"github.com/ctdk/goiardi/indexer"
27+
"github.com/ctdk/goiardi/data_store"
2628
"net/http"
2729
"encoding/json"
2830
"fmt"
@@ -186,6 +188,57 @@ func search_handler(w http.ResponseWriter, r *http.Request){
186188
}
187189
}
188190

191+
func reindexHandler(w http.ResponseWriter, r *http.Request){
192+
w.Header().Set("Content-Type", "application/json")
193+
reindex_response := make(map[string]interface{})
194+
switch r.Method {
195+
case "POST":
196+
reindexObjs := make([]indexer.Indexable, 0)
197+
// We clear the index, *then* do the fetch because if
198+
// something comes in between the time we fetch the
199+
// objects to reindex and when it gets done, they'll
200+
// just be added naturally
201+
indexer.ClearIndex()
202+
// default indices
203+
defaults := [...]string{ "node", "client", "role", "env" }
204+
ds := data_store.New()
205+
for _, d := range defaults {
206+
objList := ds.GetList(d)
207+
for _, oname := range objList {
208+
u, _ := ds.Get(d, oname)
209+
if u != nil {
210+
reindexObjs = append(reindexObjs, u.(indexer.Indexable))
211+
}
212+
}
213+
}
214+
// data bags have to be done separately
215+
dbags := data_bag.GetList()
216+
for _, db := range dbags {
217+
dbag, err := data_bag.Get(db)
218+
if err != nil {
219+
continue
220+
}
221+
dbis := make([]indexer.Indexable, len(dbag.DataBagItems))
222+
i := 0
223+
for _, k := range dbag.DataBagItems {
224+
n := k
225+
dbis[i] = &n
226+
i++
227+
}
228+
reindexObjs = append(reindexObjs, dbis...)
229+
}
230+
indexer.ReIndex(reindexObjs)
231+
reindex_response["reindex"] = "OK"
232+
default:
233+
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)
234+
return
235+
}
236+
enc := json.NewEncoder(w)
237+
if err := enc.Encode(&reindex_response); err != nil {
238+
JsonErrorReport(w, r, err.Error(), http.StatusInternalServerError)
239+
}
240+
}
241+
189242
func partialSearchFormat(results []map[string]interface{}, partialFormat map[string]interface{}) ([]map[string]interface{}, error) {
190243
/* regularize partial search keys */
191244
psearchKeys := make(map[string][]string, len(partialFormat))

0 commit comments

Comments
 (0)