Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ES Scroll for Scan API #4614

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ type (
WaitForYellowStatus(ctx context.Context, index string) (string, error)
GetMapping(ctx context.Context, index string) (map[string]string, error)

OpenScroll(ctx context.Context, p *SearchParameters, keepAliveInterval string) (*elastic.SearchResult, error)
Scroll(ctx context.Context, id string, keepAliveInterval string) (*elastic.SearchResult, error)
CloseScroll(ctx context.Context, id string) error

IsPointInTimeSupported(ctx context.Context) bool
OpenPointInTime(ctx context.Context, index string, keepAliveInterval string) (string, error)
ClosePointInTime(ctx context.Context, id string) (bool, error)
}
Expand Down Expand Up @@ -79,6 +84,7 @@ type (
Sorter []elastic.Sorter

SearchAfter []interface{}
ScrollID string
PointInTime *elastic.PointInTime
}
)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ import (
"net/http"
"net/url"
"strings"
"sync"
"time"

"github.com/blang/semver/v4"
"github.com/olivere/elastic/v7"
"github.com/olivere/elastic/v7/uritemplates"
enumspb "go.temporal.io/api/enums/v1"
Expand All @@ -44,9 +46,20 @@ type (
clientImpl struct {
esClient *elastic.Client
url url.URL

initIsPointInTimeSupported sync.Once
isPointInTimeSupported bool
}
)

const (
pointInTimeSupportedFlavor = "default" // the other flavor is "oss"
)

var (
pointInTimeSupportedIn = semver.MustParseRange(">=7.10.0")
)

var _ Client = (*clientImpl)(nil)

// newClient create a ES client
Expand Down Expand Up @@ -138,6 +151,56 @@ func (c *clientImpl) Search(ctx context.Context, p *SearchParameters) (*elastic.
return searchService.Do(ctx)
}

func (c *clientImpl) OpenScroll(
ctx context.Context,
p *SearchParameters,
keepAliveInterval string,
) (*elastic.SearchResult, error) {
scrollService := elastic.NewScrollService(c.esClient).
Index(p.Index).
Query(p.Query).
SortBy(p.Sorter...).
KeepAlive(keepAliveInterval)
if p.PageSize != 0 {
scrollService.Size(p.PageSize)
}
return scrollService.Do(ctx)
}

func (c *clientImpl) Scroll(
ctx context.Context,
id string,
keepAliveInterval string,
) (*elastic.SearchResult, error) {
return elastic.NewScrollService(c.esClient).ScrollId(id).KeepAlive(keepAliveInterval).Do(ctx)
}

func (c *clientImpl) CloseScroll(ctx context.Context, id string) error {
return elastic.NewScrollService(c.esClient).ScrollId(id).Clear(ctx)
}

func (c *clientImpl) IsPointInTimeSupported(ctx context.Context) bool {
c.initIsPointInTimeSupported.Do(func() {
c.isPointInTimeSupported = c.queryPointInTimeSupported(ctx)
})
return c.isPointInTimeSupported
}

func (c *clientImpl) queryPointInTimeSupported(ctx context.Context) bool {
result, _, err := c.esClient.Ping(c.url.String()).Do(ctx)
if err != nil {
return false
}
if result == nil || result.Version.BuildFlavor != pointInTimeSupportedFlavor {
return false
}
esVersion, err := semver.ParseTolerant(result.Version.Number)
if err != nil {
return false
}
return pointInTimeSupportedIn(esVersion)
}

func (c *clientImpl) OpenPointInTime(ctx context.Context, index string, keepAliveInterval string) (string, error) {
resp, err := c.esClient.OpenPointInTime(index).KeepAlive(keepAliveInterval).Do(ctx)
if err != nil {
Expand Down
Loading