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) }
func runTestDir(t *testing.T, dir, datasetName string) { // read the mapping mappingBytes, err := ioutil.ReadFile(dir + string(filepath.Separator) + "mapping.json") if err != nil { t.Errorf("error reading mapping: %v", err) return } var mapping bleve.IndexMapping err = json.Unmarshal(mappingBytes, &mapping) if err != nil { t.Errorf("error unmarshalling mapping: %v", err) return } // open new index if !*keepIndex { defer func() { err := os.RemoveAll("test.bleve") if err != nil { t.Fatal(err) } }() } index, err := bleve.New("test.bleve", &mapping) if err != nil { t.Errorf("error creating new index: %v", err) return } // set a custom index name index.SetName(datasetName) defer func() { err := index.Close() if err != nil { t.Fatal(err) } }() // index data fis, err := ioutil.ReadDir(dir + string(filepath.Separator) + "data") if err != nil { t.Errorf("error reading data dir: %v", err) return } for _, fi := range fis { fileBytes, err := ioutil.ReadFile(dir + string(filepath.Separator) + "data" + string(filepath.Separator) + fi.Name()) if err != nil { t.Errorf("error reading data file: %v", err) return } filename := fi.Name() ext := filepath.Ext(filename) id := filename[0 : len(filename)-len(ext)] err = index.Index(id, fileBytes) if err != nil { t.Errorf("error indexing data: %v", err) return } } indexInternal, _, err := index.Advanced() if err != nil { t.Fatal(err) } if indexInternal, ok := indexInternal.(bleveIndex.AsyncIndex); ok { start := time.Now() err = indexInternal.Wait(5 * time.Second) if err != nil { t.Fatal(err) } t.Logf("we had to wait for %v", time.Since(start)) } // read the searches searchBytes, err := ioutil.ReadFile(dir + string(filepath.Separator) + "searches.json") if err != nil { t.Errorf("error reading searches: %v", err) return } var searches SearchTests err = json.Unmarshal(searchBytes, &searches) if err != nil { t.Errorf("error unmarshalling searches: %v", err) return } // run the searches for testNum, search := range searches { res, err := index.Search(search.Search) if err != nil { t.Errorf("error running search: %v", err) } if res.Total != search.Result.Total { t.Errorf("test %d - expected total: %d got %d", testNum, search.Result.Total, res.Total) continue } if len(res.Hits) != len(search.Result.Hits) { t.Errorf("test %d - expected hits len: %d got %d", testNum, len(search.Result.Hits), len(res.Hits)) continue } for hi, hit := range search.Result.Hits { if hit.ID != res.Hits[hi].ID { t.Errorf("test %d - expected hit %d to have ID %s got %s", testNum, hi, hit.ID, res.Hits[hi].ID) } if hit.Fields != nil { if !reflect.DeepEqual(hit.Fields, res.Hits[hi].Fields) { t.Errorf("test %d - expected hit %d to have fields %#v got %#v", testNum, hi, hit.Fields, res.Hits[hi].Fields) } } if hit.Fragments != nil { if !reflect.DeepEqual(hit.Fragments, res.Hits[hi].Fragments) { t.Errorf("test %d - expected hit %d to have fragments %#v got %#v", testNum, hi, hit.Fragments, res.Hits[hi].Fragments) } } if hit.Locations != nil { if !reflect.DeepEqual(hit.Locations, res.Hits[hi].Locations) { t.Errorf("test %d - expected hit %d to have locations %v got %v", testNum, hi, hit.Locations, res.Hits[hi].Locations) } } } if search.Result.Facets != nil { if !reflect.DeepEqual(search.Result.Facets, res.Facets) { t.Errorf("test %d - expected facets: %#v got %#v", testNum, search.Result.Facets, res.Facets) } } // check that custom index name is in results for _, hit := range res.Hits { if hit.Index != datasetName { t.Fatalf("expected name: %s, got: %s", datasetName, hit.Index) } } } }