dongluni0568 2017-06-06 12:38
浏览 121
已采纳

Golang:如何为Elasticsearch管道附件构建结构

I'm constructing a struct to send a put message to Elasticsearch for pipeline attachment.

This is the json that I'm supposed to send it to ES:

PUT _ingest/pipeline/attachment 
{   
    "description": "Process documents",   
    "processors": [
        {
            "attachment": {
                "field": "thedata",
                "indexed_chars": -1
            }
        },
        {
            "set": {
                "field": "attachment.title",
                "value": "{{ title }}"
            }
        },
        {
            "set": {
                "field": "attachment.author",
                "value": "{{ author }}"
            }
        },
        {
            "set": {
                "field": "attachment.url",
                "value": "{{ url }}"
            }
        },
        {
            "set": {
                "field": "attachment.cover",
                "value": "{{ cover }}"
            }
        },
        {
            "set": {
                "field": "attachment.page",
                "value": "{{ page }}"
            }
        }
    ] 
}

Do I really need to make a struct and marshal it in order to put the message? (I'm coming from Python). Setting up pipeline is a single time process.

I'm constructing structs like this in Go:

type AS struct {
    Description string

}

type ASP struct {
    Processors 
}

type ASPA struct {
    Attachment []ASPAF `json:"attachment"`
}

type ASPAF struct {
    Field string `json:"field"`
    IndexedChars uint64 `json:"indexed_chars"`
}

type ASPS struct {
    Set []ASPSF `json:"set"`
}

type ASPSF struct {
    Field string `json:"field"`
    Value string `json:"value"`
}

If you can see the code, I'm stuck at ASP struct and AS struct for processors and rounding up as attachment struct with description.

Could someone guide me? I'm not even sure if I'm right with the above structs.

Thanks!

  • 写回答

2条回答 默认 最新

  • dst2007 2017-06-06 14:44
    关注

    I'm able to do this successfully,

    I have constructed structs like this:

    type AS struct {
        Description string `json:"description"`
        Processors []ASP `json:"processors"`
    }
    
    type ASP struct {
        Attachment ASPA `json:"attachment"`
    }
    
    type ASPA struct {
        Field string `json:"field"`
        IndexedChars int64 `json:"indexed_chars"`
    }
    
    attachment := &AS{
        Description: "Process documents",
        Processors: []ASP{
            ASP{
                Attachment: ASPA{
                    Field: "thedata",
                    IndexedChars: -1,
                },
            },
        },
    }
    

    Then I sent a put request similar to this https://github.com/DaddyOh/golang-samples/blob/master/httpClient.go

    I got the result:

    {"acknowledged":true}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?