登录
首页 >  Golang >  Go问答

如何通过 go-elasticsearch 包添加映射

来源:stackoverflow

时间:2024-04-27 09:51:35 103浏览 收藏

“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《如何通过 go-elasticsearch 包添加映射》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!

问题内容

基本上,当我尝试将新文档插入到不存在的索引时,它会自动设置为动态映射。 但我遇到了一些问题,有时我想更改 ES 上字段的数据类型。

我想通过我的 go-lang 服务设置它,但看起来 go-elasticsearch 包不支持它?如果我错了请纠正我


正确答案


您可以使用ElasticSearch Typed API

创建索引

1
2
3
4
5
6
7
8
9
10
client := gettypedclient()
 
// create index if not exists
index := "index_name"
if !isindexexists(getclient(), index) {
    res, err := client.indices.create(getindexname(index)).do(context.background())
    if err != nil {
        ...
    }
}

放置映射以及

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// update mapping
memorymapping := types.newnestedproperty()
memorymapping.properties = map[string]types.property{
    "total": types.newintegernumberproperty(),
    "free":  types.newintegernumberproperty(),
    "used":  types.newintegernumberproperty(),
}
 
uptimemapping := types.newnestedproperty()
uptimemapping.properties = map[string]types.property{
    "app": types.newintegernumberproperty(),
    "os":  types.newintegernumberproperty(),
}
 
loadmapping := types.newnestedproperty()
loadmapping.properties = map[string]types.property{
    "avg1":  types.newfloatnumberproperty(),
    "avg5":  types.newfloatnumberproperty(),
    "avg15": types.newfloatnumberproperty(),
}
 
res, err := client.indices.putmapping(index).
    request(&putmapping.request{
        properties: map[string]types.property{
            "createdat": types.newdateproperty(),
            "memory":    memorymapping,
            "uptime":    uptimemapping,
            "load":      loadmapping,
        },
    }).
    do(context.background())
 
if err != nil {
    ...
}

您可以使用 go-elasticsearch/esapi go-elasticsearch/esapi 创建索引及其映射

创建如下请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
mapping := `{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1
      },
      "mappings": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "message": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
        }
}`
 
// Index - pass index name
// Body - pass mapping, settings etc
indexReq := esapi.IndicesCreateRequest{
    Index: "my-index",
    Body: strings.NewReader(string(mapping)),
}
 
resp, err := indexReq.Do(ctx, elasticclient)
if err != nil {
    // handle error
}

通过这种方式,您可以创建具有特定映射的新索引

今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>