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

huangapple go评论146阅读模式
英文:

How to add mapping via go-elasticsearch package

问题

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

我想通过我的Go语言服务来设置它,但似乎go-elasticsearch包不支持?如果我错了,请纠正我。

英文:

Basically when I am trying to insert a new document to non-existing index, it is automatically sets to dynamic mapping.
But I have issues where in sometimes I want to change the data type of the fields on the ES.

I want to set it via my go-lang service but looks like go-elasticsearch package doesn't support it? correct me if I am wrong

答案1

得分: 2

你可以使用go-elasticsearch/esapi go-elasticsearch/esapi 创建一个带有映射的索引。

创建一个如下所示的请求:

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 - 传递索引名称
// Body - 传递映射、设置等
indexReq := esapi.IndicesCreateRequest{
	Index: "my-index",
	Body: strings.NewReader(string(mapping)),
}

resp, err := indexReq.Do(ctx, elasticclient)
if err != nil {
	// 处理错误
}

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

英文:

You can create an index along with its mapping using go-elasticsearch/esapi go-elasticsearch/esapi

Create a request as below:

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
}

by this way you can create a new index with specific mapping

答案2

得分: 0

你可以使用ElasticSearch Typed API

创建索引的代码如下:

client := GetTypedClient()

// 如果索引不存在,则创建索引
index := "index_name"
if !isIndexExists(GetClient(), index) {
    res, err := client.Indices.Create(getIndexName(index)).Do(context.Background())
    if err != nil {
        ...
    }
}

还可以使用以下代码来设置映射

// 更新映射
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 {
    ...
}
英文:

You can use ElasticSearch Typed API

To create an index

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

And put mapping as well

	// 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 {
		...
	}

huangapple
  • 本文由 发表于 2022年5月4日 23:10:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/72115342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定