Skip to content

Commit

Permalink
feat: Add DateOnly support to RangeQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
nikcio committed Nov 21, 2022
1 parent dcdf778 commit d33c0c1
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Examine.Lucene/Search/LuceneSearchQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,23 @@ internal LuceneBooleanOperationBase RangeQueryInternal<T>(string[] fields, T? mi
inner.Add(q, Occur.SHOULD);
}
}
#if !NETSTANDARD2_0 && !NETSTANDARD2_1
else if(typeof(T) == typeof(DateOnly) && valueType is IIndexRangeValueType<DateTime> dateOnlyType)
{
TimeOnly minValueTime = minInclusive ? TimeOnly.MinValue : TimeOnly.MaxValue;
var minValue = min.HasValue ? (min.Value as DateOnly?)?.ToDateTime(minValueTime) : null;

TimeOnly maxValueTime = maxInclusive ? TimeOnly.MaxValue : TimeOnly.MinValue;
var maxValue = max.HasValue ? (max.Value as DateOnly?)?.ToDateTime(maxValueTime) : null;

var q = dateOnlyType.GetQuery(minValue, maxValue, minInclusive, maxInclusive);

if (q != null)
{
inner.Add(q, Occur.SHOULD);
}
}
#endif
else
{
throw new InvalidOperationException($"Could not perform a range query on the field {f}, it's value type is {valueType?.GetType()}");
Expand Down
149 changes: 149 additions & 0 deletions src/Examine.Test/Examine.Lucene/Search/FluentApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2555,5 +2555,154 @@ public void Given_SkipTake_Returns_ExpectedTotals(int skip, int take, int expect
Assert.AreEqual(expectedResults, results.Count());
}
}

#if NET6_0_OR_GREATER
[Test]
public void Range_DateOnly()
{
var analyzer = new StandardAnalyzer(LuceneInfo.CurrentVersion);
using (var luceneDir = new RandomIdRAMDirectory())
using (var indexer = GetTestIndex(
luceneDir,
analyzer,
new FieldDefinitionCollection(new FieldDefinition("created", "datetime"))))
{


indexer.IndexItems(new[]
{
ValueSet.FromObject(123.ToString(), "content",
new
{
created = new DateTime(2000, 01, 02),
bodyText = "lorem ipsum",
nodeTypeAlias = "CWS_Home"
}),
ValueSet.FromObject(2123.ToString(), "content",
new
{
created = new DateTime(2000, 01, 04),
bodyText = "lorem ipsum",
nodeTypeAlias = "CWS_Test"
}),
ValueSet.FromObject(3123.ToString(), "content",
new
{
created = new DateTime(2000, 01, 05),
bodyText = "lorem ipsum",
nodeTypeAlias = "CWS_Page"
})
});


var searcher = indexer.Searcher;

var numberSortedCriteria = searcher.CreateQuery()
.RangeQuery<DateOnly>(new[] { "created" }, new DateOnly(2000, 01, 02), new DateOnly(2000, 01, 05), maxInclusive: false);

var numberSortedResult = numberSortedCriteria.Execute();

Assert.AreEqual(2, numberSortedResult.TotalItemCount);
}
}

[Test]
public void Range_DateOnly_Min_And_Max_Inclusive()
{
var analyzer = new StandardAnalyzer(LuceneInfo.CurrentVersion);
using (var luceneDir = new RandomIdRAMDirectory())
using (var indexer = GetTestIndex(
luceneDir,
analyzer,
new FieldDefinitionCollection(new FieldDefinition("created", "datetime"))))
{


indexer.IndexItems(new[]
{
ValueSet.FromObject(123.ToString(), "content",
new
{
created = new DateTime(2000, 01, 02),
bodyText = "lorem ipsum",
nodeTypeAlias = "CWS_Home"
}),
ValueSet.FromObject(2123.ToString(), "content",
new
{
created = new DateTime(2000, 01, 04),
bodyText = "lorem ipsum",
nodeTypeAlias = "CWS_Test"
}),
ValueSet.FromObject(3123.ToString(), "content",
new
{
created = new DateTime(2000, 01, 05),
bodyText = "lorem ipsum",
nodeTypeAlias = "CWS_Page"
})
});


var searcher = indexer.Searcher;

var numberSortedCriteria = searcher.CreateQuery()
.RangeQuery<DateOnly>(new[] { "created" }, new DateOnly(2000, 01, 02), new DateOnly(2000, 01, 05));

var numberSortedResult = numberSortedCriteria.Execute();

Assert.AreEqual(3, numberSortedResult.TotalItemCount);
}
}

[Test]
public void Range_DateOnly_No_Inclusive()
{
var analyzer = new StandardAnalyzer(LuceneInfo.CurrentVersion);
using (var luceneDir = new RandomIdRAMDirectory())
using (var indexer = GetTestIndex(
luceneDir,
analyzer,
new FieldDefinitionCollection(new FieldDefinition("created", "datetime"))))
{


indexer.IndexItems(new[]
{
ValueSet.FromObject(123.ToString(), "content",
new
{
created = new DateTime(2000, 01, 02),
bodyText = "lorem ipsum",
nodeTypeAlias = "CWS_Home"
}),
ValueSet.FromObject(2123.ToString(), "content",
new
{
created = new DateTime(2000, 01, 04),
bodyText = "lorem ipsum",
nodeTypeAlias = "CWS_Test"
}),
ValueSet.FromObject(3123.ToString(), "content",
new
{
created = new DateTime(2000, 01, 05),
bodyText = "lorem ipsum",
nodeTypeAlias = "CWS_Page"
})
});


var searcher = indexer.Searcher;

var numberSortedCriteria = searcher.CreateQuery()
.RangeQuery<DateOnly>(new[] { "created" }, new DateOnly(2000, 01, 02), new DateOnly(2000, 01, 05), minInclusive: false, maxInclusive: false);

var numberSortedResult = numberSortedCriteria.Execute();

Assert.AreEqual(1, numberSortedResult.TotalItemCount);
}
}
#endif
}
}

0 comments on commit d33c0c1

Please sign in to comment.