A BulkIndexer in Olivere Package for Golang to Replace Elastigo











up vote
1
down vote

favorite












I notice that I can use BulkIndexer if I want to send data into elasticsearch in bulk. As stated in the Elastigo documentation




A bulk indexer creates goroutines, and channels for connecting and sending data to elasticsearch in bulk, using buffers.




Code in elastigo to insert in bulk



var c_es = elastigo.NewConn()
var indexer = c_es.NewBulkIndexer(50)

func insertInBulkElastic(){
//Create a custom error function when inserting data into elasticsearch
//in bulk
indexer.Sender = func(buf *bytes.Buffer) error {
// @buf is the buffer of docs about to be written
respJson, err := c_es.DoCommand("POST", "/_bulk", nil, buf)
if err != nil {
// handle it better than this

fmt.Println("Error", string(respJson)) //

fmt.Println("Error", err)
}

if err == nil {
fmt.Println("The data was inserted successfullly to elastic search")
}
return err
}



}


Does anyone know how to send bulk request using olivere for golang?



Thanks










share|improve this question






















  • github.com/olivere/elastic/blob/release-branch.v6/bulk.go
    – Vorsprung
    Nov 22 at 6:45















up vote
1
down vote

favorite












I notice that I can use BulkIndexer if I want to send data into elasticsearch in bulk. As stated in the Elastigo documentation




A bulk indexer creates goroutines, and channels for connecting and sending data to elasticsearch in bulk, using buffers.




Code in elastigo to insert in bulk



var c_es = elastigo.NewConn()
var indexer = c_es.NewBulkIndexer(50)

func insertInBulkElastic(){
//Create a custom error function when inserting data into elasticsearch
//in bulk
indexer.Sender = func(buf *bytes.Buffer) error {
// @buf is the buffer of docs about to be written
respJson, err := c_es.DoCommand("POST", "/_bulk", nil, buf)
if err != nil {
// handle it better than this

fmt.Println("Error", string(respJson)) //

fmt.Println("Error", err)
}

if err == nil {
fmt.Println("The data was inserted successfullly to elastic search")
}
return err
}



}


Does anyone know how to send bulk request using olivere for golang?



Thanks










share|improve this question






















  • github.com/olivere/elastic/blob/release-branch.v6/bulk.go
    – Vorsprung
    Nov 22 at 6:45













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I notice that I can use BulkIndexer if I want to send data into elasticsearch in bulk. As stated in the Elastigo documentation




A bulk indexer creates goroutines, and channels for connecting and sending data to elasticsearch in bulk, using buffers.




Code in elastigo to insert in bulk



var c_es = elastigo.NewConn()
var indexer = c_es.NewBulkIndexer(50)

func insertInBulkElastic(){
//Create a custom error function when inserting data into elasticsearch
//in bulk
indexer.Sender = func(buf *bytes.Buffer) error {
// @buf is the buffer of docs about to be written
respJson, err := c_es.DoCommand("POST", "/_bulk", nil, buf)
if err != nil {
// handle it better than this

fmt.Println("Error", string(respJson)) //

fmt.Println("Error", err)
}

if err == nil {
fmt.Println("The data was inserted successfullly to elastic search")
}
return err
}



}


Does anyone know how to send bulk request using olivere for golang?



Thanks










share|improve this question













I notice that I can use BulkIndexer if I want to send data into elasticsearch in bulk. As stated in the Elastigo documentation




A bulk indexer creates goroutines, and channels for connecting and sending data to elasticsearch in bulk, using buffers.




Code in elastigo to insert in bulk



var c_es = elastigo.NewConn()
var indexer = c_es.NewBulkIndexer(50)

func insertInBulkElastic(){
//Create a custom error function when inserting data into elasticsearch
//in bulk
indexer.Sender = func(buf *bytes.Buffer) error {
// @buf is the buffer of docs about to be written
respJson, err := c_es.DoCommand("POST", "/_bulk", nil, buf)
if err != nil {
// handle it better than this

fmt.Println("Error", string(respJson)) //

fmt.Println("Error", err)
}

if err == nil {
fmt.Println("The data was inserted successfullly to elastic search")
}
return err
}



}


