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

Update DSL query to allow filtering by missing start time #5017

Merged
merged 2 commits into from
Nov 16, 2022
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
15 changes: 15 additions & 0 deletions common/persistence/elasticsearch/esVisibilityStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ const (
jsonRangeOnExecutionTime = `{"range":{"ExecutionTime":`
jsonSortForOpen = `[{"StartTime":"desc"},{"RunID":"desc"}]`
jsonSortWithTieBreaker = `{"RunID":"desc"}`
jsonMissingStartTime = `{"missing":{"field":"StartTime"}}` //used to identify uninitialized workflow execution records

dslFieldSort = "sort"
dslFieldSearchAfter = "search_after"
Expand All @@ -504,6 +505,7 @@ var (
es.StartTime: true,
es.CloseTime: true,
es.ExecutionTime: true,
es.UpdateTime: true,
}
rangeKeys = map[string]bool{
"from": true,
Expand All @@ -514,6 +516,8 @@ var (
}
)

var missingStartTimeRegex = regexp.MustCompile(jsonMissingStartTime)

func getESQueryDSLForScan(request *p.ListWorkflowExecutionsByQueryRequest) (string, error) {
sql := getSQLFromListRequest(request)
dsl, err := getCustomizedDSLFromSQL(sql, request.DomainUUID)
Expand Down Expand Up @@ -601,6 +605,9 @@ func getCustomizedDSLFromSQL(sql string, domainID string) (*fastjson.Value, erro
return nil, err
}
dslStr = dsl.String()
if strings.Contains(dslStr, jsonMissingStartTime) { // isUninitialized
dsl = replaceQueryForUninitialized(dsl)
}
if strings.Contains(dslStr, jsonMissingCloseTime) { // isOpen
dsl = replaceQueryForOpen(dsl)
}
Expand All @@ -624,6 +631,14 @@ func replaceQueryForOpen(dsl *fastjson.Value) *fastjson.Value {
return dsl
}

// ES v6 only accepts "must_not exists" query instead of "missing" query, but elasticsql produces "missing",
// so use this func to replace.
func replaceQueryForUninitialized(dsl *fastjson.Value) *fastjson.Value {
newDslStr := missingStartTimeRegex.ReplaceAllString(dsl.String(), `{"bool":{"must_not":{"exists":{"field":"StartTime"}}}}`)
dsl = fastjson.MustParse(newDslStr)
return dsl
}

func addQueryForExecutionTime(dsl *fastjson.Value) {
executionTimeQueryString := `{"range" : {"ExecutionTime" : {"gt" : "0"}}}`
addMustQuery(dsl, executionTimeQueryString)
Expand Down
6 changes: 6 additions & 0 deletions common/persistence/elasticsearch/esVisibilityStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ func (s *ESVisibilitySuite) TestGetESQueryDSLForCount() {
dsl, err = getESQueryDSLForCount(request)
s.Nil(err)
s.Equal(`{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"bfd5c907-f899-4baf-a7b2-2ab85e623ebd"}}},{"bool":{"must":[{"range":{"ExecutionTime":{"gt":"0"}}},{"bool":{"must":[{"range":{"ExecutionTime":{"lt":"1000"}}}]}}]}}]}}}`, dsl)

request.Query = `StartTime = missing and UpdateTime >= "2022-10-04T16:00:00+07:00"`
dsl, err = getESQueryDSLForCount(request)
s.Nil(err)
s.Equal(`{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"bfd5c907-f899-4baf-a7b2-2ab85e623ebd"}}},{"bool":{"must":[{"bool":{"must_not":{"exists":{"field":"StartTime"}}}},{"range":{"UpdateTime":{"from":"1664874000000000000"}}}]}}]}}}`, dsl)

}

func (s *ESVisibilitySuite) TestAddDomainToQuery() {
Expand Down