2017-02-02 4 views
1

私はgopkg.in/olivere/elastic.v5を使用していますが、golangを使用してjsonファイルからelasticsearch DBにデータをインポートしようとしています。これは私のコードですgolangを使用して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を使用してkibanaに私elasticsearch DBを確認したときにこれが(575084573a2404eec25acdcdは私のJSONファイルから正しいIDです)、罰金が、これを実行した後にコンパイルし、私は私のデータをインポートするにはどうすればよい次の応答に

{ 
    "_index": "library", 
    "_type": "article", 
    "_id": "575084573a2404eec25acdcd", 
    "found": false 
} 

を取得していますか?

編集:これは私が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" 
     } 
    } 
    } 
} 
+0

でなければなりません。それが問題になるかもしれませんか? – Niko

+0

https://godoc.org/gopkg.in/olivere/elastic.v5によれば、Elasticsearchが 'localhost:9200'で実行されている場合、' NewClient() 'のアドレスを指定する必要はありません。 – Krash

+0

良いです。チェックしてるだけ。 – Niko

答えて

2

OKをGET /library?prettyを行う上で得るものです、私はそれを得ました。インデックスとタイプを指定するのではなく、アイテムのIDを指定する必要があります。

正しい文は、私はあなたが弾性クライアントの初期化時に、あなたのelasticsearchクラスタのURLを設定する表示されていない

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

知っておいてよかったですが、私はIDを設定しましたが、設定されていなければelasticsearchが自動的にそれを行うと仮定しました – Niko

+0

あなたの助けと時間をありがとう。 :D – Krash

関連する問題