Does anyone know how to send bulk request using olivere for golang?



Thanks







elasticsearch go insert






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 5:26









Honddoe

137




137












  • github.com/olivere/elastic/blob/release-branch.v6/bulk.go
    – Vorsprung
    Nov 22 at 6:45


















  • github.com/olivere/elastic/blob/release-branch.v6/bulk.go
    – Vorsprung
    Nov 22 at 6:45
















github.com/olivere/elastic/blob/release-branch.v6/bulk.go
– Vorsprung
Nov 22 at 6:45




github.com/olivere/elastic/blob/release-branch.v6/bulk.go
– Vorsprung
Nov 22 at 6:45












1 Answer
1






active

oldest

votes

















up vote
1
down vote













Here is a working example using olivere in Go. You can read more about the BulkProcessor here



Hope this help :)



package main

import (
"context"
"log"
"time"

elastic "gopkg.in/olivere/elastic.v5"
)

func main() {
options := elastic.ClientOptionFunc{
elastic.SetHealthcheck(true),
elastic.SetHealthcheckTimeout(20 * time.Second),
elastic.SetSniff(false),
elastic.SetHealthcheckInterval(30 * time.Second),
elastic.SetURL("http://127.0.0.1:9200"),
elastic.SetRetrier(elastic.NewBackoffRetrier(elastic.NewConstantBackoff(5 * time.Second))),
}
client, err := elastic.NewClient(options...)
if err != nil {
panic(err)
}
// ensure index exist
exists, err := client.IndexExists("my_index").Do(context.Background())
if err != nil {
panic(err)
}
if !exists {
if _, err := client.CreateIndex("my_index").Do(context.Background()); err != nil {
panic(err)
}
}
client.PutMapping().Index("my_index").BodyJson(map[string]interface{}{
"properties": map[string]string{
"name": "keyword",
},
}).Do(context.Background())

// create new bulk processor from client
bulkProcessor, err := elastic.NewBulkProcessorService(client).
Workers(5).
BulkActions(1000).
FlushInterval(1 * time.Second).
After(after).
Do(context.Background())

// now the bulk processor can be reused for entire the app
myDoc := struct {
Name string
}{
Name: "jack",
}
req := elastic.NewBulkIndexRequest()
req.Index("my_index").Type("type").Id("my_doc_id").Doc(myDoc)

// Use Add method to add request into the processor
bulkProcessor.Add(req)

// wait for sometime...
time.Sleep(5 * time.Second)
}

