public virtual void TestDateToolsUTC_TicksAsMilliseconds() { // Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London //long time = 1130630400; DateTime time1 = new DateTime(2005, 10, 30, 0, 0, 0, DateTimeKind.Utc); DateTime time2 = time1.AddHours(1); { TimeZoneInfo timeZone = TZConvert.GetTimeZoneInfo("Europe/London"); string d1 = DateTools.DateToString(time1, timeZone, DateResolution.MINUTE); string d2 = DateTools.DateToString(time2, timeZone, DateResolution.MINUTE); Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times"); Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS_AS_MILLISECONDS), time1.Ticks / TimeSpan.TicksPerMillisecond, "midnight"); Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS_AS_MILLISECONDS), time2.Ticks / TimeSpan.TicksPerMillisecond, "later"); } { string d1 = DateTools.DateToString(time1, DateResolution.MINUTE); string d2 = DateTools.DateToString(time2, DateResolution.MINUTE); Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times"); Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS_AS_MILLISECONDS), time1.Ticks / TimeSpan.TicksPerMillisecond, "midnight"); Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS_AS_MILLISECONDS), time2.Ticks / TimeSpan.TicksPerMillisecond, "later"); } time1 = new DateTime(2005, 10, 30, 0, 0, 0, DateTimeKind.Utc).ToLocalTime(); time2 = time1.AddHours(1); { string d1 = DateTools.DateToString(time1, DateResolution.MINUTE); string d2 = DateTools.DateToString(time2, DateResolution.MINUTE); Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times"); Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS_AS_MILLISECONDS), time1.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond, "midnight"); Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS_AS_MILLISECONDS), time2.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond, "later"); } time1 = new DateTime(2005, 10, 30, 0, 0, 0, DateTimeKind.Unspecified); time2 = time1.AddHours(1); { string d1 = DateTools.DateToString(time1, DateResolution.MINUTE); string d2 = DateTools.DateToString(time2, DateResolution.MINUTE); Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times"); Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS_AS_MILLISECONDS), time1.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond, "midnight"); Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS_AS_MILLISECONDS), time2.ToUniversalTime().Ticks / TimeSpan.TicksPerMillisecond, "later"); } }
public virtual void TestDateToolsUTC() { /*// Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London * long time = 1130630400; * try * { * TimeZone.Default = TimeZone.getTimeZone("Europe/London"); // "GMT" * string d1 = DateTools.DateToString(new DateTime(time * 1000), DateTools.Resolution.MINUTE); * string d2 = DateTools.DateToString(new DateTime((time+3600) * 1000), DateTools.Resolution.MINUTE); * Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times"); * Assert.AreEqual(DateTools.StringToTime(d1), time * 1000, "midnight"); * Assert.AreEqual(DateTools.StringToTime(d2), (time+3600) * 1000, "later"); * } * finally * { * TimeZone.Default = null; * }*/ // Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London //long time = 1130630400; DateTime time1 = new DateTime(2005, 10, 30); DateTime time2 = time1.AddHours(1); try { //TimeZone.setDefault(TimeZone.getTimeZone("Europe/London")); // {{Aroush-2.0}} need porting 'java.util.TimeZone.getTimeZone' System.DateTime tempAux = time1; System.String d1 = DateTools.DateToString(tempAux, DateTools.Resolution.MINUTE); System.DateTime tempAux2 = time2; System.String d2 = DateTools.DateToString(tempAux2, DateTools.Resolution.MINUTE); Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times"); Assert.AreEqual(DateTools.StringToTime(d1), time1.Ticks, "midnight"); Assert.AreEqual(DateTools.StringToTime(d2), time2.Ticks, "later"); } finally { //TimeZone.SetDefault(null); // {{Aroush-2.0}} need porting 'java.util.TimeZone.setDefault' } }
public virtual void TestDateToolsUTC_DateTimeOffset_Ticks() { // Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London //long time = 1130630400; DateTimeOffset time1 = new DateTimeOffset(2005, 10, 30, 0, 0, 0, TimeSpan.Zero); DateTimeOffset time2 = time1.AddHours(1); { TimeZoneInfo timeZone = TZConvert.GetTimeZoneInfo("Europe/London"); DateTimeOffset tempAux = TimeZoneInfo.ConvertTime(time1, timeZone); string d1 = DateTools.DateToString(tempAux, DateResolution.MINUTE); DateTimeOffset tempAux2 = TimeZoneInfo.ConvertTime(time2, timeZone); string d2 = DateTools.DateToString(tempAux2, DateResolution.MINUTE); Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times"); Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS), time1.Ticks, "midnight"); Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS), time2.Ticks, "later"); } { string d1 = DateTools.DateToString(time1, DateResolution.MINUTE); string d2 = DateTools.DateToString(time2, DateResolution.MINUTE); Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times"); Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS), time1.Ticks, "midnight"); Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS), time2.Ticks, "later"); } time1 = new DateTimeOffset(2005, 10, 30, 0, 0, 0, TimeSpan.Zero).ToLocalTime(); time2 = time1.AddHours(1); { string d1 = DateTools.DateToString(time1, DateResolution.MINUTE); string d2 = DateTools.DateToString(time2, DateResolution.MINUTE); Assert.IsFalse(d1.Equals(d2, StringComparison.Ordinal), "different times"); Assert.AreEqual(DateTools.StringToTime(d1, NumericRepresentation.TICKS), time1.ToUniversalTime().Ticks, "midnight"); Assert.AreEqual(DateTools.StringToTime(d2, NumericRepresentation.TICKS), time2.ToUniversalTime().Ticks, "later"); } }
public virtual void TestDateAndTimetoString() { System.DateTime cal = new System.DateTime(2004, 2, 3, 22, 8, 56, 333, new System.Globalization.GregorianCalendar()); System.String dateString; System.DateTime tempAux = new System.DateTime(cal.Ticks); dateString = DateTools.DateToString(tempAux, DateTools.Resolution.YEAR); Assert.AreEqual("2004", dateString); System.DateTime tempAux2 = DateTools.StringToDate(dateString); Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(tempAux2)); System.DateTime tempAux3 = new System.DateTime(cal.Ticks); dateString = DateTools.DateToString(tempAux3, DateTools.Resolution.MONTH); Assert.AreEqual("200402", dateString); System.DateTime tempAux4 = DateTools.StringToDate(dateString); Assert.AreEqual("2004-02-01 00:00:00:000", IsoFormat(tempAux4)); System.DateTime tempAux5 = new System.DateTime(cal.Ticks); dateString = DateTools.DateToString(tempAux5, DateTools.Resolution.DAY); Assert.AreEqual("20040203", dateString); System.DateTime tempAux6 = DateTools.StringToDate(dateString); Assert.AreEqual("2004-02-03 00:00:00:000", IsoFormat(tempAux6)); System.DateTime tempAux7 = new System.DateTime(cal.Ticks); dateString = DateTools.DateToString(tempAux7, DateTools.Resolution.HOUR); Assert.AreEqual("2004020322", dateString); System.DateTime tempAux8 = DateTools.StringToDate(dateString); Assert.AreEqual("2004-02-03 22:00:00:000", IsoFormat(tempAux8)); System.DateTime tempAux9 = new System.DateTime(cal.Ticks); dateString = DateTools.DateToString(tempAux9, DateTools.Resolution.MINUTE); Assert.AreEqual("200402032208", dateString); System.DateTime tempAux10 = DateTools.StringToDate(dateString); Assert.AreEqual("2004-02-03 22:08:00:000", IsoFormat(tempAux10)); System.DateTime tempAux11 = new System.DateTime(cal.Ticks); dateString = DateTools.DateToString(tempAux11, DateTools.Resolution.SECOND); Assert.AreEqual("20040203220856", dateString); System.DateTime tempAux12 = DateTools.StringToDate(dateString); Assert.AreEqual("2004-02-03 22:08:56:000", IsoFormat(tempAux12)); System.DateTime tempAux13 = new System.DateTime(cal.Ticks); dateString = DateTools.DateToString(tempAux13, DateTools.Resolution.MILLISECOND); Assert.AreEqual("20040203220856333", dateString); System.DateTime tempAux14 = DateTools.StringToDate(dateString); Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(tempAux14)); // date before 1970: cal = new System.DateTime(1961, 3, 5, 23, 9, 51, 444, new System.Globalization.GregorianCalendar()); System.DateTime tempAux15 = new System.DateTime(cal.Ticks); dateString = DateTools.DateToString(tempAux15, DateTools.Resolution.MILLISECOND); Assert.AreEqual("19610305230951444", dateString); System.DateTime tempAux16 = DateTools.StringToDate(dateString); Assert.AreEqual("1961-03-05 23:09:51:444", IsoFormat(tempAux16)); System.DateTime tempAux17 = new System.DateTime(cal.Ticks); dateString = DateTools.DateToString(tempAux17, DateTools.Resolution.HOUR); Assert.AreEqual("1961030523", dateString); System.DateTime tempAux18 = DateTools.StringToDate(dateString); Assert.AreEqual("1961-03-05 23:00:00:000", IsoFormat(tempAux18)); // timeToString: cal = new System.DateTime(1970, 1, 1, 0, 0, 0, 0, new System.Globalization.GregorianCalendar()); dateString = DateTools.TimeToString(cal.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND); Assert.AreEqual("19700101000000000", dateString); cal = new System.DateTime(1970, 1, 1, 1, 2, 3, 0, new System.Globalization.GregorianCalendar()); dateString = DateTools.TimeToString(cal.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND); Assert.AreEqual("19700101010203000", dateString); }
public virtual void TestDateAndTimetoString() { // we use default locale since LuceneTestCase randomizes it //Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.Default); DateTime cal = new GregorianCalendar().ToDateTime(2004, 2, 3, 22, 8, 56, 333); /*cal.clear(); * cal = new DateTime(2004, 1, 3, 22, 8, 56); // hour, minute, second - year=2004, month=february(!), day=3 * cal.set(DateTime.MILLISECOND, 333);*/ string dateString = DateTools.DateToString(cal, DateTools.Resolution.YEAR); Assert.AreEqual("2004", dateString); Assert.AreEqual("2004-01-01 00:00:00:000", IsoFormat(DateTools.StringToDate(dateString))); dateString = DateTools.DateToString(cal, DateTools.Resolution.MONTH); Assert.AreEqual("200402", dateString); Assert.AreEqual("2004-02-01 00:00:00:000", IsoFormat(DateTools.StringToDate(dateString))); dateString = DateTools.DateToString(cal, DateTools.Resolution.DAY); Assert.AreEqual("20040203", dateString); Assert.AreEqual("2004-02-03 00:00:00:000", IsoFormat(DateTools.StringToDate(dateString))); dateString = DateTools.DateToString(cal, DateTools.Resolution.HOUR); Assert.AreEqual("2004020322", dateString); Assert.AreEqual("2004-02-03 22:00:00:000", IsoFormat(DateTools.StringToDate(dateString))); dateString = DateTools.DateToString(cal, DateTools.Resolution.MINUTE); Assert.AreEqual("200402032208", dateString); Assert.AreEqual("2004-02-03 22:08:00:000", IsoFormat(DateTools.StringToDate(dateString))); dateString = DateTools.DateToString(cal, DateTools.Resolution.SECOND); Assert.AreEqual("20040203220856", dateString); Assert.AreEqual("2004-02-03 22:08:56:000", IsoFormat(DateTools.StringToDate(dateString))); dateString = DateTools.DateToString(cal, DateTools.Resolution.MILLISECOND); Assert.AreEqual("20040203220856333", dateString); Assert.AreEqual("2004-02-03 22:08:56:333", IsoFormat(DateTools.StringToDate(dateString))); // date before 1970: cal = new GregorianCalendar().ToDateTime(1961, 3, 5, 23, 9, 51, 444); // hour, minute, second - year=1961, month=march(!), day=5 //cal.set(DateTime.MILLISECOND, 444); dateString = DateTools.DateToString(cal, DateTools.Resolution.MILLISECOND); Assert.AreEqual("19610305230951444", dateString); Assert.AreEqual("1961-03-05 23:09:51:444", IsoFormat(DateTools.StringToDate(dateString))); dateString = DateTools.DateToString(cal, DateTools.Resolution.HOUR); Assert.AreEqual("1961030523", dateString); Assert.AreEqual("1961-03-05 23:00:00:000", IsoFormat(DateTools.StringToDate(dateString))); // timeToString: cal = new GregorianCalendar().ToDateTime(1970, 1, 1, 0, 0, 0, 0); // hour, minute, second - year=1970, month=january, day=1 //cal.set(DateTime.MILLISECOND, 0); dateString = DateTools.TimeToString(cal.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND); Assert.AreEqual("19700101000000000", dateString); cal = new GregorianCalendar().ToDateTime(1970, 1, 1, 1, 2, 3, 0); // hour, minute, second - year=1970, month=january, day=1 //cal.set(DateTime.MILLISECOND, 0); dateString = DateTools.TimeToString(cal.Ticks / TimeSpan.TicksPerMillisecond, DateTools.Resolution.MILLISECOND); Assert.AreEqual("19700101010203000", dateString); }
/// <summary> /// This method generate the fields for indexing documents in lucene from the values. /// Given a name and a value, it has the following behavior: /// * If the value is enumerable, index all the items in the enumerable under the same field name /// * If the value is null, create a single field with the supplied name with the unanalyzed value 'NULL_VALUE' /// * If the value is string or was set to not analyzed, create a single field with the supplied name /// * If the value is date, create a single field with millisecond precision with the supplied name /// * If the value is numeric (int, long, double, decimal, or float) will create two fields: /// 1. with the supplied name, containing the numeric value as an unanalyzed string - useful for direct queries /// 2. with the name: name +'_Range', containing the numeric value in a form that allows range queries /// </summary> private IEnumerable <AbstractField> CreateFields(string name, object value, IndexDefinition indexDefinition, Field.Store defaultStorage) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Field must be not null, not empty and cannot contain whitespace", "name"); } if (char.IsLetter(name[0]) == false && name[0] != '_') { name = "_" + name; } if (value == null) { yield return(CreateFieldWithCaching(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage), Field.Index.NOT_ANALYZED_NO_NORMS)); yield break; } if (Equals(value, string.Empty)) { yield return(CreateFieldWithCaching(name, Constants.EmptyString, indexDefinition.GetStorage(name, defaultStorage), Field.Index.NOT_ANALYZED_NO_NORMS)); yield break; } if (value is DynamicNullObject) { if (((DynamicNullObject)value).IsExplicitNull) { yield return(CreateFieldWithCaching(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage), Field.Index.NOT_ANALYZED_NO_NORMS)); } yield break; } if (value is AbstractField) { yield return((AbstractField)value); yield break; } var itemsToIndex = value as IEnumerable; if (itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex)) { yield return(new Field(name + "_IsArray", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); int count = 1; foreach (var itemToIndex in itemsToIndex) { multipleItemsSameFieldCount.Add(count++); foreach (var field in CreateFields(name, itemToIndex, indexDefinition, defaultStorage)) { yield return(field); } multipleItemsSameFieldCount.RemoveAt(multipleItemsSameFieldCount.Count - 1); } yield break; } var fieldIndexingOptions = indexDefinition.GetIndex(name, null); if (fieldIndexingOptions == Field.Index.NOT_ANALYZED || fieldIndexingOptions == Field.Index.NOT_ANALYZED_NO_NORMS) // explicitly not analyzed { if (value is DateTime) { var val = (DateTime)value; yield return(CreateFieldWithCaching(name, val.ToString(Default.DateTimeFormatsToWrite), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is DateTimeOffset) { var val = (DateTimeOffset)value; yield return(CreateFieldWithCaching(name, val.ToString(Default.DateTimeFormatsToWrite), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else { yield return(CreateFieldWithCaching(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } yield break; } if (value is string) { var index = indexDefinition.GetIndex(name, Field.Index.ANALYZED); yield return(CreateFieldWithCaching(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage), index)); yield break; } if (value is DateTime) { yield return(CreateFieldWithCaching(name, DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is DateTimeOffset) { yield return(CreateFieldWithCaching(name, DateTools.DateToString(((DateTimeOffset)value).DateTime, DateTools.Resolution.MILLISECOND), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is bool) { yield return(new Field(name, ((bool)value) ? "true" : "false", indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is IConvertible) // we need this to store numbers in invariant format, so JSON could read them { var convert = ((IConvertible)value); yield return(CreateFieldWithCaching(name, convert.ToString(CultureInfo.InvariantCulture), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is IDynamicJsonObject) { var inner = ((IDynamicJsonObject)value).Inner; yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); yield return(CreateFieldWithCaching(name, inner.ToString(), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else { yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); yield return(CreateFieldWithCaching(name, RavenJToken.FromObject(value).ToString(), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } foreach (var numericField in CreateNumericFieldWithCaching(name, value, defaultStorage)) { yield return(numericField); } }
/// <summary> /// This method generate the fields for indexing documents in lucene from the values. /// Given a name and a value, it has the following behavior: /// * If the value is enumerable, index all the items in the enumerable under the same field name /// * If the value is null, create a single field with the supplied name with the unanalyzed value 'NULL_VALUE' /// * If the value is string or was set to not analyzed, create a single field with the supplied name /// * If the value is date, create a single field with millisecond precision with the supplied name /// * If the value is numeric (int, long, double, decimal, or float) will create two fields: /// 1. with the supplied name, containing the numeric value as an unanalyzed string - useful for direct queries /// 2. with the name: name +'_Range', containing the numeric value in a form that allows range queries /// </summary> public IEnumerable <AbstractField> CreateFields(string name, object value, Field.Store defaultStorage, bool nestedArray = false) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Field must be not null, not empty and cannot contain whitespace", "name"); } if (char.IsLetter(name[0]) == false && name[0] != '_') { name = "_" + name; } var storage = indexDefinition.GetStorage(name, defaultStorage); if (value == null) { yield return(CreateFieldWithCaching(name, Constants.NullValue, storage, Field.Index.NOT_ANALYZED_NO_NORMS)); yield break; } if (Equals(value, string.Empty)) { yield return(CreateFieldWithCaching(name, Constants.EmptyString, storage, Field.Index.NOT_ANALYZED_NO_NORMS)); yield break; } var dynamicNullObject = value as DynamicNullObject; if (ReferenceEquals(dynamicNullObject, null) == false) { if ((dynamicNullObject).IsExplicitNull) { yield return(CreateFieldWithCaching(name, Constants.NullValue, storage, Field.Index.NOT_ANALYZED_NO_NORMS)); } yield break; } var boostedValue = value as BoostedValue; if (boostedValue != null) { foreach (var field in CreateFields(name, boostedValue.Value, storage)) { field.SetBoost(boostedValue.Boost); field.SetOmitNorms(false); yield return(field); } yield break; } var abstractField = value as AbstractField; if (abstractField != null) { yield return(abstractField); yield break; } var bytes = value as byte[]; if (bytes != null) { yield return(CreateBinaryFieldWithCaching(name, bytes, storage)); yield break; } var itemsToIndex = value as IEnumerable; if (itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex)) { if (nestedArray == false && !Equals(storage, Field.Store.NO)) { yield return(new Field(name + "_IsArray", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); } int count = 1; foreach (var itemToIndex in itemsToIndex) { multipleItemsSameFieldCount.Add(count++); foreach (var field in CreateFields(name, itemToIndex, storage, nestedArray: true)) { yield return(field); } multipleItemsSameFieldCount.RemoveAt(multipleItemsSameFieldCount.Count - 1); } yield break; } var fieldIndexingOptions = indexDefinition.GetIndex(name, null); if (Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED) || Equals(fieldIndexingOptions, Field.Index.NOT_ANALYZED_NO_NORMS)) // explicitly not analyzed { if (value is DateTime) { var val = (DateTime)value; var postFix = val.Kind == DateTimeKind.Utc ? "Z" : ""; yield return(CreateFieldWithCaching(name, val.ToString(Default.DateTimeFormatsToWrite) + postFix, storage, indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is DateTimeOffset) { var val = (DateTimeOffset)value; yield return(CreateFieldWithCaching(name, val.ToString(Default.DateTimeOffsetFormatsToWrite), storage, indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else { yield return(CreateFieldWithCaching(name, value.ToString(), storage, indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } yield break; } if (value is string) { var index = indexDefinition.GetIndex(name, Field.Index.ANALYZED); yield return(CreateFieldWithCaching(name, value.ToString(), storage, index)); yield break; } if (value is DateTime) { yield return(CreateFieldWithCaching(name, DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND), storage, indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is DateTimeOffset) { yield return(CreateFieldWithCaching(name, DateTools.DateToString(((DateTimeOffset)value).UtcDateTime, DateTools.Resolution.MILLISECOND), storage, indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is bool) { yield return(new Field(name, ((bool)value) ? "true" : "false", storage, indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is IConvertible) // we need this to store numbers in invariant format, so JSON could read them { var convert = ((IConvertible)value); yield return(CreateFieldWithCaching(name, convert.ToString(CultureInfo.InvariantCulture), storage, indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else if (value is IDynamicJsonObject) { var inner = ((IDynamicJsonObject)value).Inner; yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); yield return(CreateFieldWithCaching(name, inner.ToString(), storage, indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } else { yield return(CreateFieldWithCaching(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); yield return(CreateFieldWithCaching(name, RavenJToken.FromObject(value).ToString(), storage, indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED_NO_NORMS))); } foreach (var numericField in CreateNumericFieldWithCaching(name, value, storage)) { yield return(numericField); } }
/// <summary> /// This method generate the fields for indexing documents in lucene from the values. /// Given a name and a value, it has the following behavior: /// * If the value is enumerable, index all the items in the enumerable under the same field name /// * If the value is null, create a single field with the supplied name with the unanalyzed value 'NULL_VALUE' /// * If the value is string or was set to not analyzed, create a single field with the supplied name /// * If the value is date, create a single field with millisecond precision with the supplied name /// * If the value is numeric (int, long, double, decimal, or float) will create two fields: /// 1. with the supplied name, containing the numeric value as an unanalyzed string - useful for direct queries /// 2. with the name: name +'_Range', containing the numeric value in a form that allows range queries /// </summary> private static IEnumerable <AbstractField> CreateFields(string name, object value, IndexDefinition indexDefinition, Field.Store defaultStorage) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Field must be not null, not empty and cannot contain whitespace", "name"); } if (char.IsLetter(name[0]) == false && name[0] != '_') { name = "_" + name; } if (value == null) { yield return(new Field(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage), Field.Index.NOT_ANALYZED)); yield break; } if (value is DynamicNullObject) { if (((DynamicNullObject)value).IsExplicitNull) { yield return(new Field(name, Constants.NullValue, indexDefinition.GetStorage(name, defaultStorage), Field.Index.NOT_ANALYZED)); } yield break; } if (value is AbstractField) { yield return((AbstractField)value); yield break; } var itemsToIndex = value as IEnumerable; if (itemsToIndex != null && ShouldTreatAsEnumerable(itemsToIndex)) { yield return(new Field(name + "_IsArray", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); foreach (var itemToIndex in itemsToIndex) { foreach (var field in CreateFields(name, itemToIndex, indexDefinition, defaultStorage)) { yield return(field); } } yield break; } if (indexDefinition.GetIndex(name, null) == Field.Index.NOT_ANALYZED)// explicitly not analyzed { if (value is DateTime) { var val = (DateTime)value; yield return(new Field(name, val.ToString(Default.DateTimeFormatsToWrite), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else if (value is DateTimeOffset) { var val = (DateTimeOffset)value; yield return(new Field(name, val.ToString(Default.DateTimeFormatsToWrite), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else { yield return(new Field(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } yield break; } if (value is string) { var index = indexDefinition.GetIndex(name, Field.Index.ANALYZED); yield return(new Field(name, value.ToString(), indexDefinition.GetStorage(name, defaultStorage), index)); yield break; } if (value is DateTime) { yield return(new Field(name, DateTools.DateToString((DateTime)value, DateTools.Resolution.MILLISECOND), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else if (value is DateTimeOffset) { yield return(new Field(name, DateTools.DateToString(((DateTimeOffset)value).DateTime, DateTools.Resolution.MILLISECOND), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else if (value is bool) { yield return(new Field(name, ((bool)value) ? "true" : "false", indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else if (value is IConvertible) // we need this to store numbers in invariant format, so JSON could read them { var convert = ((IConvertible)value); yield return(new Field(name, convert.ToString(CultureInfo.InvariantCulture), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else if (value is DynamicJsonObject) { var inner = ((DynamicJsonObject)value).Inner; yield return(new Field(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); yield return(new Field(name, inner.ToString(), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } else { yield return(new Field(name + "_ConvertToJson", "true", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); yield return(new Field(name, RavenJToken.FromObject(value).ToString(), indexDefinition.GetStorage(name, defaultStorage), indexDefinition.GetIndex(name, Field.Index.NOT_ANALYZED))); } var numericField = new NumericField(name + "_Range", indexDefinition.GetStorage(name, defaultStorage), true); if (value is int) { if (indexDefinition.GetSortOption(name) == SortOptions.Long) { yield return(numericField.SetLongValue((int)value)); } else { yield return(numericField.SetIntValue((int)value)); } } if (value is long) { yield return(numericField .SetLongValue((long)value)); } if (value is decimal) { yield return(numericField .SetDoubleValue((double)(decimal)value)); } if (value is float) { if (indexDefinition.GetSortOption(name) == SortOptions.Double) { yield return(numericField.SetDoubleValue((float)value)); } else { yield return(numericField.SetFloatValue((float)value)); } } if (value is double) { yield return(numericField .SetDoubleValue((double)value)); } }