Skip to content

Commit 7102ab6

Browse files
author
ssinno28
committed
Adding DateOnly, TimeOnly and Point Field Mappers
1 parent 9c48f45 commit 7102ab6

File tree

9 files changed

+166
-5
lines changed

9 files changed

+166
-5
lines changed

Lucene.Net.DocumentMapper.Tests/DocumentMapperTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,56 @@ public void Test_Not_Indexed_If_Too_Large_Text()
351351
Assert.False(isIndexed);
352352
}
353353

354+
[Fact]
355+
public void Test_Is_TimeOnly()
356+
{
357+
var documentMapper = _serviceProvider.GetRequiredService<IDocumentMapper>();
358+
// public PropertyInfo? GetProperty(string name);
359+
var propertyMapper = documentMapper.GetFieldMapper(typeof(BlogPost).GetProperty(nameof(BlogPost.PublishedTimeOnly)));
360+
361+
Assert.Equal(typeof(TimeOnlyFieldMapper), propertyMapper?.GetType());
362+
363+
var timeOnly = new TimeOnly(14, 30, 0);
364+
var blogPost = new BlogPost { PublishedTimeOnly = timeOnly };
365+
var document = documentMapper.Map(blogPost);
366+
var mappedBlogPost = documentMapper.Map<BlogPost>(document);
367+
Assert.Equal(timeOnly, mappedBlogPost.PublishedTimeOnly);
368+
}
369+
370+
[Fact]
371+
public void Test_Is_DateOnly()
372+
{
373+
var documentMapper = _serviceProvider.GetRequiredService<IDocumentMapper>();
374+
// public PropertyInfo? GetProperty(string name);
375+
var propertyMapper = documentMapper.GetFieldMapper(typeof(BlogPost).GetProperty(nameof(BlogPost.PublishedDateOnly)));
376+
377+
Assert.Equal(typeof(DateOnlyFieldMapper), propertyMapper?.GetType());
378+
379+
var dateOnly = new DateOnly(2023, 12, 25);
380+
var blogPost = new BlogPost { PublishedDateOnly = dateOnly };
381+
var document = documentMapper.Map(blogPost);
382+
var mappedBlogPost = documentMapper.Map<BlogPost>(document);
383+
Assert.Equal(dateOnly, mappedBlogPost.PublishedDateOnly);
384+
}
385+
386+
[Fact]
387+
public void Test_Is_Point()
388+
{
389+
var documentMapper = _serviceProvider.GetRequiredService<IDocumentMapper>();
390+
391+
var blogPost = new BlogPost
392+
{
393+
Latitude = 40.7128,
394+
Longitude = -74.0060
395+
};
396+
397+
var document = documentMapper.Map(blogPost);
398+
var mappedBlogPost = documentMapper.Map<BlogPost>(document);
399+
400+
Assert.Equal(blogPost.Latitude, mappedBlogPost.Latitude);
401+
Assert.Equal(blogPost.Longitude, mappedBlogPost.Longitude);
402+
}
403+
354404
private byte[] GetByteArray(int sizeInKb)
355405
{
356406
Random rnd = new Random();

Lucene.Net.DocumentMapper.Tests/Lucene.Net.DocumentMapper.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<LangVersion>preview</LangVersion>
77
<IsPackable>false</IsPackable>

Lucene.Net.DocumentMapper.Tests/Models/Blog/BlogPost.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using System;
22
using System.Collections.Generic;
33
using Lucene.Net.DocumentMapper.Attributes;
4+
using Lucene.Net.DocumentMapper.Interfaces;
45

56
namespace Lucene.Net.DocumentMapper.Tests.Models
67
{
7-
public class BlogPost
8+
public class BlogPost : ILocationIndex
89
{
910
public DateTime PublishedDate { get; set; }
1011
public DateTimeOffset PublishedDateOffset { get; set; }
12+
public DateOnly PublishedDateOnly { get; set; }
13+
public TimeOnly PublishedTimeOnly { get; set; }
1114
public bool IsPublished { get; set; }
1215
public string Name { get; set; } = string.Empty;
1316
[Search(Tokenized = true)]
@@ -25,6 +28,8 @@ public class BlogPost
2528
public byte[] Thumbnail { get; set; }
2629

2730
public EnumCategory Category3 { get; set; }
31+
public double Latitude { get; set; }
32+
public double Longitude { get; set; }
2833
}
2934

3035
public enum EnumCategory
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Reflection;
3+
using Lucene.Net.DocumentMapper.Interfaces;
4+
using Lucene.Net.Documents;
5+
using Lucene.Net.Documents.Extensions;
6+
7+
namespace Lucene.Net.DocumentMapper.FieldMappers
8+
{
9+
public class DateOnlyFieldMapper : AFieldMapper, IFieldMapper
10+
{
11+
public int Priority => 0;
12+
13+
public bool IsMatch(PropertyInfo propertyInfo)
14+
{
15+
var type = GetPropertyType(propertyInfo);
16+
return type == typeof(DateOnly);
17+
}
18+
19+
public Field MapToField(PropertyInfo propertyInfo, object value, string name)
20+
{
21+
var date = value is DateOnly only ? only : default;
22+
return new Int64Field(name, date.ToDateTime(TimeOnly.MinValue).Ticks, GetStore(propertyInfo));
23+
}
24+
25+
public object MapFromField(Field field)
26+
{
27+
long ticks = field.GetInt64ValueOrDefault();
28+
return DateOnly.FromDateTime(new DateTime(ticks));
29+
}
30+
}
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Lucene.Net.DocumentMapper.Interfaces;
5+
using Lucene.Net.Documents;
6+
using Lucene.Net.Spatial;
7+
using Lucene.Net.Spatial.Vector;
8+
using Spatial4n.Context;
9+
using Spatial4n.Shapes;
10+
11+
namespace Lucene.Net.DocumentMapper.FieldMappers
12+
{
13+
public class PointFieldMapper : IFieldsMapper
14+
{
15+
public bool IsMatch(Type type)
16+
{
17+
return type.GetInterfaces().ToList().Contains(typeof(ILocationIndex));
18+
}
19+
20+
public IList<Field> MapToFields(object @object)
21+
{
22+
ILocationIndex locationIndex = (ILocationIndex) @object;
23+
24+
SpatialContext ctx = SpatialContext.GEO;
25+
SpatialStrategy strategy = new PointVectorStrategy(ctx, FieldPrefixConstants.LocationPointPrefix);
26+
IShape shape = new Point(locationIndex.Longitude, locationIndex.Latitude, ctx);
27+
28+
return strategy.CreateIndexableFields(shape);
29+
}
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Reflection;
3+
using Lucene.Net.DocumentMapper.Interfaces;
4+
using Lucene.Net.Documents;
5+
6+
namespace Lucene.Net.DocumentMapper.FieldMappers
7+
{
8+
public class TimeOnlyFieldMapper : AFieldMapper, IFieldMapper
9+
{
10+
public int Priority => 0;
11+
12+
public bool IsMatch(PropertyInfo propertyInfo)
13+
{
14+
var type = GetPropertyType(propertyInfo);
15+
return type == typeof(TimeOnly);
16+
}
17+
18+
public Field MapToField(PropertyInfo propertyInfo, object value, string name)
19+
{
20+
return new StringField(name,
21+
value.ToString(),
22+
GetStore(propertyInfo));
23+
}
24+
25+
public object MapFromField(Field field)
26+
{
27+
return TimeOnly.Parse(field.GetStringValue());
28+
}
29+
}
30+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Lucene.Net.DocumentMapper;
2+
3+
public class FieldPrefixConstants
4+
{
5+
public static string LocationPointPrefix = "LatLong";
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Lucene.Net.DocumentMapper.Interfaces;
2+
3+
public interface ILocationIndex
4+
{
5+
double Latitude { get; set; }
6+
double Longitude { get; set; }
7+
}

Lucene.Net.DocumentMapper/Lucene.Net.DocumentMapper.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net7.0</TargetFramework>
4+
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<LangVersion>preview</LangVersion>
77
<AssemblyName>Lucene.Net.DocumentMapper</AssemblyName>
88
<RootNamespace>Lucene.Net.DocumentMapper</RootNamespace>
99
</PropertyGroup>
1010

1111
<PropertyGroup>
12-
<Version>1.0.15</Version>
12+
<Version>1.0.16</Version>
1313
<RepositoryUrl>https://github.com/ssinno28/Lucene.Net.DocumentMapper</RepositoryUrl>
1414
<RepositoryType>git</RepositoryType>
1515
<Description>A service that provides functionality for mapping C# types to Lucene Documents and back.</Description>
@@ -26,7 +26,8 @@
2626
</PropertyGroup>
2727

2828
<ItemGroup>
29-
<PackageReference Include="Lucene.Net" Version="4.8.0-beta00016" />
29+
<PackageReference Include="Lucene.Net" Version="4.8.0-beta00017" />
30+
<PackageReference Include="Lucene.Net.Spatial" Version="4.8.0-beta00017" />
3031
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
3132
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3233
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />

0 commit comments

Comments
 (0)