Skip to content

Commit

Permalink
Remove RunId from visibility query sort for ES 7 (#3140)
Browse files Browse the repository at this point in the history
  • Loading branch information
meiliang86 authored and alexshtin committed Jul 29, 2022
1 parent 0b28e9a commit 28cd79b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,20 @@ const (

// Default sort by uses the sorting order defined in the index template, so no
// additional sorting is needed during query.
// Keep RunID as tiebreaker because ES v6 date precision is milliseconds.
// ES v7 date precision is nanoseconds which is good enough for tie breaking.
// Remove this when we drop support for ES v6.
var defaultSorter = []elastic.Sorter{
elastic.NewFieldSort(searchattribute.CloseTime).Desc().Missing("_first"),
elastic.NewFieldSort(searchattribute.StartTime).Desc().Missing("_first"),
elastic.NewFieldSort(searchattribute.RunID).Desc().Missing("_first"),
}

var defaultSorterV7 = []elastic.Sorter{
elastic.NewFieldSort(searchattribute.CloseTime).Desc().Missing("_first"),
elastic.NewFieldSort(searchattribute.StartTime).Desc().Missing("_first"),
}

type (
visibilityStore struct {
esClient client.Client
Expand Down Expand Up @@ -639,7 +647,12 @@ func (s *visibilityStore) buildSearchParameters(
Index: s.index,
Query: boolQuery,
PageSize: request.PageSize,
Sorter: defaultSorter,
}

if _, isV7 := s.esClient.(client.ClientV7); isV7 {
params.Sorter = defaultSorterV7
} else {
params.Sorter = defaultSorter
}

if token != nil && len(token.SearchAfter) > 0 {
Expand Down Expand Up @@ -714,7 +727,11 @@ func (s *visibilityStore) convertQuery(

func (s *visibilityStore) setDefaultFieldSort(fieldSorts []*elastic.FieldSort) []elastic.Sorter {
if len(fieldSorts) == 0 {
return defaultSorter
if _, isV7 := s.esClient.(client.ClientV7); isV7 {
return defaultSorterV7
} else {
return defaultSorter
}
}

res := make([]elastic.Sorter, len(fieldSorts)+1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (s *ESVisibilitySuite) TestBuildSearchParameters() {
Query: boolQuery,
SearchAfter: []interface{}{json.Number("1528358645123456789"), "qwe"},
PageSize: testPageSize,
Sorter: defaultSorter,
Sorter: defaultSorterV7,
}, p)

// test request latestTime overflow
Expand All @@ -326,7 +326,7 @@ func (s *ESVisibilitySuite) TestBuildSearchParameters() {
Query: boolQuery,
SearchAfter: []interface{}{json.Number("1528358645123456789"), "qwe"},
PageSize: testPageSize,
Sorter: defaultSorter,
Sorter: defaultSorterV7,
}, p)
request = createTestRequest() // revert

Expand All @@ -340,7 +340,7 @@ func (s *ESVisibilitySuite) TestBuildSearchParameters() {
Query: boolQuery,
SearchAfter: nil,
PageSize: testPageSize,
Sorter: defaultSorter,
Sorter: defaultSorterV7,
}, p)

// test for additional boolQuery
Expand All @@ -354,7 +354,7 @@ func (s *ESVisibilitySuite) TestBuildSearchParameters() {
Query: boolQuery,
SearchAfter: nil,
PageSize: testPageSize,
Sorter: defaultSorter,
Sorter: defaultSorterV7,
}, p)

// test for search after
Expand All @@ -371,7 +371,7 @@ func (s *ESVisibilitySuite) TestBuildSearchParameters() {
Query: boolQuery,
SearchAfter: token.SearchAfter,
PageSize: testPageSize,
Sorter: defaultSorter,
Sorter: defaultSorterV7,
}, p)
request = createTestRequest() // revert

Expand All @@ -386,7 +386,7 @@ func (s *ESVisibilitySuite) TestBuildSearchParameters() {
Query: boolQuery,
PageSize: testPageSize,
SearchAfter: nil,
Sorter: defaultSorter,
Sorter: defaultSorterV7,
}, p)
}

Expand All @@ -411,7 +411,7 @@ func (s *ESVisibilitySuite) TestBuildSearchParametersV2() {
SearchAfter: nil,
PointInTime: nil,
PageSize: testPageSize,
Sorter: defaultSorter,
Sorter: defaultSorterV7,
}, p)
request.Query = ""

Expand Down Expand Up @@ -465,7 +465,7 @@ func (s *ESVisibilitySuite) TestBuildSearchParametersV2DisableOrderByClause() {
SearchAfter: nil,
PointInTime: nil,
PageSize: testPageSize,
Sorter: defaultSorter,
Sorter: defaultSorterV7,
}, p)
request.Query = ""

Expand Down

0 comments on commit 28cd79b

Please sign in to comment.