elasticsearch - 如何使用golang将数据导入elasticsearch

标签 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 检查 kibana 上的 elasticsearch 数据库时(575084573a2404eec25acdcd 是我的 json 文件中的正确 ID ), 我收到以下回复

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

如何导入我的数据?

编辑:这就是我在 kibana 上做的 GET/library?pretty

{
  "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"
      }
    }
  }
}

最佳答案

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

正确的说法应该是

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

关于elasticsearch - 如何使用golang将数据导入elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42010937/

相关文章:

go - 当 defer func 评估其参数时

Elasticsearch : Root mapping definition has unsupported parameters index : not_analyzed

ruby-on-rails - 通过关联ID的轮胎 Elasticsearch 过滤器

javascript - 语言/Javascript : Empty postForm and decode(body) on JSON POST

go - 如何用golang实现slowEqual

mongodb - filter:= bson.D {{“hello”,“world”}}}而不是使用value(world)如何传递包含该值的变量(world)

对嵌套对象数组的 Elasticsearch 查询

php - 在 Elasticsearch 格式错误的查询中按日期范围搜索

docker - Docker,是否可以将主机数据复制到Docker卷?

Go 1.0.2 zlib 压缩时如何提高速度