Elasticsearch Point In Time 请求 API 在 Golang 中的使用

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

Elasticsearch Point In Time Request API in Golang

问题

我正在尝试使用官方的go-elasticsearch库,在golang中使用点在时间(Point in Time)API。我似乎找不到任何解释如何使用它的文档。

我已经能够创建一个OpenPointInTime对象并检索到一个PIT id。我无法弄清楚该如何使用它,或者在elasticsearch.Client.Search函数中放置它的位置。我也没有找到示例。

有人可以给出一个使用官方库的基本示例吗?

英文:

I'm trying to use the point in time api in golang using the official go-elasticsearch library. I can't seem to find any documentation that explains how to use it.

I've been able to create a OpenPointInTime object and retrieve a PIT id. I can't figure out what to do with it or where to place it in the elasticsearch.Client.Search function. I haven't been able to to find an example either.
Can anyone give a basic example using the official library.

答案1

得分: 1

在浏览了elasticsearch库的GitHub存储库上的已关闭问题后,我找到了这个问题线程:https://github.com/elastic/go-elasticsearch/issues/234

根据这个线程,我需要从OpenPointInTime响应中获取一个PIT id,并将其添加到请求体中。
以下代码对我有效:

var query_buffer bytes.Buffer
body := `
	{
		"query": {
			"term": {
				"_id": "AkUN7YUB2JzVdyKtJ8bD"
			}
		},
		"pit": {
			"id":  "your pit id here", 
			"keep_alive": "3m"  
		}
	}
`
es, _ := elasticsearch.NewDefaultClient()
json.NewDecoder(&query_buffer).Decode(&body)
res, err := es.Search(
	es.Search.WithAllowPartialSearchResults(true),
	es.Search.WithBody(&query_buffer),
)

请注意,这只是一个示例代码片段,具体的实现可能因你的需求而有所不同。

英文:

After going through the closed issues on the github repo of the elasticsearch library, I found this issue thread: https://github.com/elastic/go-elasticsearch/issues/234

According to this thread, I need to get a PIT id from OpenPointInTime response and add it to the body.
This worked for me:

var query_buffer bytes.Buffer
body := `
	{
		"query": {
			"term": {
				"_id": "AkUN7YUB2JzVdyKtJ8bD"
			}
		},
		"pit": {
			"id":  "your pit id here", 
			"keep_alive": "3m"  
		}
	}
`
es, _ := elasticsearch.NewDefaultClient()
json.NewDecoder(&query_buffer).Decode(&body)
res, err := es.Search(
	es.Search.WithAllowPartialSearchResults(true),
	es.Search.WithBody(&query_buffer),
)

huangapple
  • 本文由 发表于 2023年1月30日 23:29:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75286550.html
匿名

发表评论

匿名网友

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

确定