From 3499e5c24c0b176c900f4191dac9176126035725 Mon Sep 17 00:00:00 2001 From: Oliver Eilhard Date: Mon, 30 Aug 2021 18:13:29 +0200 Subject: [PATCH] PointInTime API: Make keep alive optional The keep alive parameter for the PIT is optional. Close #1524 --- pit.go | 21 ++++++++++++++++----- pit_test.go | 2 +- search_source_test.go | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pit.go b/pit.go index 22871309..49ecb9ca 100644 --- a/pit.go +++ b/pit.go @@ -17,7 +17,15 @@ type PointInTime struct { } // NewPointInTime creates a new PointInTime. -func NewPointInTime(id, keepAlive string) *PointInTime { +func NewPointInTime(id string) *PointInTime { + return &PointInTime{ + Id: id, + } +} + +// NewPointInTimeWithKeepAlive creates a new PointInTime with the given +// time to keep alive. +func NewPointInTimeWithKeepAlive(id, keepAlive string) *PointInTime { return &PointInTime{ Id: id, KeepAlive: keepAlive, @@ -29,8 +37,11 @@ func (pit *PointInTime) Source() (interface{}, error) { if pit == nil { return nil, nil } - return map[string]interface{}{ - "id": pit.Id, - "keep_alive": pit.KeepAlive, - }, nil + m := map[string]interface{}{ + "id": pit.Id, + } + if pit.KeepAlive != "" { + m["keep_alive"] = pit.KeepAlive + } + return m, nil } diff --git a/pit_test.go b/pit_test.go index 6e521fbc..c97ff832 100644 --- a/pit_test.go +++ b/pit_test.go @@ -76,7 +76,7 @@ func TestPointInTimeLifecycle(t *testing.T) { searchResult, err := client.Search(). // Index(testIndexName). // <-- you may not use indices with PointInTime! Query(NewMatchAllQuery()). - PointInTime(NewPointInTime(pitResp.Id, "1m")). + PointInTime(NewPointInTimeWithKeepAlive(pitResp.Id, "1m")). Size(100). Pretty(true). Do(context.TODO()) diff --git a/search_source_test.go b/search_source_test.go index 4b2852ac..1427e20f 100644 --- a/search_source_test.go +++ b/search_source_test.go @@ -320,7 +320,7 @@ func TestSearchSourceSeqNoAndPrimaryTerm(t *testing.T) { func TestSearchSourcePointInTime(t *testing.T) { matchAllQ := NewMatchAllQuery() builder := NewSearchSource().Query(matchAllQ).PointInTime( - NewPointInTime("pit_id", "2m"), + NewPointInTimeWithKeepAlive("pit_id", "2m"), ) src, err := builder.Source() if err != nil {