Golang:如何构建用于Elasticsearch管道附件的结构体

huangapple go评论89阅读模式
英文:

Golang: How to construct a struct for Elasticsearch pipeline attachment

问题

我正在构建一个结构体,用于向Elasticsearch发送一个用于管道附件的PUT消息。

这是我应该发送给ES的JSON:

PUT _ingest/pipeline/attachment
{
"description": "处理文档",
"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 }}"
}
}
]
}

我真的需要创建一个结构体并将其编组以发送消息吗?(我来自Python)。设置管道是一个一次性的过程。

我在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"
}

如果你能看到代码,我在ASP structAS struct方面遇到了困难,对于处理器和以描述为附件的结构体

有人可以指导我吗?我甚至不确定我上面的结构体是否正确。

谢谢!

英文:

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!

答案1

得分: 0

我能成功完成这个任务,

我构建了如下的结构体:

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: "处理文档",
    Processors: []ASP{
        ASP{
            Attachment: ASPA{
                Field: "thedata",
                IndexedChars: -1,
            },
        },
    },
}

然后我发送了一个类似于这样的PUT请求
https://github.com/DaddyOh/golang-samples/blob/master/httpClient.go

我得到了如下结果:

{"acknowledged":true}
英文:

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}

答案2

得分: 0

你可以使用 logrus 包,它具有 ElasticSearch 钩子,并且非常简单易用。

英文:

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

huangapple
  • 本文由 发表于 2017年6月6日 20:38:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/44390244.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定