Lucene.Net.Linq icon indicating copy to clipboard operation
Lucene.Net.Linq copied to clipboard

Errors when searching DateTime fields

Open xumix opened this issue 10 years ago • 6 comments

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: 

xumix avatar Jun 30 '15 16:06 xumix

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

xumix avatar Jun 30 '15 17:06 xumix

This is actually a blocker, since datetime fielda are not usable at all

xumix avatar Jul 01 '15 08:07 xumix

Resolved with .NotAnalyzed() but this looks not intuitive

xumix avatar Jul 01 '15 10:07 xumix

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.

xumix avatar Jul 01 '15 14:07 xumix

Finally got it working with .AsNumeric()

xumix avatar Jul 01 '15 15:07 xumix

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.

chriseldredge avatar Jul 01 '15 15:07 chriseldredge