英文:
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 struct
和AS 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论