database/elasticsearch/elasticsearch.go
func (db *Database) Init() error {

	// Create URL from host/port
	db.getURL()

	// Test connection to ElasticSearch
	err := db.TestConnection()
	if err != nil {
		return errors.Wrap(err, "failed to connect to database")
	}

	client, err := elastic.NewSimpleClient(
		elastic.SetURL(db.URL),
		elastic.SetBasicAuth(
			utils.Getopts(db.Username, "MALICE_ELASTICSEARCH_USERNAME", ""),
			utils.Getopts(db.Password, "MALICE_ELASTICSEARCH_PASSWORD", ""),
		),
	)
	if err != nil {
		return errors.Wrap(err, "failed to create elasticsearch simple client")
	}

	exists, err := client.IndexExists(db.Index).Do(context.Background())
	if err != nil {
		return errors.Wrap(err, "failed to check if index exists")
	}

	if !exists {
		// Index does not exist yet.
		createIndex, err := client.CreateIndex(db.Index).BodyString(mapping).Do(context.Background())
		if err != nil {
			return errors.Wrapf(err, "failed to create index: %s", db.Index)
		}

		if !createIndex.Acknowledged {
			log.Error("index creation not acknowledged")
		} else {
			log.Debugf("created index %s", db.Index)
		}
	} else {
		log.Debugf("index %s already exists", db.Index)
	}

	return nil
}
func (db *Database) Init() error {

	// Create URL from host/port
	db.getURL()

	// Test connection to ElasticSearch
	err := db.TestConnection()
	if err != nil {
		return errors.Wrap(err, "failed to connect to database")
	}

	client, err := elastic.NewSimpleClient(
		elastic.SetURL(db.URL),
		elastic.SetBasicAuth(
			utils.Getopts(db.Username, "MALICE_ELASTICSEARCH_USERNAME", ""),
			utils.Getopts(db.Password, "MALICE_ELASTICSEARCH_PASSWORD", ""),
		),
	)
	if err != nil {
		return errors.Wrap(err, "failed to create elasticsearch simple client")
	}

	exists, err := client.IndexExists(db.Index).Do(context.Background())
	if err != nil {
		return errors.Wrap(err, "failed to check if index exists")
	}

	if !exists {
		// Index does not exist yet.
		createIndex, err := client.CreateIndex(db.Index).BodyString(mapping).Do(context.Background())
		if err != nil {
			return errors.Wrapf(err, "failed to create index: %s", db.Index)
		}

		if !createIndex.Acknowledged {
			log.Error("index creation not acknowledged")
		} else {
			log.Debugf("created index %s", db.Index)
		}
	} else {
		log.Debugf("index %s already exists", db.Index)
	}

	return nil
}
// Init initalizes ElasticSearch for use with malice