I'm learning Golang. I'm trying index a nested object from Mongo into a nested index Elasticsearch.
For the Mongo objects I created two structs:
type Parent struct {
Client_Mac string
Visit NestedVisit `json:"visits" elastic:"type:nested"`
}
type NestedVisit struct {
Last_Seen time.Time `json:"last_seen" elastic:"type:date"`
}
I'm just need to loop through the array and index each one, including the nested index.
For (my own) speed, I created a nested mapping outside Golang:
{
"test": {
"mappings": {
"test": {
"properties": {
"client_mac": {
"type": "string"
},
"visits": {
"type": "nested",
"properties": {
"last_seen": {
"type": "date",
"format": "dateOptionalTime"
},
}
}
}
}
}
}
}
I started indexing the objects as so by looping through the Mongo object. In my main function, I have this:
func main() {
db := session.DB("test")
data := GetData(db)
for _, each := range data {
ReIndex(each)
}
}
The ReIndex function looks as so:
func ReIndex(s Parent) {
var m map[string]int
m = make(map[string]int)
for _, each := range s.Visits {
m["last_seen"] = each.Last_Seen
}
fmt.Printf("Test: %v
", m)
visit1 := Parent{Client_Mac: s.Client_Mac, Visit: NestedVisit{Last_Seen: 1000}}
put1, err := el.Index().
Index("test").
Type("test").
Id(s.Id.Hex()).
BodyJson(visit1).
Do()
if err != nil {
panic(err)
}
fmt.Printf("Indexed test %s to index %s, type %s
", put1.Id, put1.Index, put1.Type)
}
Which was ok but I need the Visits to be an array of visits.
So, within the loop, I tried looping through the Visit array and created a map:
var m map[string]int
m = make(map[string]int)
for _, each := range s.Visits {
m["last_seen"] = each.Last_Seen
}
I then tried changing the Parent struct:
type Parent struct {
...
Visit interface{} `json:"visits" elastic:"type:nested"`
}
visit1 := Parent{Client_Mac: s.Client_Mac, Visit: m}
But the index looks like this:
"visits":{"last_seen":1000}
Could someone explain what type Visits should have to facilitate an array?
Ps. I'm using mgo and the elastic search client.