Lucene.Net.Linq
Lucene.Net.Linq copied to clipboard
Errors when searching DateTime fields
I have my system configured with ru-RU locale, this is what I get if I try to search within DateTime fields
tourMap.Property(p => p.StartDate).Stored();
query = query.Where(q => q.EndDate <= date1);
System.FormatException occurred
HResult=-2146233033
Message=String was not recognized as a valid DateTime.
Source=mscorlib
StackTrace:
at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at Lucene.Net.Linq.Converters.DateTimeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at Lucene.Net.Linq.Search.NonGenericConvertableFieldComparatorSource.NonGenericConvertableFieldComparator`1.<GetCurrentReaderValues>b__0(String s)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Lucene.Net.Linq.Search.NonGenericConvertableFieldComparatorSource.NonGenericConvertableFieldComparator`1.GetCurrentReaderValues(IndexReader reader, Int32 docBase)
at Lucene.Net.Linq.Search.FieldComparator`1.SetNextReader(IndexReader reader, Int32 docBase)
at Lucene.Net.Search.TopFieldCollector.OneComparatorNonScoringCollector.SetNextReader(IndexReader reader, Int32 docBase) in d:\Lucene.Net\FullRepo\trunk\src\core\Search\TopFieldCollector.cs:line 99
at Lucene.Net.Search.IndexSearcher.Search(Weight weight, Filter filter, Collector collector) in d:\Lucene.Net\FullRepo\trunk\src\core\Search\IndexSearcher.cs:line 237
at Lucene.Net.Search.IndexSearcher.Search(Weight weight, Filter filter, Int32 nDocs, Sort sort, Boolean fillFields) in d:\Lucene.Net\FullRepo\trunk\src\core\Search\IndexSearcher.cs:line 212
at Lucene.Net.Search.IndexSearcher.Search(Weight weight, Filter filter, Int32 nDocs, Sort sort) in d:\Lucene.Net\FullRepo\trunk\src\core\Search\IndexSearcher.cs:line 194
at Lucene.Net.Search.Searcher.Search(Query query, Filter filter, Int32 n, Sort sort) in d:\Lucene.Net\FullRepo\trunk\src\core\Search\Searcher.cs:line 57
at Lucene.Net.Linq.LuceneQueryExecutorBase`1.<ExecuteCollection>d__3`1.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at RestChild.Booking.Logic.TourSearchService.SearchTours(SearchToursParams parameter) in c:\Projects\RestChild\RestChild.Booking.Logic\TourSearchService.cs:line 90
InnerException:
The error is here:
return DateTime.SpecifyKind(DateTime.ParseExact((string) value, format, null), DateTimeKind.Utc);
The value is: 2015-08-15t21:00:00 but the format is yyyy-MM-ddTHH:mm:ss
This is actually a blocker, since datetime fielda are not usable at all
Resolved with .NotAnalyzed() but this looks not intuitive
Now, more problems, the same query finds no documents for the specified dates there are many post about this on SO http://stackoverflow.com/questions/5495645/indexing-and-searching-date-in-lucene my parsed query looks like this:
+(+(+StartDate:[2015-01-01T00:00:00 TO *] +EndDate:[* TO 2016-01-01T00:00:00]) +(HotelName:*чайка* HotelRegionName:*чайка*)) +TypeOfRest:2
If I remove date constraints then there are many items with dates like 2015-09-15T00:00:00 etc.
Finally got it working with .AsNumeric()
Treating dates and times as numeric fields will generally result in faster range queries and is the recommended approach. It sounds like there may be bugs with string based indexing so I'll keep this open until I can look into it.