/// <summary> /// Expert: allows you to customize the /// <see cref="FieldType"/>. /// </summary> /// <param name="name"> field name </param> /// <param name="value"> 32-bit <see cref="int"/> value </param> /// <param name="type"> customized field type: must have <see cref="FieldType.NumericType"/> /// of <see cref="NumericType.INT32"/>. </param> /// <exception cref="ArgumentNullException"> if the field <paramref name="name"/> or <paramref name="type"/> is <c>null</c>. </exception> /// <exception cref="ArgumentException">if the field type does not have a /// <see cref="FieldType.NumericType"/> of <see cref="NumericType.INT32"/> </exception> public Int32Field(string name, int value, FieldType type) : base(name, type) { if (type.NumericType != Documents.NumericType.INT32) { throw new ArgumentException("type.NumericType must be NumericType.INT32 but got " + type.NumericType); } FieldsData = Int32.GetInstance(value); }
// TODO: not great but maybe not a big problem? /// <summary> /// Create a stored-only field with the given <see cref="int"/> value. </summary> /// <param name="name"> field name </param> /// <param name="value"> <see cref="int"/> value </param> /// <exception cref="ArgumentNullException"> if the field <paramref name="name"/> is <c>null</c>. </exception> public StoredField(string name, int value) : base(name, TYPE) { FieldsData = Int32.GetInstance(value); }
/// <summary> /// Creates a stored or un-stored <see cref="Int32Field"/> with the provided value /// and default <c>precisionStep</c> /// <see cref="Util.NumericUtils.PRECISION_STEP_DEFAULT"/> (4). /// </summary> /// <param name="name"> field name </param> /// <param name="value"> 32-bit <see cref="int"/> value </param> /// <param name="stored"> <see cref="Field.Store.YES"/> if the content should also be stored </param> /// <exception cref="ArgumentNullException"> if the field <paramref name="name"/> is <c>null</c>. </exception> public Int32Field(string name, int value, Store stored) : base(name, stored == Store.YES ? TYPE_STORED : TYPE_NOT_STORED) { FieldsData = Int32.GetInstance(value); }
public virtual void TestNumericField() { using Directory dir = NewDirectory(); DirectoryReader r = null; try { var numDocs = AtLeast(500); var answers = new Number[numDocs]; using (var w = new RandomIndexWriter(Random, dir)) { NumericType[] typeAnswers = new NumericType[numDocs]; for (int id = 0; id < numDocs; id++) { Document doc = new Document(); Field nf; Field sf; Number answer; NumericType typeAnswer; if (Random.NextBoolean()) { // float/double if (Random.NextBoolean()) { float f = Random.NextSingle(); answer = Single.GetInstance(f); nf = new SingleField("nf", f, Field.Store.NO); sf = new StoredField("nf", f); typeAnswer = NumericType.SINGLE; } else { double d = Random.NextDouble(); answer = Double.GetInstance(d); nf = new DoubleField("nf", d, Field.Store.NO); sf = new StoredField("nf", d); typeAnswer = NumericType.DOUBLE; } } else { // int/long if (Random.NextBoolean()) { int i = Random.Next(); answer = Int32.GetInstance(i); nf = new Int32Field("nf", i, Field.Store.NO); sf = new StoredField("nf", i); typeAnswer = NumericType.INT32; } else { long l = Random.NextInt64(); answer = Int64.GetInstance(l); nf = new Int64Field("nf", l, Field.Store.NO); sf = new StoredField("nf", l); typeAnswer = NumericType.INT64; } } doc.Add(nf); doc.Add(sf); answers[id] = answer; typeAnswers[id] = typeAnswer; FieldType ft = new FieldType(Int32Field.TYPE_STORED); ft.NumericPrecisionStep = int.MaxValue; doc.Add(new Int32Field("id", id, ft)); w.AddDocument(doc); } r = w.GetReader(); } // w.Dispose(); Assert.AreEqual(numDocs, r.NumDocs); foreach (AtomicReaderContext ctx in r.Leaves) { AtomicReader sub = ctx.AtomicReader; FieldCache.Int32s ids = FieldCache.DEFAULT.GetInt32s(sub, "id", false); for (int docID = 0; docID < sub.NumDocs; docID++) { Document doc = sub.Document(docID); Field f = doc.GetField <Field>("nf"); Assert.IsTrue(f is StoredField, "got f=" + f); #pragma warning disable 612, 618 Assert.AreEqual(answers[ids.Get(docID)], f.GetNumericValue()); #pragma warning restore 612, 618 } } } finally { r?.Dispose(); } }