Skip to content

Commit abbee5b

Browse files
committed
fix #3505 in the best way we can for 6.x
(cherry picked from commit b414ad9)
1 parent cea4645 commit abbee5b

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

src/Nest/CommonOptions/DateMath/DateMath.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ public static DateMath FromString(string dateMath)
7979
return math;
8080
}
8181

82+
internal bool IsValid => Self.Anchor.Match(d => d != default, s => !s.IsNullOrEmpty());
83+
8284
public override string ToString()
8385
{
84-
var isValid = Self.Anchor.Match(d => d != default(DateTime), s => !s.IsNullOrEmpty());
85-
if (!isValid) return string.Empty;
86+
if (!IsValid) return string.Empty;
8687

8788
var separator = Self.Round.HasValue || Self.Ranges.HasAny() ? "||" : string.Empty;
8889

src/Nest/QueryDsl/TermLevel/Range/DateRangeQuery .cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public class DateRangeQuery : FieldNameQueryBase, IDateRangeQuery
4343
internal override void InternalWrapInContainer(IQueryContainer c) => c.Range = this;
4444

4545
internal static bool IsConditionless(IDateRangeQuery q) => q.Field.IsConditionless()
46-
|| q.GreaterThanOrEqualTo == null
47-
&& q.LessThanOrEqualTo == null
48-
&& q.GreaterThan == null
49-
&& q.LessThan == null;
46+
&& (q.GreaterThanOrEqualTo == null || !q.GreaterThanOrEqualTo.IsValid)
47+
&& (q.LessThanOrEqualTo == null || !q.LessThanOrEqualTo.IsValid)
48+
&& (q.GreaterThan == null || !q.GreaterThan.IsValid)
49+
&& (q.LessThan == null || !q.LessThan.IsValid);
5050
}
5151

5252
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]

src/Tests/Tests/QueryDsl/TermLevel/Range/DateRangeQueryUsageTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,48 @@ protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project>
6666
.TimeZone("+01:00")
6767
);
6868
}
69+
70+
public class DateRangeDefaultDateTimeQueryUsageTests : QueryDslUsageTestsBase
71+
{
72+
public DateRangeDefaultDateTimeQueryUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
73+
74+
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<IDateRangeQuery>(q => q.Range as IDateRangeQuery)
75+
{ };
76+
77+
protected override QueryContainer QueryInitializer => new DateRangeQuery
78+
{
79+
Field = "description",
80+
GreaterThan = default,
81+
GreaterThanOrEqualTo = default,
82+
LessThan = default,
83+
LessThanOrEqualTo = default,
84+
Format = "dd/MM/yyyy||yyyy",
85+
TimeZone = "+01:00"
86+
};
87+
88+
//TODO in 7.0 DateMath should make its DateTime nullable so that DateTime.MinValue is sent out as well.
89+
// this is only marginally better then sending an empty string.
90+
protected override object QueryJson => new
91+
{
92+
range = new
93+
{
94+
description = new
95+
{
96+
format = "dd/MM/yyyy||yyyy",
97+
time_zone = "+01:00"
98+
}
99+
}
100+
};
101+
102+
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
103+
.DateRange(c => c
104+
.Field(p => p.Description)
105+
.GreaterThan(default)
106+
.GreaterThanOrEquals(default)
107+
.LessThan(default)
108+
.LessThanOrEquals(default)
109+
.Format("dd/MM/yyyy||yyyy")
110+
.TimeZone("+01:00")
111+
);
112+
}
69113
}

0 commit comments

Comments
 (0)