Skip to content

Commit

Permalink
Update DSL query to allow filtering by missing start time and get cou…
Browse files Browse the repository at this point in the history
…nt of uninitialized wf execution records (#3081)
  • Loading branch information
neil-xie committed Nov 1, 2022
1 parent 9b3f0de commit 4699ae3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
14 changes: 14 additions & 0 deletions common/persistence/elasticsearch/esVisibilityStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,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 @@ -503,6 +504,7 @@ var (
es.StartTime: true,
es.CloseTime: true,
es.ExecutionTime: true,
es.UpdateTime: true,
}
rangeKeys = map[string]bool{
"from": true,
Expand Down Expand Up @@ -600,6 +602,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 @@ -623,6 +628,15 @@ 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 {
re := regexp.MustCompile(jsonMissingStartTime)
newDslStr := re.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 @@ -746,6 +746,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

0 comments on commit 4699ae3

Please sign in to comment.