基本上,当我试图插入一个新的文档到不存在的索引,它是自动设置为动态Map。但是我有时会遇到一些问题,我想更改ES上字段的数据类型。我想通过我的go-lang服务设置它,但看起来go-elasticsearch包不支持它?如果我错了请纠正我
q7solyqu1#
您可以使用go-elasticsearch/esapigo-elasticsearch/esapi创建索引沿着Map创建一个请求如下:
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 }
通过这种方式,您可以创建一个具有特定Map的新索引
guicsvcw2#
可以使用ElasticSearch Typed API
创建索引
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 { ... } }
还有put mapping
// 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 { ... }
2条答案
按热度按时间q7solyqu1#
您可以使用go-elasticsearch/esapigo-elasticsearch/esapi创建索引沿着Map
创建一个请求如下:
通过这种方式,您可以创建一个具有特定Map的新索引
guicsvcw2#
可以使用ElasticSearch Typed API
创建索引
还有put mapping