func main() { flag.Parse() if *indexPath == "" { log.Fatal("must specify index path") } // create a new default mapping mapping := bleve.NewIndexMapping() if *mappingFile != "" { mappingBytes, err := ioutil.ReadFile(*mappingFile) if err != nil { log.Fatal(err) } err = json.Unmarshal(mappingBytes, &mapping) if err != nil { log.Fatal(err) } } // create the index index, err := bleve.NewUsing(*indexPath, mapping, *indexType, *storeType, nil) if err != nil { log.Fatal(err) } defer func() { cerr := index.Close() if cerr != nil { log.Fatalf("error closing index: %v", err) } }() log.Printf("Created bleve index at: %s", *indexPath) }
func Init() { // open a new index var err error index, err = bleve.Open("acgsh.bleve") if err != nil { log.Println(err) mapping := bleve.NewIndexMapping() index, err = bleve.New("acgsh.bleve", mapping) if err != nil { log.Println(err) return } } }
func (h *CreateIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // find the name of the index to create var indexName string if h.IndexNameLookup != nil { indexName = h.IndexNameLookup(req) } if indexName == "" { showError(w, req, "index name is required", 400) return } indexMapping := bleve.NewIndexMapping() // read the request body requestBody, err := ioutil.ReadAll(req.Body) if err != nil { showError(w, req, fmt.Sprintf("error reading request body: %v", err), 400) return } // interpret request body as index mapping if len(requestBody) > 0 { err := json.Unmarshal(requestBody, &indexMapping) if err != nil { showError(w, req, fmt.Sprintf("error parsing index mapping: %v", err), 400) return } } newIndex, err := bleve.New(h.indexPath(indexName), indexMapping) if err != nil { showError(w, req, fmt.Sprintf("error creating index: %v", err), 500) return } newIndex.SetName(indexName) RegisterIndexName(indexName, newIndex) rv := struct { Status string `json:"status"` }{ Status: "ok", } mustEncode(w, rv) }