-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add LongRangeQuery to handle range queries on long numeric field types.
- Loading branch information
1 parent
1230f1c
commit 69733ea
Showing
9 changed files
with
173 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Nest | ||
{ | ||
public interface ILongRangeQuery : IRangeQuery | ||
{ | ||
[JsonProperty("gte")] | ||
long? GreaterThanOrEqualTo { get; set; } | ||
|
||
[JsonProperty("lte")] | ||
long? LessThanOrEqualTo { get; set; } | ||
|
||
[JsonProperty("gt")] | ||
long? GreaterThan { get; set; } | ||
|
||
[JsonProperty("lt")] | ||
long? LessThan { get; set; } | ||
|
||
[JsonProperty("relation")] | ||
RangeRelation? Relation { get; set; } | ||
} | ||
|
||
public class LongRangeQuery : FieldNameQueryBase, ILongRangeQuery | ||
{ | ||
protected override bool Conditionless => IsConditionless(this); | ||
public long? GreaterThanOrEqualTo { get; set; } | ||
public long? LessThanOrEqualTo { get; set; } | ||
public long? GreaterThan { get; set; } | ||
public long? LessThan { get; set; } | ||
|
||
public RangeRelation? Relation { get; set; } | ||
|
||
internal override void InternalWrapInContainer(IQueryContainer c) => c.Range = this; | ||
|
||
internal static bool IsConditionless(ILongRangeQuery q) | ||
{ | ||
return q.Field.IsConditionless() | ||
|| (q.GreaterThanOrEqualTo == null | ||
&& q.LessThanOrEqualTo == null | ||
&& q.GreaterThan == null | ||
&& q.LessThan == null); | ||
} | ||
} | ||
|
||
[JsonObject(MemberSerialization = MemberSerialization.OptIn)] | ||
public class LongRangeQueryDescriptor<T> | ||
: FieldNameQueryDescriptorBase<LongRangeQueryDescriptor<T>, ILongRangeQuery, T> | ||
, ILongRangeQuery where T : class | ||
{ | ||
protected override bool Conditionless => LongRangeQuery.IsConditionless(this); | ||
long? ILongRangeQuery.GreaterThanOrEqualTo { get; set; } | ||
long? ILongRangeQuery.LessThanOrEqualTo { get; set; } | ||
long? ILongRangeQuery.GreaterThan { get; set; } | ||
long? ILongRangeQuery.LessThan { get; set; } | ||
RangeRelation? ILongRangeQuery.Relation { get; set; } | ||
|
||
public LongRangeQueryDescriptor<T> Relation(RangeRelation? relation) => Assign(a => a.Relation = relation); | ||
|
||
public LongRangeQueryDescriptor<T> GreaterThan(long? from) => Assign(a => a.GreaterThan = from); | ||
|
||
public LongRangeQueryDescriptor<T> GreaterThanOrEquals(long? from) => Assign(a => a.GreaterThanOrEqualTo = from); | ||
|
||
public LongRangeQueryDescriptor<T> LessThan(long? to) => Assign(a => a.LessThan = to); | ||
|
||
public LongRangeQueryDescriptor<T> LessThanOrEquals(long? to) => Assign(a => a.LessThanOrEqualTo = to); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Tests/QueryDsl/TermLevel/Range/LongRangeQueryUsageTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using Nest; | ||
using Tests.Framework.Integration; | ||
using Tests.Framework.ManagedElasticsearch.Clusters; | ||
using Tests.Framework.MockData; | ||
|
||
namespace Tests.QueryDsl.TermLevel.Range | ||
{ | ||
public class LongRangeQueryUsageTests : QueryDslUsageTestsBase | ||
{ | ||
public LongRangeQueryUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) {} | ||
|
||
protected override object QueryJson => new | ||
{ | ||
range = new | ||
{ | ||
description = new | ||
{ | ||
_name = "named_query", | ||
boost = 1.1, | ||
gt = 636634079999999999, | ||
gte = 636634080000000000, | ||
lt = 636634080000000000, | ||
lte = 636634079999999999, | ||
relation = "within" | ||
} | ||
} | ||
}; | ||
|
||
protected override QueryContainer QueryInitializer => new LongRangeQuery | ||
{ | ||
Name = "named_query", | ||
Boost = 1.1, | ||
Field = "description", | ||
GreaterThan = 636634079999999999, | ||
GreaterThanOrEqualTo = 636634080000000000, | ||
LessThan = 636634080000000000, | ||
LessThanOrEqualTo = 636634079999999999, | ||
Relation = RangeRelation.Within | ||
}; | ||
|
||
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q | ||
.LongRange(c => c | ||
.Name("named_query") | ||
.Boost(1.1) | ||
.Field(p => p.Description) | ||
.GreaterThan(636634079999999999) | ||
.GreaterThanOrEquals(636634080000000000) | ||
.LessThan(636634080000000000) | ||
.LessThanOrEquals(636634079999999999) | ||
.Relation(RangeRelation.Within) | ||
); | ||
|
||
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<ILongRangeQuery>(q => q.Range as ILongRangeQuery) | ||
{ | ||
q=> q.Field = null, | ||
q=> | ||
{ | ||
q.GreaterThan = null; | ||
q.GreaterThanOrEqualTo = null; | ||
q.LessThan = null; | ||
q.LessThanOrEqualTo = null; | ||
} | ||
}; | ||
} | ||
} |