rnk
rnk

Reputation: 2204

Golang: How to construct a struct for Elasticsearch pipeline attachment

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!

Upvotes: 1

Views: 609

Answers (2)

Ami
Ami

Reputation: 357

you can use logrus package which has elastic search hook and is simple enough.

Upvotes: 0

rnk
rnk

Reputation: 2204

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}

Upvotes: 0

Related Questions