如何使用golang

时间:2017-02-02 19:40:23

标签: elasticsearch go

我正在使用gopkg.in/olivere/elastic.v5,我正在尝试使用golang将数据从json文件导入到elasticsearch数据库。这是我的代码

package main

import(
    "gopkg.in/olivere/elastic.v5"
    "golang.org/x/net/context"
    "log"
    "os"
    "encoding/json"
)

type people struct{
    Firstname string `json:"firstname"`
    Lastname string `json:"lastname"`
    Institution string `json:"institution"`
    Email string `json:"email"`
}

type item struct{
    Id string `json:"id"`
    Title string `json:"title"`
    Journal  string `json:"journal"`
    Volume int `json:"volume"`
    Number int `json:"number"`
    Pages string `json:"pages"`
    Year int `json:"year"`
    Authors []people `json:"authors"`
    Abstract string `json:"abstract"`
    Link string `json:"link"`
    Keywords []string `json:"keywords"`
    Body string `json:"body"`
}

var client *elastic.Client
var err error
func init(){
    client,err = elastic.NewClient()
    if err!=nil{
        log.Fatal(err)
    }
}

func main() {
    var data []item

    file,err := os.Open("data.json")
    if err!=nil{
        log.Fatal(err)
    }
    defer file.Close()

    jsonDeocder :=  json.NewDecoder(file)
    if err := jsonDeocder.Decode(&data); err!=nil{
        log.Fatal("Decode: ",err)
    }

    bulkIndex("library","article",data)
}

func bulkIndex(index string,typ string ,data []item){
    ctx := context.Background()
    for _,item := range data{
        _,err := client.Index().Index(index).Type(typ).BodyJson(item).Do(ctx)   
        if err !=nil{
            log.Fatal(err)
        }
    }   
}

包文档非常庞大,我不确定我是否采用了正确的方法。这编译得很好,但是在运行之后,当我使用GET /library/article/575084573a2404eec25acdcd?pretty检查我的elasticsearch数据库时使用575084573a2404eec25acdcd{ "_index": "library", "_type": "article", "_id": "575084573a2404eec25acdcd", "found": false } 是我的json文件中的正确ID),我收到以下响应

GET /library?pretty

如何导入数据?

编辑:这就是我在kibana上做{ "library": { "aliases": {}, "mappings": { "article": { "properties": { "abstract": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "authors": { "properties": { "email": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "firstname": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "institution": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "lastname": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, "body": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "id": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "journal": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "keywords": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "link": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "number": { "type": "long" }, "pages": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "title": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } }, "volume": { "type": "long" }, "year": { "type": "long" } } } }, "settings": { "index": { "creation_date": "1486063182258", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "_SLeDWb4QPinFcSwOCUtCw", "version": { "created": "5020099" }, "provided_name": "library" } } } }

--name-status

1 个答案:

答案 0 :(得分:2)

好的,我明白了。我应该为我的项目指定Id,而不是仅指定索引和类型。

正确的陈述应该是

_,err := client.Index().Index(index).Type(typ).Id(item.Id).BodyJson(item).Do(ctx)