dongtangjie0495 2015-04-20 23:38
浏览 88

Golang并从Mongo对象为Elasticsearch创建嵌套索引

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大