func (be *BleveEngine) Index(document *Document) (int64, error) { start := time.Now().UnixNano() / int64(time.Millisecond) var index bleve.Index mapping := bleve.NewIndexMapping() index, err := bleve.New(INDEX, mapping) if err != nil { index, _ = bleve.Open(INDEX) } id := (*document).Id data := (*document).Data index.Index(id, data) index.Close() return time.Now().UnixNano()/int64(time.Millisecond) - start, nil }
func (be *BleveEngine) BatchIndex(documents []*Document) (int64, error) { start := time.Now().UnixNano() / int64(time.Millisecond) var index bleve.Index mapping := bleve.NewIndexMapping() index, err := bleve.New(INDEX, mapping) if err != nil { index, _ = bleve.Open(INDEX) } batch := index.NewBatch() for _, document := range documents { batch.Index(document.Id, document.Data) } index.Batch(batch) index.Close() return time.Now().UnixNano()/int64(time.Millisecond) - start, nil }
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 var index bleve.Index var err error if *storeType != "" { index, err = bleve.NewUsing(*indexPath, mapping, *storeType, nil) } else { index, err = bleve.New(*indexPath, mapping) } 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) }
Use: "bleve", Short: "command-line tool to interact with a bleve index", Long: `Bleve is a command-line tool to interact with a bleve index.`, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { if len(args) < 1 { return fmt.Errorf("must specify path to index") } var err error idx, err = bleve.Open(args[0]) if err != nil { return fmt.Errorf("error opening bleve index: %v", err) } return nil }, PersistentPostRunE: func(cmd *cobra.Command, args []string) error { err := idx.Close() if err != nil { return fmt.Errorf("error closing bleve index: %v", err) } return nil }, } // Execute adds all child commands to the root command sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := RootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(-1) } }