Exemplo n.º 1
0
        public void TestStaticMethod2Old()
        {
            String[]
            fields = { "b", "t" };
            Occur[]
            flags =
            {
                Occur.MUST,
                Occur.MUST_NOT
            };
            StandardQueryParser parser = new StandardQueryParser();

            parser.SetMultiFields(fields);
            parser.Analyzer = (new MockAnalyzer(Random));

            Query q = QueryParserUtil.Parse("one", fields, flags,
                                            new MockAnalyzer(Random));// , fields, flags, new

            // MockAnalyzer());
            assertEquals("+b:one -t:one", q.toString());

            q = QueryParserUtil.Parse("one two", fields, flags, new MockAnalyzer(Random));
            assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());

            try
            {
                Occur[] flags2 = { Occur.MUST };
                q = QueryParserUtil.Parse("blah", fields, flags2, new MockAnalyzer(Random));
                fail();
            }
            catch (Exception e) when(e.IsIllegalArgumentException())
            {
                // expected exception, array length differs
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Parses a query, searching on the fields specified. Use this if you need to
        /// specify certain fields as required, and others as prohibited.
        /// <para/>
        /// Usage:
        /// <code>
        /// string[] fields = {&quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;};
        /// Occur[] flags = {Occur.SHOULD,
        ///     Occur.MUST,
        ///     Occur.MUST_NOT};
        /// MultiFieldQueryParser.Parse(&quot;query&quot;, fields, flags, analyzer);
        /// </code>
        /// <para/>
        /// The code above would construct a query:
        /// <code>
        /// (filename:query) +(contents:query) -(description:query)
        /// </code>
        /// </summary>
        /// <param name="query">Query string to parse</param>
        /// <param name="fields">Fields to search on</param>
        /// <param name="flags">Flags describing the fields</param>
        /// <param name="analyzer">Analyzer to use</param>
        /// <exception cref="ArgumentException">
        /// if the length of the fields array differs from the length of the
        /// flags array
        /// </exception>
        public static Query Parse(string query, string[] fields,
                                  Occur[] flags, Analyzer analyzer)
        {
            if (fields.Length != flags.Length)
            {
                throw new ArgumentException("fields.length != flags.length");
            }
            BooleanQuery bQuery = new BooleanQuery();

            StandardQueryParser qp = new StandardQueryParser();

            qp.Analyzer = analyzer;

            for (int i = 0; i < fields.Length; i++)
            {
                Query q = qp.Parse(query, fields[i]);

                if (q != null && // q never null, just being defensive
                    (!(q is BooleanQuery) || ((BooleanQuery)q).Clauses.Count > 0))
                {
                    bQuery.Add(q, flags[i]);
                }
            }
            return(bQuery);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Parses a query, searching on the fields specified. Use this if you need to
        /// specify certain fields as required, and others as prohibited.
        /// <para/>
        /// Usage:
        /// <code>
        /// string[] query = {&quot;query1&quot;, &quot;query2&quot;, &quot;query3&quot;};
        /// string[] fields = {&quot;filename&quot;, &quot;contents&quot;, &quot;description&quot;};
        /// Occur[] flags = {Occur.SHOULD,
        ///     Occur.MUST,
        ///     Occur.MUST_NOT};
        /// MultiFieldQueryParser.Parse(query, fields, flags, analyzer);
        /// </code>
        /// <para/>
        /// The code above would construct a query:
        /// <code>
        /// (filename:query1) +(contents:query2) -(description:query3)
        /// </code>
        /// </summary>
        /// <param name="queries">Queries string to parse</param>
        /// <param name="fields">Fields to search on</param>
        /// <param name="flags">Flags describing the fields</param>
        /// <param name="analyzer">Analyzer to use</param>
        /// <exception cref="ArgumentException">
        /// if the length of the queries, fields, and flags array differ
        /// </exception>
        public static Query Parse(string[] queries, string[] fields,
                                  Occur[] flags, Analyzer analyzer)
        {
            if (!(queries.Length == fields.Length && queries.Length == flags.Length))
            {
                throw new ArgumentException(
                          "queries, fields, and flags array have have different length");
            }
            BooleanQuery bQuery = new BooleanQuery();

            StandardQueryParser qp = new StandardQueryParser();

            qp.Analyzer = analyzer;

            for (int i = 0; i < fields.Length; i++)
            {
                Query q = qp.Parse(queries[i], fields[i]);

                if (q != null && // q never null, just being defensive
                    (!(q is BooleanQuery booleanQuery) || booleanQuery.Clauses.Count > 0))
                {
                    bQuery.Add(q, flags[i]);
                }
            }
            return(bQuery);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Parses a query which searches on the fields specified.
        /// <para/>
        /// If x fields are specified, this effectively constructs:
        /// <code>
        /// (field1:query1) (field2:query2) (field3:query3)...(fieldx:queryx)
        /// </code>
        /// </summary>
        /// <param name="queries">Queries strings to parse</param>
        /// <param name="fields">Fields to search on</param>
        /// <param name="analyzer">Analyzer to use</param>
        /// <exception cref="ArgumentException">
        /// if the length of the queries array differs from the length of the
        /// fields array
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="queries"/> or <paramref name="fields"/> is <c>null</c>
        /// </exception>
        public static Query Parse(string[] queries, string[] fields, Analyzer analyzer)
        {
            // LUCENENET: Added null guard clauses
            if (queries is null)
            {
                throw new ArgumentNullException(nameof(queries));
            }
            if (fields is null)
            {
                throw new ArgumentNullException(nameof(fields));
            }

            if (queries.Length != fields.Length)
            {
                throw new ArgumentException("queries.Length != fields.Length");
            }
            BooleanQuery bQuery = new BooleanQuery();

            StandardQueryParser qp = new StandardQueryParser();

            qp.Analyzer = analyzer;

            for (int i = 0; i < fields.Length; i++)
            {
                Query q = qp.Parse(queries[i], fields[i]);

                if (q != null && // q never null, just being defensive
                    (!(q is BooleanQuery booleanQuery) || booleanQuery.Clauses.Count > 0))
                {
                    bQuery.Add(q, Occur.SHOULD);
                }
            }
            return(bQuery);
        }
Exemplo n.º 5
0
        public void TestStopWordSearching()
        {
            Analyzer analyzer = new MockAnalyzer(Random);

            Store.Directory ramDir = NewDirectory();
            IndexWriter     iw     = new IndexWriter(ramDir, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
            Document        doc    = new Document();

            doc.Add(NewTextField("body", "blah the footest blah", Field.Store.NO));
            iw.AddDocument(doc);
            iw.Dispose();

            StandardQueryParser mfqp = new StandardQueryParser();

            mfqp.SetMultiFields(new String[] { "body" });
            mfqp.Analyzer        = (analyzer);
            mfqp.DefaultOperator = (StandardQueryConfigHandler.Operator.AND);
            Query         q   = mfqp.Parse("the footest", null);
            IndexReader   ir  = DirectoryReader.Open(ramDir);
            IndexSearcher @is = NewSearcher(ir);

            ScoreDoc[] hits = @is.Search(q, null, 1000).ScoreDocs;
            assertEquals(1, hits.Length);
            ir.Dispose();
            ramDir.Dispose();
        }
Exemplo n.º 6
0
        public override void SetDefaultOperatorAND(ICommonQueryParserConfiguration cqpC)
        {
            Debug.Assert(cqpC is StandardQueryParser);
            StandardQueryParser qp = (StandardQueryParser)cqpC;

            qp.DefaultOperator = (Operator.AND);
        }
Exemplo n.º 7
0
        public override Query GetQuery(String query, ICommonQueryParserConfiguration cqpC)
        {
            Debug.Assert(cqpC != null, "Parameter must not be null");
            Debug.Assert((cqpC is StandardQueryParser), "Parameter must be instance of StandardQueryParser");
            StandardQueryParser qp = (StandardQueryParser)cqpC;

            return(Parse(query, qp));
        }
Exemplo n.º 8
0
        public override void SetDateResolution(ICommonQueryParserConfiguration cqpC,
                                               string field, DateTools.Resolution value)
        {
            Debug.Assert(cqpC is StandardQueryParser);
            StandardQueryParser qp = (StandardQueryParser)cqpC;

            qp.DateResolutionMap.Put(field, value);
        }
Exemplo n.º 9
0
 public static void AfterClass()
 {
     searcher = null;
     reader.Dispose();
     reader = null;
     directory.Dispose();
     directory = null;
     qp        = null;
 }
Exemplo n.º 10
0
        public override void SetDefaultOperatorOR(ICommonQueryParserConfiguration cqpC)
        {
            if (Debugging.AssertsEnabled)
            {
                Debugging.Assert(cqpC is StandardQueryParser);
            }
            StandardQueryParser qp = (StandardQueryParser)cqpC;

            qp.DefaultOperator = (Operator.OR);
        }
Exemplo n.º 11
0
        public override void AfterClass()
        {
            searcher = null;
            reader.Dispose();
            reader = null;
            directory.Dispose();
            directory = null;
            qp        = null;

            base.AfterClass();
        }
Exemplo n.º 12
0
        public override void TestDefaultOperator()
        {
            StandardQueryParser qp = GetParser(new MockAnalyzer(Random()));

            // make sure OR is the default:
            assertEquals(StandardQueryConfigHandler.Operator.OR, qp.DefaultOperator);
            SetDefaultOperatorAND(qp);
            assertEquals(StandardQueryConfigHandler.Operator.AND, qp.DefaultOperator);
            SetDefaultOperatorOR(qp);
            assertEquals(StandardQueryConfigHandler.Operator.OR, qp.DefaultOperator);
        }
Exemplo n.º 13
0
        public StandardQueryParser GetParser(Analyzer a)
        {
            if (a == null)
            {
                a = new MockAnalyzer(Random(), MockTokenizer.SIMPLE, true);
            }
            StandardQueryParser qp = new StandardQueryParser(a);

            qp.DefaultOperator = (Operator.OR);

            return(qp);
        }
Exemplo n.º 14
0
        public void TestPosIncrementAnalyzer()
        {
            StandardQueryParser qp = new StandardQueryParser();

            qp.Analyzer = (new PosIncrementAnalyzer());

            assertEquals("quick brown", qp.Parse("the quick brown", "").toString());
            assertEquals("\"? quick brown\"", qp.Parse("\"the quick brown\"", "")
                         .toString());
            assertEquals("quick brown fox", qp.Parse("the quick brown fox", "")
                         .toString());
            assertEquals("\"? quick brown fox\"", qp.Parse("\"the quick brown fox\"", "")
                         .toString());
        }
Exemplo n.º 15
0
        // verify parsing of query using a stopping analyzer
        private void assertStopQueryEquals(String qtxt, String expectedRes)
        {
            String[]
                    fields = { "b", "t" };
            Occur[] occur  = { Occur.SHOULD, Occur.SHOULD };
            TestQPHelper.QPTestAnalyzer a    = new TestQPHelper.QPTestAnalyzer();
            StandardQueryParser         mfqp = new StandardQueryParser();

            mfqp.SetMultiFields(fields);
            mfqp.Analyzer = (a);

            Query q = mfqp.Parse(qtxt, null);

            assertEquals(expectedRes, q.toString());

            q = QueryParserUtil.Parse(qtxt, fields, occur, a);
            assertEquals(expectedRes, q.toString());
        }
Exemplo n.º 16
0
        // verify parsing of query using a stopping analyzer
        private void assertStopQueryEquals(String qtxt, String expectedRes)
        {
            String[]
            fields = { "b", "t" };
            BooleanClause.Occur[]       occur = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD }; // LUCENENET TODO: Make this Occur.Should instead of BooleanClause.Occur.Should
            TestQPHelper.QPTestAnalyzer a     = new TestQPHelper.QPTestAnalyzer();
            StandardQueryParser         mfqp  = new StandardQueryParser();

            mfqp.SetMultiFields(fields);
            mfqp.Analyzer = (a);

            Query q = mfqp.Parse(qtxt, null);

            assertEquals(expectedRes, q.toString());

            q = QueryParserUtil.Parse(qtxt, fields, occur, a);
            assertEquals(expectedRes, q.toString());
        }
Exemplo n.º 17
0
        public override void TestNewFieldQuery()
        {
            /** ordinary behavior, synonyms form uncoordinated boolean query */
            StandardQueryParser dumb     = GetParser(new Analyzer1());
            BooleanQuery        expanded = new BooleanQuery(true);

            expanded.Add(new TermQuery(new Term("field", "dogs")),
                         BooleanClause.Occur.SHOULD);
            expanded.Add(new TermQuery(new Term("field", "dog")),
                         BooleanClause.Occur.SHOULD);
            assertEquals(expanded, dumb.Parse("\"dogs\"", "field"));
            /** even with the phrase operator the behavior is the same */
            assertEquals(expanded, dumb.Parse("dogs", "field"));

            /**
             * custom behavior, the synonyms are expanded, unless you use quote operator
             */
            //TODO test something like "SmartQueryParser()"
        }
Exemplo n.º 18
0
        public void TestAnalyzerReturningNull()
        {
            String[]
            fields = new String[] { "f1", "f2", "f3" };
            StandardQueryParser parser = new StandardQueryParser();

            parser.SetMultiFields(fields);
            parser.Analyzer = (new AnalyzerReturningNull());

            Query q = parser.Parse("bla AND blo", null);

            assertEquals("+(f2:bla f3:bla) +(f2:blo f3:blo)", q.toString());
            // the following queries are not affected as their terms are not
            // analyzed anyway:
            q = parser.Parse("bla*", null);
            assertEquals("f1:bla* f2:bla* f3:bla*", q.toString());
            q = parser.Parse("bla~", null);
            assertEquals("f1:bla~2 f2:bla~2 f3:bla~2", q.toString());
            q = parser.Parse("[a TO c]", null);
            assertEquals("f1:[a TO c] f2:[a TO c] f3:[a TO c]", q.toString());
        }
Exemplo n.º 19
0
        public void TestStaticMethod2Old()
        {
            String[]
            fields = { "b", "t" };
            BooleanClause.Occur[]
            flags =
            {
                BooleanClause.Occur.MUST,
                BooleanClause.Occur.MUST_NOT
            };
            StandardQueryParser parser = new StandardQueryParser();

            parser.SetMultiFields(fields);
            parser.Analyzer = (new MockAnalyzer(Random()));

            Query q = QueryParserUtil.Parse("one", fields, flags,
                                            new MockAnalyzer(Random()));// , fields, flags, new

            // MockAnalyzer());
            assertEquals("+b:one -t:one", q.toString());

            q = QueryParserUtil.Parse("one two", fields, flags, new MockAnalyzer(Random()));
            assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());

            try
            {
                BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST };
                q = QueryParserUtil.Parse("blah", fields, flags2, new MockAnalyzer(Random()));
                fail();
            }
#pragma warning disable 168
            catch (ArgumentException e)
#pragma warning restore 168
            {
                // expected exception, array length differs
            }
        }
Exemplo n.º 20
0
        public void TestBoostsSimple()
        {
            IDictionary <String, float?> boosts = new Dictionary <String, float?>();

            boosts.Put("b", 5);
            boosts.Put("t", 10);
            String[]            fields = { "b", "t" };
            StandardQueryParser mfqp   = new StandardQueryParser();

            mfqp.SetMultiFields(fields);
            mfqp.FieldsBoost = (boosts);
            mfqp.Analyzer    = (new MockAnalyzer(Random));

            // Check for simple
            Query q = mfqp.Parse("one", null);

            assertEquals("b:one^5.0 t:one^10.0", q.toString());

            // Check for AND
            q = mfqp.Parse("one AND two", null);
            assertEquals("+(b:one^5.0 t:one^10.0) +(b:two^5.0 t:two^10.0)", q
                         .toString());

            // Check for OR
            q = mfqp.Parse("one OR two", null);
            assertEquals("(b:one^5.0 t:one^10.0) (b:two^5.0 t:two^10.0)", q.toString());

            // Check for AND and a field
            q = mfqp.Parse("one AND two AND foo:test", null);
            assertEquals("+(b:one^5.0 t:one^10.0) +(b:two^5.0 t:two^10.0) +foo:test", q
                         .toString());

            q = mfqp.Parse("one^3 AND two^4", null);
            assertEquals("+((b:one^5.0 t:one^10.0)^3.0) +((b:two^5.0 t:two^10.0)^4.0)",
                         q.toString());
        }
Exemplo n.º 21
0
        public void TestSimple()
        {
            String[]
            fields = { "b", "t" };
            StandardQueryParser mfqp = new StandardQueryParser();

            mfqp.SetMultiFields(fields);
            mfqp.Analyzer = (new MockAnalyzer(Random));

            Query q = mfqp.Parse("one", null);

            assertEquals("b:one t:one", q.toString());

            q = mfqp.Parse("one two", null);
            assertEquals("(b:one t:one) (b:two t:two)", q.toString());

            q = mfqp.Parse("+one +two", null);
            assertEquals("+(b:one t:one) +(b:two t:two)", q.toString());

            q = mfqp.Parse("+one -two -three", null);
            assertEquals("+(b:one t:one) -(b:two t:two) -(b:three t:three)", q
                         .toString());

            q = mfqp.Parse("one^2 two", null);
            assertEquals("((b:one t:one)^2.0) (b:two t:two)", q.toString());

            q = mfqp.Parse("one~ two", null);
            assertEquals("(b:one~2 t:one~2) (b:two t:two)", q.toString());

            q = mfqp.Parse("one~0.8 two^2", null);
            assertEquals("(b:one~0 t:one~0) ((b:two t:two)^2.0)", q.toString());

            q = mfqp.Parse("one* two*", null);
            assertEquals("(b:one* t:one*) (b:two* t:two*)", q.toString());

            q = mfqp.Parse("[a TO c] two", null);
            assertEquals("(b:[a TO c] t:[a TO c]) (b:two t:two)", q.toString());

            q = mfqp.Parse("w?ldcard", null);
            assertEquals("b:w?ldcard t:w?ldcard", q.toString());

            q = mfqp.Parse("\"foo bar\"", null);
            assertEquals("b:\"foo bar\" t:\"foo bar\"", q.toString());

            q = mfqp.Parse("\"aa bb cc\" \"dd ee\"", null);
            assertEquals("(b:\"aa bb cc\" t:\"aa bb cc\") (b:\"dd ee\" t:\"dd ee\")", q
                         .toString());

            q = mfqp.Parse("\"foo bar\"~4", null);
            assertEquals("b:\"foo bar\"~4 t:\"foo bar\"~4", q.toString());

            // LUCENE-1213: QueryParser was ignoring slop when phrase
            // had a field.
            q = mfqp.Parse("b:\"foo bar\"~4", null);
            assertEquals("b:\"foo bar\"~4", q.toString());

            // make sure that terms which have a field are not touched:
            q = mfqp.Parse("one f:two", null);
            assertEquals("(b:one t:one) f:two", q.toString());

            // AND mode:
            mfqp.DefaultOperator = (StandardQueryConfigHandler.Operator.AND);
            q = mfqp.Parse("one two", null);
            assertEquals("+(b:one t:one) +(b:two t:two)", q.toString());
            q = mfqp.Parse("\"aa bb cc\" \"dd ee\"", null);
            assertEquals("+(b:\"aa bb cc\" t:\"aa bb cc\") +(b:\"dd ee\" t:\"dd ee\")",
                         q.toString());
        }
Exemplo n.º 22
0
 public Query Parse(String query, StandardQueryParser qp)
 {
     return(qp.Parse(query, DefaultField));
 }
Exemplo n.º 23
0
        public void TestMultiAnalyzer()
        {
            StandardQueryParser qp = new StandardQueryParser();

            qp.Analyzer = (new MultiAnalyzer());

            // trivial, no multiple tokens:
            assertEquals("foo", qp.Parse("foo", "").toString());
            assertEquals("foo", qp.Parse("\"foo\"", "").toString());
            assertEquals("foo foobar", qp.Parse("foo foobar", "").toString());
            assertEquals("\"foo foobar\"", qp.Parse("\"foo foobar\"", "").toString());
            assertEquals("\"foo foobar blah\"", qp.Parse("\"foo foobar blah\"", "")
                         .toString());

            // two tokens at the same position:
            assertEquals("(multi multi2) foo", qp.Parse("multi foo", "").toString());
            assertEquals("foo (multi multi2)", qp.Parse("foo multi", "").toString());
            assertEquals("(multi multi2) (multi multi2)", qp.Parse("multi multi", "")
                         .toString());
            assertEquals("+(foo (multi multi2)) +(bar (multi multi2))", qp.Parse(
                             "+(foo multi) +(bar multi)", "").toString());
            assertEquals("+(foo (multi multi2)) field:\"bar (multi multi2)\"", qp
                         .Parse("+(foo multi) field:\"bar multi\"", "").toString());

            // phrases:
            assertEquals("\"(multi multi2) foo\"", qp.Parse("\"multi foo\"", "")
                         .toString());
            assertEquals("\"foo (multi multi2)\"", qp.Parse("\"foo multi\"", "")
                         .toString());
            assertEquals("\"foo (multi multi2) foobar (multi multi2)\"", qp.Parse(
                             "\"foo multi foobar multi\"", "").toString());

            // fields:
            assertEquals("(field:multi field:multi2) field:foo", qp.Parse(
                             "field:multi field:foo", "").toString());
            assertEquals("field:\"(multi multi2) foo\"", qp.Parse(
                             "field:\"multi foo\"", "").toString());

            // three tokens at one position:
            assertEquals("triplemulti multi3 multi2", qp.Parse("triplemulti", "")
                         .toString());
            assertEquals("foo (triplemulti multi3 multi2) foobar", qp.Parse(
                             "foo triplemulti foobar", "").toString());

            // phrase with non-default slop:
            assertEquals("\"(multi multi2) foo\"~10", qp.Parse("\"multi foo\"~10", "")
                         .toString());

            // phrase with non-default boost:
            assertEquals("\"(multi multi2) foo\"^2.0", qp.Parse("\"multi foo\"^2", "")
                         .toString());

            // phrase after changing default slop
#pragma warning disable 612, 618
            qp.SetDefaultPhraseSlop(99);
#pragma warning restore 612, 618
            assertEquals("\"(multi multi2) foo\"~99 bar", qp.Parse("\"multi foo\" bar",
                                                                   "").toString());
            assertEquals("\"(multi multi2) foo\"~99 \"foo bar\"~2", qp.Parse(
                             "\"multi foo\" \"foo bar\"~2", "").toString());
#pragma warning disable 612, 618
            qp.SetDefaultPhraseSlop(0);
#pragma warning restore 612, 618

            // non-default operator:
            qp.DefaultOperator = (StandardQueryConfigHandler.Operator.AND);
            assertEquals("+(multi multi2) +foo", qp.Parse("multi foo", "").toString());
        }
Exemplo n.º 24
0
        public void BeforeClass()
        {
            ANALYZER = new MockAnalyzer(Random());

            qp = new StandardQueryParser(ANALYZER);

            HashMap <String, /*Number*/ object> randomNumberMap = new HashMap <string, object>();

            /*SimpleDateFormat*/
            string dateFormat;
            long   randomDate;
            bool   dateFormatSanityCheckPass;
            int    count = 0;

            do
            {
                if (count > 100)
                {
                    fail("This test has problems to find a sane random DateFormat/NumberFormat. Stopped trying after 100 iterations.");
                }

                dateFormatSanityCheckPass = true;
                LOCALE     = randomLocale(Random());
                TIMEZONE   = randomTimeZone(Random());
                DATE_STYLE = randomDateStyle(Random());
                TIME_STYLE = randomDateStyle(Random());

                //// assumes localized date pattern will have at least year, month, day,
                //// hour, minute
                //dateFormat = (SimpleDateFormat)DateFormat.getDateTimeInstance(
                //    DATE_STYLE, TIME_STYLE, LOCALE);

                //// not all date patterns includes era, full year, timezone and second,
                //// so we add them here
                //dateFormat.applyPattern(dateFormat.toPattern() + " G s Z yyyy");
                //dateFormat.setTimeZone(TIMEZONE);

                DATE_FORMAT = new NumberDateFormat(DATE_STYLE, TIME_STYLE, LOCALE)
                {
                    TimeZone = TIMEZONE
                };
                dateFormat = DATE_FORMAT.GetDateFormat();

                do
                {
                    randomDate = Random().nextLong();

                    // prune date value so it doesn't pass in insane values to some
                    // calendars.
                    randomDate = randomDate % 3400000000000L;

                    // truncate to second
                    randomDate = (randomDate / 1000L) * 1000L;

                    // only positive values
                    randomDate = Math.Abs(randomDate);
                } while (randomDate == 0L);

                dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat, randomDate);

                dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat, 0);

                dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat,
                                                                   -randomDate);

                count++;
            } while (!dateFormatSanityCheckPass);

            //NUMBER_FORMAT = NumberFormat.getNumberInstance(LOCALE);
            //NUMBER_FORMAT.setMaximumFractionDigits((Random().nextInt() & 20) + 1);
            //NUMBER_FORMAT.setMinimumFractionDigits((Random().nextInt() & 20) + 1);
            //NUMBER_FORMAT.setMaximumIntegerDigits((Random().nextInt() & 20) + 1);
            //NUMBER_FORMAT.setMinimumIntegerDigits((Random().nextInt() & 20) + 1);

            NUMBER_FORMAT = new NumberFormat(LOCALE);

            double randomDouble;
            long   randomLong;
            int    randomInt;
            float  randomFloat;

            while ((randomLong = Convert.ToInt64(NormalizeNumber(Math.Abs(Random().nextLong()))
                                                 )) == 0L)
            {
                ;
            }
            while ((randomDouble = Convert.ToDouble(NormalizeNumber(Math.Abs(Random().NextDouble()))
                                                    )) == 0.0)
            {
                ;
            }
            while ((randomFloat = Convert.ToSingle(NormalizeNumber(Math.Abs(Random().nextFloat()))
                                                   )) == 0.0f)
            {
                ;
            }
            while ((randomInt = Convert.ToInt32(NormalizeNumber(Math.Abs(Random().nextInt())))) == 0)
            {
                ;
            }

            randomNumberMap.Put(FieldType.NumericType.LONG.ToString(), randomLong);
            randomNumberMap.Put(FieldType.NumericType.INT.ToString(), randomInt);
            randomNumberMap.Put(FieldType.NumericType.FLOAT.ToString(), randomFloat);
            randomNumberMap.Put(FieldType.NumericType.DOUBLE.ToString(), randomDouble);
            randomNumberMap.Put(DATE_FIELD_NAME, randomDate);

            RANDOM_NUMBER_MAP = Collections.UnmodifiableMap(randomNumberMap);

            directory = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), directory,
                                                             NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))
                                                             .SetMaxBufferedDocs(TestUtil.NextInt(Random(), 50, 1000))
                                                             .SetMergePolicy(NewLogMergePolicy()));

            Document doc = new Document();
            HashMap <String, NumericConfig> numericConfigMap = new HashMap <String, NumericConfig>();
            HashMap <String, Field>         numericFieldMap  = new HashMap <String, Field>();

            qp.NumericConfigMap = (numericConfigMap);

            foreach (FieldType.NumericType type in Enum.GetValues(typeof(FieldType.NumericType)))
            {
                numericConfigMap.Put(type.ToString(), new NumericConfig(PRECISION_STEP,
                                                                        NUMBER_FORMAT, type));

                FieldType ft2 = new FieldType(IntField.TYPE_NOT_STORED);
                ft2.NumericTypeValue     = (type);
                ft2.Stored               = (true);
                ft2.NumericPrecisionStep = (PRECISION_STEP);
                ft2.Freeze();
                Field field;

                switch (type)
                {
                case FieldType.NumericType.INT:
                    field = new IntField(type.ToString(), 0, ft2);
                    break;

                case FieldType.NumericType.FLOAT:
                    field = new FloatField(type.ToString(), 0.0f, ft2);
                    break;

                case FieldType.NumericType.LONG:
                    field = new LongField(type.ToString(), 0L, ft2);
                    break;

                case FieldType.NumericType.DOUBLE:
                    field = new DoubleField(type.ToString(), 0.0, ft2);
                    break;

                default:
                    fail();
                    field = null;
                    break;
                }
                numericFieldMap.Put(type.ToString(), field);
                doc.Add(field);
            }

            numericConfigMap.Put(DATE_FIELD_NAME, new NumericConfig(PRECISION_STEP,
                                                                    DATE_FORMAT, FieldType.NumericType.LONG));
            FieldType ft = new FieldType(LongField.TYPE_NOT_STORED);

            ft.Stored = (true);
            ft.NumericPrecisionStep = (PRECISION_STEP);
            LongField dateField = new LongField(DATE_FIELD_NAME, 0L, ft);

            numericFieldMap.Put(DATE_FIELD_NAME, dateField);
            doc.Add(dateField);

            foreach (NumberType numberType in Enum.GetValues(typeof(NumberType)))
            {
                setFieldValues(numberType, numericFieldMap);
                if (VERBOSE)
                {
                    Console.WriteLine("Indexing document: " + doc);
                }
                writer.AddDocument(doc);
            }

            reader   = writer.Reader;
            searcher = NewSearcher(reader);
            writer.Dispose();
        }