Skip to content

Commit

Permalink
Fix between and on customized fields (#2062)
Browse files Browse the repository at this point in the history
* Fix between and on customized fields

* Add unit tests
  • Loading branch information
vancexu authored Jun 19, 2019
1 parent da72b54 commit 9f89425
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
12 changes: 11 additions & 1 deletion common/persistence/elasticsearch/esVisibilityStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ func (v *esVisibilityStore) getESQueryDSL(request *p.ListWorkflowExecutionsReque
dsl.Set(dslFieldFrom, fastjson.MustParse(strconv.Itoa(token.From)))
}

return dsl.String(), sortField, nil
dslStr := cleanDSL(dsl.String())

return dslStr, sortField, nil
}

func getSQLFromListRequest(request *p.ListWorkflowExecutionsRequestV2) string {
Expand Down Expand Up @@ -987,3 +989,11 @@ func timeProcessFunc(obj *fastjson.Object, key string, value *fastjson.Value) er
return nil
})
}

// elasticsql may transfer `Attr.Name` to "`Attr.Name`" instead of "Attr.Name" in dsl in some operator like "between and"
// this function is used to clean up
func cleanDSL(input string) string {
var re = regexp.MustCompile("(`)(Attr.\\w+)(`)")
result := re.ReplaceAllString(input, `$2`)
return result
}
19 changes: 19 additions & 0 deletions common/persistence/elasticsearch/esVisibilityStore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,3 +1085,22 @@ func (s *ESVisibilitySuite) getTokenHelper(sortValue interface{}) *esVisibilityP
token, _ = v.deserializePageToken(encoded)
return token
}

func (s *ESVisibilitySuite) TestCleanDSL() {
// dsl without `field`
dsl := `{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"2b8344db-0ed6-47a4-92fd-bdeb6ead93e3"}}},{"bool":{"must":[{"match_phrase":{"Attr.CustomIntField":{"query":"1"}}}]}}]}},"from":0,"size":10,"sort":[{"StartTime":"desc"},{"RunID":"desc"}]}`
res := cleanDSL(dsl)
s.Equal(dsl, res)

// dsl with `field`
dsl = `{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"2b8344db-0ed6-47a4-92fd-bdeb6ead93e3"}}},{"bool":{"must":[{"range":{"` + "`Attr.CustomIntField`" + `":{"from":"1","to":"5"}}}]}}]}},"from":0,"size":10,"sort":[{"StartTime":"desc"},{"RunID":"desc"}]}`
res = cleanDSL(dsl)
expected := `{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"2b8344db-0ed6-47a4-92fd-bdeb6ead93e3"}}},{"bool":{"must":[{"range":{"Attr.CustomIntField":{"from":"1","to":"5"}}}]}}]}},"from":0,"size":10,"sort":[{"StartTime":"desc"},{"RunID":"desc"}]}`
s.Equal(expected, res)

// dsl with mixed
dsl = `{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"2b8344db-0ed6-47a4-92fd-bdeb6ead93e3"}}},{"bool":{"must":[{"range":{"` + "`Attr.CustomIntField`" + `":{"from":"1","to":"5"}}},{"range":{"` + "`Attr.CustomDoubleField`" + `":{"from":"1.0","to":"2.0"}}},{"range":{"StartTime":{"gt":"0"}}}]}}]}},"from":0,"size":10,"sort":[{"StartTime":"desc"},{"RunID":"desc"}]}`
res = cleanDSL(dsl)
expected = `{"query":{"bool":{"must":[{"match_phrase":{"DomainID":{"query":"2b8344db-0ed6-47a4-92fd-bdeb6ead93e3"}}},{"bool":{"must":[{"range":{"Attr.CustomIntField":{"from":"1","to":"5"}}},{"range":{"Attr.CustomDoubleField":{"from":"1.0","to":"2.0"}}},{"range":{"StartTime":{"gt":"0"}}}]}}]}},"from":0,"size":10,"sort":[{"StartTime":"desc"},{"RunID":"desc"}]}`
s.Equal(expected, res)
}

0 comments on commit 9f89425

Please sign in to comment.