其他分享
首页 > 其他分享> > elastic v7批量写数据golang

elastic v7批量写数据golang

作者:互联网

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "github.com/olivere/elastic"
    "log"
)

func main() {

    client, err := elastic.NewClient(elastic.SetURL("http://192.168.2.10:9201"),
        elastic.SetBasicAuth("user", "secret"))
    if err != nil {
        // Handle error
        panic(err)
    }

    // Index a second tweet (by string)
    tweet2 := `[{"user" : "jack", "message" : "hello world"}]`
    var m []map[string]string
    if err := json.Unmarshal([]byte(tweet2), &m); err != nil {
        fmt.Errorf("err:%v", err)
    }
    log.Println(tweet2)

    if len(tweet2) > 0 {
        for _, v := range m {
            p, err := client.Index().
                Index("twitter").
                Type("doc").BodyJson(v).Do(context.Background())
            if err != nil {
                // Handle error
                panic(err)
            }
            fmt.Printf("Indexed tweet %s to index %s, type %s\n", p.Id, p.Index, p.Type)
        }
    }
}

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

标签:Index,elastic,err,golang,location,v7,tweet2,string
来源: https://www.cnblogs.com/xuweiqiang/p/16251874.html