func indexDocument(engine factory.SearchEngine) (timeTook int64) { data, err := utils.ReadFile(*pathToDoc) utils.ErrorCheck(err) fmt.Printf("Indexing document: %s\n", *pathToDoc) doc := factory.Document{utils.FixIdSyntax(*pathToDoc), data} timeTook, err = engine.Index(&doc) utils.ErrorCheck(err) return timeTook }
func indexDocuments(engine factory.SearchEngine) (int64, int) { // prepare documents for batch index files, err := ioutil.ReadDir(*pathToFolder) utils.ErrorCheck(err) var docCount int = 0 var documents = make([]*factory.Document, len(files)) for _, file := range files { if file.IsDir() { continue // expected flat structure } path := *pathToFolder + "/" + file.Name() data, err := utils.ReadFile(path) utils.ErrorCheck(err) documents[docCount] = &factory.Document{utils.FixIdSyntax(path), data} docCount++ } // index in batch timeTook, err := engine.BatchIndex(documents) utils.ErrorCheck(err) return timeTook, docCount }
func (es *ElasticEngine) Index(document *Document) (int64, error) { start := time.Now().UnixNano() / int64(time.Millisecond) err := es.createIndexIfNotExists() utils.ErrorCheck(err) // Index the data _, err = es.Client.Index().Index(INDEX).Type((*document).Id).Id((*document).Id). BodyJson(string((*document).Data[:])).Do() if err != nil { return 0, err } return int64(time.Now().UnixNano()/int64(time.Millisecond) - start), nil }
func (es *ElasticEngine) BatchIndex(documents []*Document) (int64, error) { err := es.createIndexIfNotExists() utils.ErrorCheck(err) bulkRequest := es.Client.Bulk() for _, document := range documents { bulkRequest.Add(elastic.NewBulkIndexRequest().Index(INDEX).Type(document.Id).Id(document.Id). Doc(string((*document).Data[:]))) } bulkResponse, err := bulkRequest.Do() return int64(bulkResponse.Took), err }
func doSearch() { if *query == "" { fmt.Fprintf(os.Stderr, "error: %v\n", "Query string must be specified") os.Exit(1) } engine := getSearchEngine() // Search searchResult, err := engine.Search(*query) utils.ErrorCheck(err) switch value := searchResult.(type) { case *elastic.SearchResult: printElasticResult(value) case *bleve.SearchResult: printBleveResult(value) } }
func getSearchEngine() factory.SearchEngine { engine, err := factory.GetSearchEngine(url, vendor, *KVStore) utils.ErrorCheck(err) return engine }
func doDelete() { engine := getSearchEngine() err := engine.Delete() utils.ErrorCheck(err) fmt.Printf("Index deleted\n") }