如何在Golang中使用Olivere将数据插入Elasticsearch

I need a correct example where I can insert the data into elasticsearch using olivere in golang. I have collected the data and it prints the result for each data that will be inserted. The problem is that there is no data inserted into elasticsearch.

This is the code to collect the data that will be inserted into elasticsearch.

indexing := company
indextype := employee 
data := lists{Id: id__,
            Article_id:      id_row,
            Category_id:     category_id,
            Datee:           date,
            Media_id:        media,
            Mention_times:   mention,
            Data_input_date: data_input_date,
            User:           user,
            name:         pc_name}

//Save to Elastic using olivere 
response := elastic.NewBulkIndexRequest().Index(indexing).Type(indextype).Id(id__).Doc(data)  
if response != nil {
        fmt.Println("Print the result", OlivereResponse)
    } 
/* log the result 
 Print the result for inserting to elastic using olivere {"index": 
{"_index":"company","_id":"2660e1","_type":"employee"}}
{"id":"26688","article_id":20,"category_id":"farming","datee":"2018-11-23","media_id":8, Mention_times: "20", "data_input_date":"2018-12-01 15:42:22","usere":"robot-kwd41","pc_name":"server"}
*/



 if OlivereResponse == nil {
       fmt.Println("Response returns null when inserting to elasticsearch using 
          olivere")
    }

Can anyone provide a complete example how to insert data into elasticsearch using olivere package in golang?

Thanks }

To insert data into Elastic I am using such code:

func (e *Elastic) SaveDocument(index string, id string, data interface{}) error {
    _, err := e.client.Index().Index(index).Type("entity").Id(id).
        BodyJson(data).Refresh("true").Do(e.ctx)

    if err != nil {
        e.fileLogger.Log(data)
        fmt.Printf("Error save document to elastic search: %s. Save log to file
", err.Error())
    }

    return err
}

Here are:

  • e.client is an instance f *elastic.Client;
  • e.ctx is an context.Context