Beispiel #1
0
 /// <summary>
 /// Expert: allows you to customize the <see cref="FieldType"/>.
 /// </summary>
 /// <param name="name"> field name </param>
 /// <param name="value"> 64-bit double value </param>
 /// <param name="type"> customized field type: must have <see cref="FieldType.NumericType"/>
 ///         of <see cref="NumericType.DOUBLE"/>. </param>
 /// <exception cref="ArgumentNullException"> if the field name or type is <c>null</c>, or
 ///          if the field type does not have a <see cref="NumericType.DOUBLE"/> <see cref="FieldType.NumericType"/> </exception>
 public DoubleField(string name, double value, FieldType type)
     : base(name, type)
 {
     if (type.NumericType != Documents.NumericType.DOUBLE)
     {
         throw new ArgumentException("type.NumericType must be NumericType.DOUBLE but got " + type.NumericType);
     }
     FieldsData = Double.GetInstance(value);
 }
Beispiel #2
0
 /// <summary>
 /// Create a stored-only field with the given <see cref="double"/> value. </summary>
 /// <param name="name"> field name </param>
 /// <param name="value"> <see cref="double"/> value </param>
 /// <exception cref="ArgumentNullException"> if the field <paramref name="name"/> is <c>null</c>. </exception>
 public StoredField(string name, double value)
     : base(name, TYPE)
 {
     FieldsData = Double.GetInstance(value);
 }
Beispiel #3
0
 /// <summary>
 /// Creates a stored or un-stored <see cref="DoubleField"/> 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"> 64-bit <see cref="double"/> value </param>
 /// <param name="stored"> <see cref="Field.Store.YES"/> if the content should also be stored </param>
 /// <exception cref="ArgumentNullException"> if the field name is <c>null</c>.  </exception>
 public DoubleField(string name, double value, Store stored)
     : base(name, stored == Store.YES ? TYPE_STORED : TYPE_NOT_STORED)
 {
     FieldsData = Double.GetInstance(value);
 }
Beispiel #4
0
        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();
            }
        }