func after(executionID int64, requests elastic.BulkableRequest, response *elastic.BulkResponse, err error) {
if err != nil {
log.Printf("bulk commit failed, err: %vn", err)
}
// do what ever you want in case bulk commit success
log.Printf("commit successfully, len(requests)=%dn", len(requests))
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424391%2fa-bulkindexer-in-olivere-package-for-golang-to-replace-elastigo%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Here is a working example using olivere in Go. You can read more about the BulkProcessor here



    Hope this help :)



    package main

    import (
    "context"
    "log"
    "time"

    elastic "gopkg.in/olivere/elastic.v5"
    )

    func main() {
    options := elastic.ClientOptionFunc{
    elastic.SetHealthcheck(true),
    elastic.SetHealthcheckTimeout(20 * time.Second),
    elastic.SetSniff(false),
    elastic.SetHealthcheckInterval(30 * time.Second),
    elastic.SetURL("http://127.0.0.1:9200"),
    elastic.SetRetrier(elastic.NewBackoffRetrier(elastic.NewConstantBackoff(5 * time.Second))),
    }
    client, err := elastic.NewClient(options...)
    if err != nil {
    panic(err)
    }
    // ensure index exist
    exists, err := client.IndexExists("my_index").Do(context.Background())
    if err != nil {
    panic(err)
    }
    if !exists {
    if _, err := client.CreateIndex("my_index").Do(context.Background()); err != nil {
    panic(err)
    }
    }
    client.PutMapping().Index("my_index").BodyJson(map[string]interface{}{
    "properties": map[string]string{
    "name": "keyword",
    },
    }).Do(context.Background())

    // create new bulk processor from client
    bulkProcessor, err := elastic.NewBulkProcessorService(client).
    Workers(5).
    BulkActions(1000).
    FlushInterval(1 * time.Second).
    After(after).
    Do(context.Background())

    // now the bulk processor can be reused for entire the app
    myDoc := struct {
    Name string
    }{
    Name: "jack",
    }
    req := elastic.NewBulkIndexRequest()
    req.Index("my_index").Type("type").Id("my_doc_id").Doc(myDoc)

    // Use Add method to add request into the processor
    bulkProcessor.Add(req)

    // wait for sometime...
    time.Sleep(5 * time.Second)
    }

    func after(executionID int64, requests elastic.BulkableRequest, response *elastic.BulkResponse, err error) {
    if err != nil {
    log.Printf("bulk commit failed, err: %vn", err)
    }
    // do what ever you want in case bulk commit success
    log.Printf("commit successfully, len(requests)=%dn", len(requests))
    }





    share|improve this answer

























      up vote
      1
      down vote













      Here is a working example using olivere in Go. You can read more about the BulkProcessor here



      Hope this help :)



      package main

      import (
      "context"
      "log"
      "time"

      elastic "gopkg.in/olivere/elastic.v5"
      )

      func main() {
      options := elastic.ClientOptionFunc{
      elastic.SetHealthcheck(true),
      elastic.SetHealthcheckTimeout(20 * time.Second),
      elastic.SetSniff(false),
      elastic.SetHealthcheckInterval(30 * time.Second),
      elastic.SetURL("http://127.0.0.1:9200"),
      elastic.SetRetrier(elastic.NewBackoffRetrier(elastic.NewConstantBackoff(5 * time.Second))),
      }
      client, err := elastic.NewClient(options...)
      if err != nil {
      panic(err)
      }
      // ensure index exist
      exists, err := client.IndexExists("my_index").Do(context.Background())
      if err != nil {
      panic(err)
      }
      if !exists {
      if _, err := client.CreateIndex("my_index").Do(context.Background()); err != nil {
      panic(err)
      }
      }
      client.PutMapping().Index("my_index").BodyJson(map[string]interface{}{
      "properties": map[string]string{
      "name": "keyword",
      },
      }).Do(context.Background())

      // create new bulk processor from client
      bulkProcessor, err := elastic.NewBulkProcessorService(client).
      Workers(5).
      BulkActions(1000).
      FlushInterval(1 * time.Second).
      After(after).
      Do(context.Background())

      // now the bulk processor can be reused for entire the app
      myDoc := struct {
      Name string
      }{
      Name: "jack",
      }
      req := elastic.NewBulkIndexRequest()
      req.Index("my_index").Type("type").Id("my_doc_id").Doc(myDoc)

      // Use Add method to add request into the processor
      bulkProcessor.Add(req)

      // wait for sometime...
      time.Sleep(5 * time.Second)
      }

      func after(executionID int64, requests elastic.BulkableRequest, response *elastic.BulkResponse, err error) {
      if err != nil {
      log.Printf("bulk commit failed, err: %vn", err)
      }
      // do what ever you want in case bulk commit success
      log.Printf("commit successfully, len(requests)=%dn", len(requests))
      }





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        Here is a working example using olivere in Go. You can read more about the BulkProcessor here



        Hope this help :)



        package main

        import (
        "context"
        "log"
        "time"

        elastic "gopkg.in/olivere/elastic.v5"
        )

        func main() {
        options := elastic.ClientOptionFunc{
        elastic.SetHealthcheck(true),
        elastic.SetHealthcheckTimeout(20 * time.Second),
        elastic.SetSniff(false),
        elastic.SetHealthcheckInterval(30 * time.Second),
        elastic.SetURL("http://127.0.0.1:9200"),
        elastic.SetRetrier(elastic.NewBackoffRetrier(elastic.NewConstantBackoff(5 * time.Second))),
        }
        client, err := elastic.NewClient(options...)
        if err != nil {
        panic(err)
        }
        // ensure index exist
        exists, err := client.IndexExists("my_index").Do(context.Background())
        if err != nil {
        panic(err)
        }
        if !exists {
        if _, err := client.CreateIndex("my_index").Do(context.Background()); err != nil {
        panic(err)
        }
        }
        client.PutMapping().Index("my_index").BodyJson(map[string]interface{}{
        "properties": map[string]string{
        "name": "keyword",
        },
        }).Do(context.Background())

        // create new bulk processor from client
        bulkProcessor, err := elastic.NewBulkProcessorService(client).
        Workers(5).
        BulkActions(1000).
        FlushInterval(1 * time.Second).
        After(after).
        Do(context.Background())

        // now the bulk processor can be reused for entire the app
        myDoc := struct {
        Name string
        }{
        Name: "jack",
        }
        req := elastic.NewBulkIndexRequest()
        req.Index("my_index").Type("type").Id("my_doc_id").Doc(myDoc)

        // Use Add method to add request into the processor
        bulkProcessor.Add(req)

        // wait for sometime...
        time.Sleep(5 * time.Second)
        }

        func after(executionID int64, requests elastic.BulkableRequest, response *elastic.BulkResponse, err error) {
        if err != nil {
        log.Printf("bulk commit failed, err: %vn", err)
        }
        // do what ever you want in case bulk commit success
        log.Printf("commit successfully, len(requests)=%dn", len(requests))
        }





        share|improve this answer












        Here is a working example using olivere in Go. You can read more about the BulkProcessor here



        Hope this help :)



        package main

        import (
        "context"
        "log"
        "time"

        elastic "gopkg.in/olivere/elastic.v5"
        )

        func main() {
        options := elastic.ClientOptionFunc{
        elastic.SetHealthcheck(true),
        elastic.SetHealthcheckTimeout(20 * time.Second),
        elastic.SetSniff(false),
        elastic.SetHealthcheckInterval(30 * time.Second),
        elastic.SetURL("http://127.0.0.1:9200"),
        elastic.SetRetrier(elastic.NewBackoffRetrier(elastic.NewConstantBackoff(5 * time.Second))),
        }
        client, err := elastic.NewClient(options...)
        if err != nil {
        panic(err)
        }
        // ensure index exist
        exists, err := client.IndexExists("my_index").Do(context.Background())
        if err != nil {
        panic(err)
        }
        if !exists {
        if _, err := client.CreateIndex("my_index").Do(context.Background()); err != nil {
        panic(err)
        }
        }
        client.PutMapping().Index("my_index").BodyJson(map[string]interface{}{
        "properties": map[string]string{
        "name": "keyword",
        },
        }).Do(context.Background())

        // create new bulk processor from client
        bulkProcessor, err := elastic.NewBulkProcessorService(client).
        Workers(5).
        BulkActions(1000).
        FlushInterval(1 * time.Second).
        After(after).
        Do(context.Background())

        // now the bulk processor can be reused for entire the app
        myDoc := struct {
        Name string
        }{
        Name: "jack",
        }
        req := elastic.NewBulkIndexRequest()
        req.Index("my_index").Type("type").Id("my_doc_id").Doc(myDoc)

        // Use Add method to add request into the processor
        bulkProcessor.Add(req)

        // wait for sometime...
        time.Sleep(5 * time.Second)
        }

        func after(executionID int64, requests elastic.BulkableRequest, response *elastic.BulkResponse, err error) {
        if err != nil {
        log.Printf("bulk commit failed, err: %vn", err)
        }
        // do what ever you want in case bulk commit success
        log.Printf("commit successfully, len(requests)=%dn", len(requests))
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 10:53









        thanhpham

        763




        763






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424391%2fa-bulkindexer-in-olivere-package-for-golang-to-replace-elastigo%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Catalogne

            Violoncelliste

            Héron pourpré