// Post to Elastic Search, and returns an error in case is not success
// (there are good elastic search Go libs to do that, but is easy enoughs)
Post to Elastic Search, and returns an error in case is not success
(there are good elastic search Go libs to do that, but is easy enoughs)
// Post to Elastic Search and returns an error in case is not success // ( there are good elastic search Go libs to do that but is easy enoughs )
func postES(es string, index string, path string, body map[string]interface{}) error {
	b, err := json.Marshal(body)
	if err != nil {
		return err
	}
	u, err := url.Parse(es)
	if err != nil {
		return err
	}
	u.Path = index
	reader := bytes.NewReader(b)
	r, err := http.NewRequest(http.MethodPost, u.String(), reader)
	if err != nil {
		return err
	}
	resp, err := client.Do(r)
	if err != nil {
		return errors.New("ES error:" + err.Error())
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusCreated {
		respBody, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			log.Println("[ERROR]: reading ES response", path, err)
		} else {
			log.Println("[ERROR]: ES error ", string(respBody))
		}
		return errors.New("Error creating article on Elastic Search " + resp.Status)
	}
	log.Println("[INFO]: Created article from ", path)
	return nil
}
func postES ( es string , index string , path string , body map [ string ] interface { } ) error { b , err := json . Marshal ( body ) if err != nil { return err } u , err := url . Parse ( es ) if err != nil { return err } u . Path = index reader := bytes . NewReader ( b ) r , err := http . NewRequest ( http . MethodPost , u . String ( ) , reader ) if err != nil { return err } resp , err := client . Do ( r ) if err != nil { return errors . New ( " " + err . Error ( ) ) } defer resp . Body . Close ( ) if resp . StatusCode != http . StatusCreated { respBody , err := ioutil . ReadAll ( resp . Body ) if err != nil { log . Println ( " " , path , err ) } else { log . Println ( " " , string ( respBody ) ) } return errors . New ( " " + resp . Status ) } log . Println ( " " , path ) return nil }
Post to Elastic Search, and returns an error in case is not success
(there are good elastic search Go libs to do that, but is easy enoughs)
Post to Elastic Search and returns an error in case is not success ( there are good elastic search Go libs to do that but is easy enoughs )