Skip to content

Commit

Permalink
Fix list open workflow within OR clause (#2281)
Browse files Browse the repository at this point in the history
  • Loading branch information
vancexu committed Jul 26, 2019
1 parent cacbd21 commit 797e4c9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
16 changes: 8 additions & 8 deletions common/persistence/elasticsearch/esVisibilityStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,11 @@ var (
"ExecutionTime": true,
}
rangeKeys = map[string]bool{
"from": true,
"to": true,
"gt": true,
"lt": true,
"from": true,
"to": true,
"gt": true,
"lt": true,
"query": true,
}
)

Expand Down Expand Up @@ -555,14 +556,13 @@ func getCustomizedDSLFromSQL(sql string, domainID string) (*fastjson.Value, erro
return dsl, nil
}

// ES v6 only accepts "must_not exists" query instead of "missing" query, but elasticsql will produce "missing",
// ES v6 only accepts "must_not exists" query instead of "missing" query, but elasticsql produces "missing",
// so use this func to replace.
// Note it also means a temp limitation that we cannot support field missing search
func replaceQueryForOpen(dsl *fastjson.Value) *fastjson.Value {
re := regexp.MustCompile(`(,)?` + jsonMissingCloseTime + `(,)?`)
newDslStr := re.ReplaceAllString(dsl.String(), "")
re := regexp.MustCompile(jsonMissingCloseTime)
newDslStr := re.ReplaceAllString(dsl.String(), `{"bool":{"must_not":{"exists":{"field":"CloseTime"}}}}`)
dsl = fastjson.MustParse(newDslStr)
dsl.Get("query").Get("bool").Set("must_not", fastjson.MustParse(`{"exists": {"field": "CloseTime"}}`))
return dsl
}

Expand Down
16 changes: 13 additions & 3 deletions common/persistence/elasticsearch/esVisibilityStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,13 +707,23 @@ func (s *ESVisibilitySuite) TestGetESQueryDSL() {
dsl, sortField, err = v.getESQueryDSL(request, token)
s.Nil(err)
s.Equal(definition.StartTime, sortField)
s.Equal(`{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"bfd5c907-f899-4baf-a7b2-2ab85e623ebd"}}},{"bool":{"must":[{"match_phrase":{"WorkflowID":{"query":"wid"}}}],"must_not":{"exists":{"field":"CloseTime"}}}}]}},"from":0,"size":10,"sort":[{"StartTime":"desc"},{"RunID":"desc"}]}`, dsl)
s.Equal(`{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"bfd5c907-f899-4baf-a7b2-2ab85e623ebd"}}},{"bool":{"must":[{"match_phrase":{"WorkflowID":{"query":"wid"}}},{"bool":{"must_not":{"exists":{"field":"CloseTime"}}}}]}}]}},"from":0,"size":10,"sort":[{"StartTime":"desc"},{"RunID":"desc"}]}`, dsl)

request.Query = `WorkflowID = 'wid' or CloseTime = missing`
dsl, _, err = v.getESQueryDSL(request, token)
s.Nil(err)
s.Equal(`{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"bfd5c907-f899-4baf-a7b2-2ab85e623ebd"}}},{"bool":{"should":[{"match_phrase":{"WorkflowID":{"query":"wid"}}},{"bool":{"must_not":{"exists":{"field":"CloseTime"}}}}]}}]}},"from":0,"size":10,"sort":[{"StartTime":"desc"},{"RunID":"desc"}]}`, dsl)

request.Query = `CloseTime = missing order by CloseTime desc`
dsl, sortField, err = v.getESQueryDSL(request, token)
s.Nil(err)
s.Equal(definition.CloseTime, sortField)
s.Equal(`{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"bfd5c907-f899-4baf-a7b2-2ab85e623ebd"}}},{"bool":{"must":[],"must_not":{"exists":{"field":"CloseTime"}}}}]}},"from":0,"size":10,"sort":[{"CloseTime":"desc"},{"RunID":"desc"}]}`, dsl)
s.Equal(`{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"bfd5c907-f899-4baf-a7b2-2ab85e623ebd"}}},{"bool":{"must":[{"bool":{"must_not":{"exists":{"field":"CloseTime"}}}}]}}]}},"from":0,"size":10,"sort":[{"CloseTime":"desc"},{"RunID":"desc"}]}`, dsl)

request.Query = `StartTime = "2018-06-07T15:04:05-08:00"`
dsl, _, err = v.getESQueryDSL(request, token)
s.Nil(err)
s.Equal(`{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"bfd5c907-f899-4baf-a7b2-2ab85e623ebd"}}},{"bool":{"must":[{"match_phrase":{"StartTime":{"query":"1528412645000000000"}}}]}}]}},"from":0,"size":10,"sort":[{"StartTime":"desc"},{"RunID":"desc"}]}`, dsl)

request.Query = `WorkflowID = 'wid' and StartTime > "2018-06-07T15:04:05+00:00"`
dsl, sortField, err = v.getESQueryDSL(request, token)
Expand Down Expand Up @@ -784,7 +794,7 @@ func (s *ESVisibilitySuite) TestGetESQueryDSLForScan() {
request.Query = `CloseTime = missing and (ExecutionTime >= "2019-08-27T15:04:05+00:00" or StartTime <= "2018-06-07T15:04:05+00:00")`
dsl, err = getESQueryDSLForScan(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":[{"bool":{"should":[{"range":{"ExecutionTime":{"from":"1566918245000000000"}}},{"range":{"StartTime":{"to":"1528383845000000000"}}}]}}],"must_not":{"exists":{"field":"CloseTime"}}}}]}}]}},"from":0,"size":10}`, dsl)
s.Equal(`{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"bfd5c907-f899-4baf-a7b2-2ab85e623ebd"}}},{"bool":{"must":[{"range":{"ExecutionTime":{"gt":"0"}}},{"bool":{"must":[{"bool":{"must_not":{"exists":{"field":"CloseTime"}}}},{"bool":{"should":[{"range":{"ExecutionTime":{"from":"1566918245000000000"}}},{"range":{"StartTime":{"to":"1528383845000000000"}}}]}}]}}]}}]}},"from":0,"size":10}`, dsl)

request.Query = `ExecutionTime < 1000 and ExecutionTime > 500`
dsl, err = getESQueryDSLForScan(request)
Expand Down

0 comments on commit 797e4c9

Please sign in to comment.