簡體   English   中英

Golang 中的 Elasticsearch 時間點請求 API

[英]Elasticsearch Point In Time Request API in Golang

我正在嘗試使用官方go- elasticsearch 庫在 golang 中使用時間點 api。 我似乎找不到任何說明如何使用它的文檔。

我已經能夠創建一個OpenPointInTime object 並檢索一個 PIT id。 我不知道如何處理它或將它放在elasticsearch.Client.Search function 中的什么位置。我也找不到示例。 任何人都可以使用官方圖書館給出一個基本示例。

下面是一個基本示例,說明如何使用官方 Go 庫 go-elasticsearch 在 Elasticsearch 搜索請求中使用時間點 (PIT) id:

package main

import (
    "context"
    "fmt"

    "github.com/elastic/go-elasticsearch/v8"
)

func main() {
    es, _ := elasticsearch.NewDefaultClient()

    res, err := es.Search(
        es.Search.WithContext(context.Background()),
        es.Search.WithIndex("my_index"),
        es.Search.WithBody(`{
            "query": {
                "match_all": {}
            }
        }`),
        es.Search.WithPointInTime("PIT-id-here"),
    )
    if err != nil {
        fmt.Println(err)
        return
    }
    defer res.Body.Close()

    // access the search results
    // ...
}

在此示例中,我們使用 es.search() function 中的 point_in_time 參數來指定 PIT id,然后使用 for 循環檢索搜索結果以訪問 res['hits']['hits '] 大批。

在瀏覽了 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),
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM