diff --git a/changelog/unreleased/fix-last-month-search.md b/changelog/unreleased/fix-last-month-search.md new file mode 100644 index 0000000000..7dd219f268 --- /dev/null +++ b/changelog/unreleased/fix-last-month-search.md @@ -0,0 +1,8 @@ +Bugfix: Fix last month search + +We've fixed the last month search edge case when currently is 31-th. + +https://github.com/owncloud/ocis/issues/7629 +https://github.com/owncloud/ocis/pull/7742 + +The issue is related to the build-in package behavior https://github.com/golang/go/issues/31145 diff --git a/services/search/pkg/query/kql/cast.go b/services/search/pkg/query/kql/cast.go index eb5d043611..06fb800ce5 100644 --- a/services/search/pkg/query/kql/cast.go +++ b/services/search/pkg/query/kql/cast.go @@ -115,7 +115,7 @@ func toTimeRange(in interface{}) (*time.Time, *time.Time, error) { from = n.BeginningOfMonth() to = n.EndOfMonth() case "last month": - lastMonth := n.With(n.AddDate(0, -1, 0)) + lastMonth := n.With(n.BeginningOfMonth().AddDate(0, 0, -1)) from = lastMonth.BeginningOfMonth() to = lastMonth.EndOfMonth() case "last 30 days": diff --git a/services/search/pkg/query/kql/dictionary_test.go b/services/search/pkg/query/kql/dictionary_test.go index 56aa326955..ba2780db89 100644 --- a/services/search/pkg/query/kql/dictionary_test.go +++ b/services/search/pkg/query/kql/dictionary_test.go @@ -694,6 +694,72 @@ func TestParse_DateTimeRestrictionNode(t *testing.T) { }, }, }, + { + name: "NaturalLanguage DateTimeNode - last month - edge case when last day of the month", + patch: setWorldClock(t, "2023-10-31"), + query: join([]string{ + `Mtime:"last month"`, + }), + ast: &ast.Ast{ + Nodes: []ast.Node{ + &ast.DateTimeNode{ + Key: "Mtime", + Operator: &ast.OperatorNode{Value: ">="}, + Value: mustParseTime(t, "2023-09-01"), + }, + &ast.OperatorNode{Value: kql.BoolAND}, + &ast.DateTimeNode{ + Key: "Mtime", + Operator: &ast.OperatorNode{Value: "<="}, + Value: mustParseTime(t, "2023-09-30 23:59:59.999999999"), + }, + }, + }, + }, + { + name: "NaturalLanguage DateTimeNode - last month - edge case when last day of the month", + patch: setWorldClock(t, "2023-03-31"), + query: join([]string{ + `Mtime:"last month"`, + }), + ast: &ast.Ast{ + Nodes: []ast.Node{ + &ast.DateTimeNode{ + Key: "Mtime", + Operator: &ast.OperatorNode{Value: ">="}, + Value: mustParseTime(t, "2023-02-01"), + }, + &ast.OperatorNode{Value: kql.BoolAND}, + &ast.DateTimeNode{ + Key: "Mtime", + Operator: &ast.OperatorNode{Value: "<="}, + Value: mustParseTime(t, "2023-02-28 23:59:59.999999999"), + }, + }, + }, + }, + { + name: "NaturalLanguage DateTimeNode - last month - edge case when last day of the month", + patch: setWorldClock(t, "2024-03-31"), + query: join([]string{ + `Mtime:"last month"`, + }), + ast: &ast.Ast{ + Nodes: []ast.Node{ + &ast.DateTimeNode{ + Key: "Mtime", + Operator: &ast.OperatorNode{Value: ">="}, + Value: mustParseTime(t, "2024-02-01"), + }, + &ast.OperatorNode{Value: kql.BoolAND}, + &ast.DateTimeNode{ + Key: "Mtime", + Operator: &ast.OperatorNode{Value: "<="}, + Value: mustParseTime(t, "2024-02-29 23:59:59.999999999"), + }, + }, + }, + }, { name: "NaturalLanguage DateTimeNode - last month - the beginning of the year", patch: setWorldClock(t, "2023-01-01"),