lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [02/58] lucenenet git commit: WIP on QueryParsers.Flexible
Date Tue, 06 Dec 2016 15:11:37 GMT
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/c83be6be/src/Lucene.Net.Tests.QueryParser/Flexible/Standard/TestMultiFieldQPHelper.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Flexible/Standard/TestMultiFieldQPHelper.cs
b/src/Lucene.Net.Tests.QueryParser/Flexible/Standard/TestMultiFieldQPHelper.cs
new file mode 100644
index 0000000..84affc0
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Flexible/Standard/TestMultiFieldQPHelper.cs
@@ -0,0 +1,409 @@
+´╗┐using Lucene.Net.Analysis;
+using Lucene.Net.Documents;
+using Lucene.Net.Index;
+using Lucene.Net.QueryParsers.Flexible.Standard.Config;
+using Lucene.Net.Search;
+using Lucene.Net.Support;
+using Lucene.Net.Util;
+using NUnit.Framework;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using static Lucene.Net.Analysis.Analyzer;
+
+namespace Lucene.Net.QueryParsers.Flexible.Standard
+{
+    /// <summary>
+    /// This test case is a copy of the core Lucene query parser test, it was adapted
+    /// to use new QueryParserHelper instead of the old query parser.
+    /// 
+    /// Tests QueryParser.
+    /// </summary>
+    public class TestMultiFieldQPHelper : LuceneTestCase
+    {
+        /**
+        * test stop words parsing for both the non static form, and for the
+        * corresponding static form (qtxt, fields[]).
+        */
+        [Test]
+        public void testStopwordsParsing()
+        {
+            assertStopQueryEquals("one", "b:one t:one");
+            assertStopQueryEquals("one stop", "b:one t:one");
+            assertStopQueryEquals("one (stop)", "b:one t:one");
+            assertStopQueryEquals("one ((stop))", "b:one t:one");
+            assertStopQueryEquals("stop", "");
+            assertStopQueryEquals("(stop)", "");
+            assertStopQueryEquals("((stop))", "");
+        }
+
+        // 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 = (Query)mfqp.Parse(qtxt, null);
+            assertEquals(expectedRes, q.toString());
+
+            q = QueryParserUtil.Parse(qtxt, fields, occur, a);
+            assertEquals(expectedRes, q.toString());
+        }
+
+        [Test]
+        public void testSimple()
+        {
+            String[]
+            fields = { "b", "t" };
+            StandardQueryParser mfqp = new StandardQueryParser();
+            mfqp.SetMultiFields(fields);
+            mfqp.Analyzer = (new MockAnalyzer(Random()));
+
+            Query q = (Query)mfqp.Parse("one", null);
+            assertEquals("b:one t:one", q.toString());
+
+            q = (Query)mfqp.Parse("one two", null);
+            assertEquals("(b:one t:one) (b:two t:two)", q.toString());
+
+            q = (Query)mfqp.Parse("+one +two", null);
+            assertEquals("+(b:one t:one) +(b:two t:two)", q.toString());
+
+            q = (Query)mfqp.Parse("+one -two -three", null);
+            assertEquals("+(b:one t:one) -(b:two t:two) -(b:three t:three)", q
+                .toString());
+
+            q = (Query)mfqp.Parse("one^2 two", null);
+            assertEquals("((b:one t:one)^2.0) (b:two t:two)", q.toString());
+
+            q = (Query)mfqp.Parse("one~ two", null);
+            assertEquals("(b:one~2 t:one~2) (b:two t:two)", q.toString());
+
+            q = (Query)mfqp.Parse("one~0.8 two^2", null);
+            assertEquals("(b:one~0 t:one~0) ((b:two t:two)^2.0)", q.toString());
+
+            q = (Query)mfqp.Parse("one* two*", null);
+            assertEquals("(b:one* t:one*) (b:two* t:two*)", q.toString());
+
+            q = (Query)mfqp.Parse("[a TO c] two", null);
+            assertEquals("(b:[a TO c] t:[a TO c]) (b:two t:two)", q.toString());
+
+            q = (Query)mfqp.Parse("w?ldcard", null);
+            assertEquals("b:w?ldcard t:w?ldcard", q.toString());
+
+            q = (Query)mfqp.Parse("\"foo bar\"", null);
+            assertEquals("b:\"foo bar\" t:\"foo bar\"", q.toString());
+
+            q = (Query)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 = (Query)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 = (Query)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 = (Query)mfqp.Parse("one f:two", null);
+            assertEquals("(b:one t:one) f:two", q.toString());
+
+            // AND mode:
+            mfqp.SetDefaultOperator(/*StandardQueryConfigHandler.*/Operator.AND);
+            q = (Query)mfqp.Parse("one two", null);
+            assertEquals("+(b:one t:one) +(b:two t:two)", q.toString());
+            q = (Query)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());
+
+        }
+
+        [Test]
+        public void testBoostsSimple()
+        {
+            IDictionary<String, float?> boosts = new Dictionary<String, float?>();
// LUCENENET TODO: make this non-nullable..?
+            boosts.Put("b", 5);
+            boosts.Put("t", 10);
+            String[] fields = { "b", "t" };
+            StandardQueryParser mfqp = new StandardQueryParser();
+            mfqp.SetMultiFields(fields);
+            mfqp.SetFieldsBoost(boosts);
+            mfqp.Analyzer = (new MockAnalyzer(Random()));
+
+            // Check for simple
+            Query q = (Query)mfqp.Parse("one", null);
+            assertEquals("b:one^5.0 t:one^10.0", q.toString());
+
+            // Check for AND
+            q = (Query)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 = (Query)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 = (Query)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 = (Query)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());
+        }
+
+        [Test]
+        public void testStaticMethod1()
+        {
+            String[]
+            fields = { "b", "t" };
+            String[]
+            queries = { "one", "two" };
+            Query q = QueryParserUtil.Parse(queries, fields, new MockAnalyzer(Random()));
+            assertEquals("b:one t:two", q.toString());
+
+            String[] queries2 = { "+one", "+two" };
+            q = QueryParserUtil.Parse(queries2, fields, new MockAnalyzer(Random()));
+            assertEquals("b:one t:two", q.toString());
+
+            String[] queries3 = { "one", "+two" };
+            q = QueryParserUtil.Parse(queries3, fields, new MockAnalyzer(Random()));
+            assertEquals("b:one t:two", q.toString());
+
+            String[] queries4 = { "one +more", "+two" };
+            q = QueryParserUtil.Parse(queries4, fields, new MockAnalyzer(Random()));
+            assertEquals("(b:one +b:more) t:two", q.toString());
+
+            String[] queries5 = { "blah" };
+            try
+            {
+                q = QueryParserUtil.Parse(queries5, fields, new MockAnalyzer(Random()));
+                fail();
+            }
+            catch (ArgumentException e)
+            {
+                // expected exception, array length differs
+            }
+
+            // check also with stop words for this static form (qtxts[], fields[]).
+            TestQPHelper.QPTestAnalyzer stopA = new TestQPHelper.QPTestAnalyzer();
+
+            String[] queries6 = { "((+stop))", "+((stop))" };
+            q = QueryParserUtil.Parse(queries6, fields, stopA);
+            assertEquals("", q.toString());
+
+            String[] queries7 = { "one ((+stop)) +more", "+((stop)) +two" };
+            q = QueryParserUtil.Parse(queries7, fields, stopA);
+            assertEquals("(b:one +b:more) (+t:two)", q.toString());
+
+        }
+
+        [Test]
+        public void testStaticMethod2()
+        {
+            String[]
+            fields = { "b", "t" };
+            BooleanClause.Occur[]
+            flags = {
+        BooleanClause.Occur.MUST,
+        BooleanClause.Occur.MUST_NOT };
+            Query q = QueryParserUtil.Parse("one", fields, flags,
+                new MockAnalyzer(Random()));
+            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();
+            }
+            catch (ArgumentException e)
+            {
+                // expected exception, array length differs
+            }
+        }
+
+        [Test]
+        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();
+            }
+            catch (ArgumentException e)
+            {
+                // expected exception, array length differs
+            }
+        }
+
+        [Test]
+        public void testStaticMethod3()
+        {
+            String[]
+            queries = { "one", "two", "three" };
+            String[]
+            fields = { "f1", "f2", "f3" };
+            BooleanClause.Occur[]
+            flags = {
+        BooleanClause.Occur.MUST,
+        BooleanClause.Occur.MUST_NOT, BooleanClause.Occur.SHOULD };
+            Query q = QueryParserUtil.Parse(queries, fields, flags,
+                new MockAnalyzer(Random()));
+            assertEquals("+f1:one -f2:two f3:three", q.toString());
+
+            try
+            {
+                BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST };
+                q = QueryParserUtil
+                    .Parse(queries, fields, flags2, new MockAnalyzer(Random()));
+                fail();
+            }
+            catch (ArgumentException e)
+            {
+                // expected exception, array length differs
+            }
+        }
+
+        [Test]
+        public void testStaticMethod3Old()
+        {
+            String[]
+            queries = { "one", "two" };
+            String[]
+            fields = { "b", "t" };
+            BooleanClause.Occur[]
+            flags = {
+        BooleanClause.Occur.MUST,
+        BooleanClause.Occur.MUST_NOT };
+            Query q = QueryParserUtil.Parse(queries, fields, flags,
+                new MockAnalyzer(Random()));
+            assertEquals("+b:one -t:two", q.toString());
+
+            try
+            {
+                BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST };
+                q = QueryParserUtil
+                    .Parse(queries, fields, flags2, new MockAnalyzer(Random()));
+                fail();
+            }
+            catch (ArgumentException e)
+            {
+                // expected exception, array length differs
+            }
+        }
+
+        [Test]
+        public void testAnalyzerReturningNull()
+        {
+            String[]
+            fields = new String[] { "f1", "f2", "f3" };
+            StandardQueryParser parser = new StandardQueryParser();
+            parser.SetMultiFields(fields);
+            parser.Analyzer = (new AnalyzerReturningNull());
+
+            Query q = (Query)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 = (Query)parser.Parse("bla*", null);
+            assertEquals("f1:bla* f2:bla* f3:bla*", q.toString());
+            q = (Query)parser.Parse("bla~", null);
+            assertEquals("f1:bla~2 f2:bla~2 f3:bla~2", q.toString());
+            q = (Query)parser.Parse("[a TO c]", null);
+            assertEquals("f1:[a TO c] f2:[a TO c] f3:[a TO c]", q.toString());
+        }
+
+        [Test]
+        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.SetDefaultOperator(/*StandardQueryConfigHandler.*/Operator.AND);
+            Query q = (Query)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();
+        }
+
+        /**
+         * Return no tokens for field "f1".
+         */
+        private class AnalyzerReturningNull : Analyzer
+        {
+            MockAnalyzer stdAnalyzer = new MockAnalyzer(Random());
+
+            public AnalyzerReturningNull()
+                        : base(PER_FIELD_REUSE_STRATEGY)
+            {
+            }
+
+
+            public override TextReader InitReader(String fieldName, TextReader reader) //
LUCENENET TODO: Change accessiblity of InitReader to protected
+            {
+                if ("f1".equals(fieldName))
+                {
+                    // we don't use the reader, so close it:
+                    IOUtils.CloseWhileHandlingException(reader);
+                    // return empty reader, so MockTokenizer returns no tokens:
+                    return new StringReader("");
+                }
+                else
+                {
+                    return base.InitReader(fieldName, reader);
+                }
+            }
+
+
+            public override TokenStreamComponents CreateComponents(String fieldName, TextReader
reader)
+            {
+                return stdAnalyzer.CreateComponents(fieldName, reader);
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/c83be6be/src/Lucene.Net.Tests.QueryParser/Flexible/Standard/TestNumericQueryParser.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Flexible/Standard/TestNumericQueryParser.cs
b/src/Lucene.Net.Tests.QueryParser/Flexible/Standard/TestNumericQueryParser.cs
new file mode 100644
index 0000000..04ac847
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Flexible/Standard/TestNumericQueryParser.cs
@@ -0,0 +1,523 @@
+´╗┐// LUCENENET TODO: Complete implementation
+
+//using Lucene.Net;
+//using Lucene.Net.Analysis;
+//using Lucene.Net.Index;
+//using Lucene.Net.QueryParsers.Flexible.Standard.Parser;
+//using Lucene.Net.Search;
+//using Lucene.Net.Store;
+//using Lucene.Net.Util;
+//using NUnit.Framework;
+//using System;
+//using System.Collections.Generic;
+//using System.Linq;
+//using System.Text;
+//using System.Threading.Tasks;
+
+//namespace Lucene.Net.QueryParsers.Flexible.Standard
+//{
+//    public class TestNumericQueryParser : LuceneTestCase
+//    {
+//        private static enum NumberType
+//        {
+//            NEGATIVE, ZERO, POSITIVE;
+//        }
+
+//        private readonly static int[] DATE_STYLES = {DateFormat.FULL, DateFormat.LONG,
+//      DateFormat.MEDIUM, DateFormat.SHORT};
+
+//        private readonly static int PRECISION_STEP = 8;
+//        private readonly static String FIELD_NAME = "field";
+//        private static Locale LOCALE;
+//        private static TimeZone TIMEZONE;
+//        private static Map<String, Number> RANDOM_NUMBER_MAP;
+//        private readonly static EscapeQuerySyntax ESCAPER = new EscapeQuerySyntaxImpl();
+//        private readonly static String DATE_FIELD_NAME = "date";
+//        private static int DATE_STYLE;
+//        private static int TIME_STYLE;
+
+//        private static Analyzer ANALYZER;
+
+//        private static NumberFormat NUMBER_FORMAT;
+
+//        private static StandardQueryParser qp;
+
+//        private static NumberDateFormat DATE_FORMAT;
+
+//        private static Directory directory = null;
+//        private static IndexReader reader = null;
+//        private static IndexSearcher searcher = null;
+
+//        private static bool checkDateFormatSanity(DateFormat dateFormat, long date)
+//        {
+//            try
+//            {
+//                return date == dateFormat.parse(dateFormat.format(new DateTime(date)))
+//                  .getTime();
+//            }
+//            catch (ParseException e)
+//            {
+//                return false;
+//            }
+//        }
+
+//        [TestFixtureSetUp]
+//  public static void beforeClass() 
+//        {
+//            ANALYZER = new MockAnalyzer(Random());
+    
+//    qp = new StandardQueryParser(ANALYZER);
+
+//        HashMap<String, Number> randomNumberMap = new HashMap<>();
+
+//        SimpleDateFormat 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(dateFormat);
+      
+//      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);
+    
+//    double randomDouble;
+//long randomLong;
+//int randomInt;
+//float randomFloat;
+    
+//    while ((randomLong = normalizeNumber(Math.abs(Random().nextLong()))
+//        .longValue()) == 0L)
+//      ;
+//    while ((randomDouble = normalizeNumber(Math.abs(Random().nextDouble()))
+//        .doubleValue()) == 0.0)
+//      ;
+//    while ((randomFloat = normalizeNumber(Math.abs(Random().nextFloat()))
+//        .floatValue()) == 0.0f)
+//      ;
+//    while ((randomInt = normalizeNumber(Math.abs(Random().nextInt())).intValue()) == 0)
+//      ;
+    
+//    randomNumberMap.put(NumericType.LONG.name(), randomLong);
+//    randomNumberMap.put(NumericType.INT.name(), randomInt);
+//    randomNumberMap.put(NumericType.FLOAT.name(), randomFloat);
+//    randomNumberMap.put(NumericType.DOUBLE.name(), 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<>();
+//HashMap<String, Field> numericFieldMap = new HashMap<>();
+//qp.setNumericConfigMap(numericConfigMap);
+    
+//    for (NumericType type : NumericType.values()) {
+//      numericConfigMap.put(type.name(), new NumericConfig(PRECISION_STEP,
+//          NUMBER_FORMAT, type));
+
+//      FieldType ft = new FieldType(IntField.TYPE_NOT_STORED);
+//ft.setNumericType(type);
+//      ft.setStored(true);
+//      ft.setNumericPrecisionStep(PRECISION_STEP);
+//      ft.freeze();
+//      final Field field;
+
+//      switch(type) {
+//      case INT:
+//        field = new IntField(type.name(), 0, ft);
+//        break;
+//      case FLOAT:
+//        field = new FloatField(type.name(), 0.0f, ft);
+//        break;
+//      case LONG:
+//        field = new LongField(type.name(), 0l, ft);
+//        break;
+//      case DOUBLE:
+//        field = new DoubleField(type.name(), 0.0, ft);
+//        break;
+//      default:
+//        fail();
+//field = null;
+//      }
+//      numericFieldMap.put(type.name(), field);
+//      doc.add(field);
+//    }
+    
+//    numericConfigMap.put(DATE_FIELD_NAME, new NumericConfig(PRECISION_STEP,
+//        DATE_FORMAT, NumericType.LONG));
+//    FieldType ft = new FieldType(LongField.TYPE_NOT_STORED);
+//ft.setStored(true);
+//    ft.setNumericPrecisionStep(PRECISION_STEP);
+//    LongField dateField = new LongField(DATE_FIELD_NAME, 0l, ft);
+//numericFieldMap.put(DATE_FIELD_NAME, dateField);
+//    doc.add(dateField);
+    
+//    for (NumberType numberType : NumberType.values()) {
+//      setFieldValues(numberType, numericFieldMap);
+//      if (VERBOSE) System.out.println("Indexing document: " + doc);
+//writer.addDocument(doc);
+//    }
+    
+//    reader = writer.getReader();
+//    searcher = newSearcher(reader);
+//writer.close();
+    
+//  }
+  
+//  private static Number getNumberType(NumberType numberType, String fieldName)
+//{
+
+//    if (numberType == null)
+//    {
+//        return null;
+//    }
+
+//    switch (numberType)
+//    {
+
+//        case POSITIVE:
+//            return RANDOM_NUMBER_MAP.get(fieldName);
+
+//        case NEGATIVE:
+//            Number number = RANDOM_NUMBER_MAP.get(fieldName);
+
+//            if (NumericType.LONG.name().equals(fieldName)
+//                || DATE_FIELD_NAME.equals(fieldName))
+//            {
+//                number = -number.longValue();
+
+//            }
+//            else if (NumericType.DOUBLE.name().equals(fieldName))
+//            {
+//                number = -number.doubleValue();
+
+//            }
+//            else if (NumericType.FLOAT.name().equals(fieldName))
+//            {
+//                number = -number.floatValue();
+
+//            }
+//            else if (NumericType.INT.name().equals(fieldName))
+//            {
+//                number = -number.intValue();
+
+//            }
+//            else
+//            {
+//                throw new IllegalArgumentException("field name not found: "
+//                    + fieldName);
+//            }
+
+//            return number;
+
+//        default:
+//            return 0;
+
+//    }
+
+//}
+
+//private static void setFieldValues(NumberType numberType,
+//    HashMap<String, Field> numericFieldMap)
+//{
+
+//    Number number = getNumberType(numberType, NumericType.DOUBLE
+//        .name());
+//    numericFieldMap.get(NumericType.DOUBLE.name()).setDoubleValue(
+//        number.doubleValue());
+
+//    number = getNumberType(numberType, NumericType.INT.name());
+//    numericFieldMap.get(NumericType.INT.name()).setIntValue(
+//        number.intValue());
+
+//    number = getNumberType(numberType, NumericType.LONG.name());
+//    numericFieldMap.get(NumericType.LONG.name()).setLongValue(
+//        number.longValue());
+
+//    number = getNumberType(numberType, NumericType.FLOAT.name());
+//    numericFieldMap.get(NumericType.FLOAT.name()).setFloatValue(
+//        number.floatValue());
+
+//    number = getNumberType(numberType, DATE_FIELD_NAME);
+//    numericFieldMap.get(DATE_FIELD_NAME).setLongValue(number.longValue());
+//}
+
+//private static int randomDateStyle(Random random)
+//{
+//    return DATE_STYLES[random.nextInt(DATE_STYLES.length)];
+//}
+
+//@Test
+//  public void testInclusiveNumericRange() throws Exception
+//{
+//    assertRangeQuery(NumberType.ZERO, NumberType.ZERO, true, true, 1);
+//    assertRangeQuery(NumberType.ZERO, NumberType.POSITIVE, true, true, 2);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.ZERO, true, true, 2);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.POSITIVE, true, true, 3);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.NEGATIVE, true, true, 1);
+//}
+
+//@Test
+//  // test disabled since standard syntax parser does not work with inclusive and
+//  // exclusive at the same time
+//  public void testInclusiveLowerNumericRange() throws Exception
+//{
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.ZERO, false, true, 1);
+//    assertRangeQuery(NumberType.ZERO, NumberType.POSITIVE, false, true, 1);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.POSITIVE, false, true, 2);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.NEGATIVE, false, true, 0);
+//}
+
+//@Test
+//  // test disabled since standard syntax parser does not work with inclusive and
+//  // exclusive at the same time
+//  public void testInclusiveUpperNumericRange() throws Exception
+//{
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.ZERO, true, false, 1);
+//    assertRangeQuery(NumberType.ZERO, NumberType.POSITIVE, true, false, 1);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.POSITIVE, true, false, 2);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.NEGATIVE, true, false, 0);
+//}
+
+//@Test
+//  public void testExclusiveNumericRange() throws Exception
+//{
+//    assertRangeQuery(NumberType.ZERO, NumberType.ZERO, false, false, 0);
+//    assertRangeQuery(NumberType.ZERO, NumberType.POSITIVE, false, false, 0);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.ZERO, false, false, 0);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.POSITIVE, false, false, 1);
+//    assertRangeQuery(NumberType.NEGATIVE, NumberType.NEGATIVE, false, false, 0);
+//}
+
+//@Test
+//  public void testOpenRangeNumericQuery() throws Exception
+//{
+//    assertOpenRangeQuery(NumberType.ZERO, "<", 1);
+//    assertOpenRangeQuery(NumberType.POSITIVE, "<", 2);
+//    assertOpenRangeQuery(NumberType.NEGATIVE, "<", 0);
+
+//    assertOpenRangeQuery(NumberType.ZERO, "<=", 2);
+//    assertOpenRangeQuery(NumberType.POSITIVE, "<=", 3);
+//    assertOpenRangeQuery(NumberType.NEGATIVE, "<=", 1);
+
+//    assertOpenRangeQuery(NumberType.ZERO, ">", 1);
+//    assertOpenRangeQuery(NumberType.POSITIVE, ">", 0);
+//    assertOpenRangeQuery(NumberType.NEGATIVE, ">", 2);
+
+//    assertOpenRangeQuery(NumberType.ZERO, ">=", 2);
+//    assertOpenRangeQuery(NumberType.POSITIVE, ">=", 1);
+//    assertOpenRangeQuery(NumberType.NEGATIVE, ">=", 3);
+
+//    assertOpenRangeQuery(NumberType.NEGATIVE, "=", 1);
+//    assertOpenRangeQuery(NumberType.ZERO, "=", 1);
+//    assertOpenRangeQuery(NumberType.POSITIVE, "=", 1);
+
+//    assertRangeQuery(NumberType.NEGATIVE, null, true, true, 3);
+//    assertRangeQuery(NumberType.NEGATIVE, null, false, true, 2);
+//    assertRangeQuery(NumberType.POSITIVE, null, true, false, 1);
+//    assertRangeQuery(NumberType.ZERO, null, false, false, 1);
+
+//    assertRangeQuery(null, NumberType.POSITIVE, true, true, 3);
+//    assertRangeQuery(null, NumberType.POSITIVE, true, false, 2);
+//    assertRangeQuery(null, NumberType.NEGATIVE, false, true, 1);
+//    assertRangeQuery(null, NumberType.ZERO, false, false, 1);
+
+//    assertRangeQuery(null, null, false, false, 3);
+//    assertRangeQuery(null, null, true, true, 3);
+
+//}
+
+//@Test
+//  public void testSimpleNumericQuery() throws Exception
+//{
+//    assertSimpleQuery(NumberType.ZERO, 1);
+//    assertSimpleQuery(NumberType.POSITIVE, 1);
+//    assertSimpleQuery(NumberType.NEGATIVE, 1);
+//}
+
+//public void assertRangeQuery(NumberType lowerType, NumberType upperType,
+//    boolean lowerInclusive, boolean upperInclusive, int expectedDocCount)
+//      throws QueryNodeException, IOException {
+    
+//    StringBuilder sb = new StringBuilder();
+
+//String lowerInclusiveStr = (lowerInclusive ? "[" : "{");
+//String upperInclusiveStr = (upperInclusive ? "]" : "}");
+    
+//    for (NumericType type : NumericType.values()) {
+//      String lowerStr = numberToString(getNumberType(lowerType, type.name()));
+//String upperStr = numberToString(getNumberType(upperType, type.name()));
+
+//sb.append("+").append(type.name()).append(':').append(lowerInclusiveStr)
+//          .append('"').append(lowerStr).append("\" TO \"").append(upperStr)
+//          .append('"').append(upperInclusiveStr).append(' ');
+//    }
+    
+//    Number lowerDateNumber = getNumberType(lowerType, DATE_FIELD_NAME);
+//Number upperDateNumber = getNumberType(upperType, DATE_FIELD_NAME);
+//String lowerDateStr;
+//String upperDateStr;
+    
+//    if (lowerDateNumber != null) {
+//      lowerDateStr = ESCAPER.escape(
+//          DATE_FORMAT.format(new Date(lowerDateNumber.longValue())), LOCALE,
+//          EscapeQuerySyntax.Type.STRING).toString();
+      
+//    } else {
+//      lowerDateStr = "*";
+//    }
+    
+//    if (upperDateNumber != null) {
+//    upperDateStr = ESCAPER.escape(
+//          DATE_FORMAT.format(new Date(upperDateNumber.longValue())), LOCALE,
+//          EscapeQuerySyntax.Type.STRING).toString();
+    
+//    } else {
+//      upperDateStr = "*";
+//    }
+    
+//    sb.append("+").append(DATE_FIELD_NAME).append(':')
+//        .append(lowerInclusiveStr).append('"').append(lowerDateStr).append(
+//            "\" TO \"").append(upperDateStr).append('"').append(
+//            upperInclusiveStr);
+
+
+//    testQuery(sb.toString(), expectedDocCount);
+    
+//  }
+  
+//  public void assertOpenRangeQuery(NumberType boundType, String operator, int expectedDocCount)
+//      throws QueryNodeException, IOException {
+
+//    StringBuilder sb = new StringBuilder();
+    
+//    for (NumericType type : NumericType.values()) {
+//      String boundStr = numberToString(getNumberType(boundType, type.name()));
+
+//sb.append("+").append(type.name()).append(operator).append('"').append(boundStr).append('"').append('
');
+//    }
+    
+//    String boundDateStr = ESCAPER.escape(
+//        DATE_FORMAT.format(new Date(getNumberType(boundType, DATE_FIELD_NAME)
+//            .longValue())), LOCALE, EscapeQuerySyntax.Type.STRING).toString();
+
+//sb.append("+").append(DATE_FIELD_NAME).append(operator).append('"').append(boundDateStr).append('"');
+
+
+//    testQuery(sb.toString(), expectedDocCount);
+//  }
+  
+//  public void assertSimpleQuery(NumberType numberType, int expectedDocCount)
+//      throws QueryNodeException, IOException {
+//    StringBuilder sb = new StringBuilder();
+    
+//    for (NumericType type : NumericType.values()) {
+//      String numberStr = numberToString(getNumberType(numberType, type.name()));
+//sb.append('+').append(type.name()).append(":\"").append(numberStr)
+//          .append("\" ");
+//    }
+    
+//    String dateStr = ESCAPER.escape(
+//        DATE_FORMAT.format(new Date(getNumberType(numberType, DATE_FIELD_NAME)
+//            .longValue())), LOCALE, EscapeQuerySyntax.Type.STRING).toString();
+
+//sb.append('+').append(DATE_FIELD_NAME).append(":\"").append(dateStr)
+//        .append('"');
+
+
+//    testQuery(sb.toString(), expectedDocCount);
+    
+//  }
+  
+//  private void testQuery(String queryStr, int expectedDocCount)
+//      throws QueryNodeException, IOException {
+//    if (VERBOSE) System.out.println("Parsing: " + queryStr);
+
+//Query query = qp.parse(queryStr, FIELD_NAME);
+//    if (VERBOSE) System.out.println("Querying: " + query);
+//TopDocs topDocs = searcher.search(query, 1000);
+
+//String msg = "Query <" + queryStr + "> retrieved " + topDocs.totalHits
+//    + " document(s), " + expectedDocCount + " document(s) expected.";
+    
+//    if (VERBOSE) System.out.println(msg);
+
+
+//    assertEquals(msg, expectedDocCount, topDocs.totalHits);
+//  }
+  
+//  private static String numberToString(Number number)
+//{
+//    return number == null ? "*" : ESCAPER.escape(NUMBER_FORMAT.format(number),
+//        LOCALE, EscapeQuerySyntax.Type.STRING).toString();
+//}
+
+//private static Number normalizeNumber(Number number) throws ParseException
+//{
+//    return NUMBER_FORMAT.parse(NUMBER_FORMAT.format(number));
+//}
+
+//@AfterClass
+//  public static void afterClass() throws Exception
+//{
+//    searcher = null;
+//    reader.close();
+//    reader = null;
+//    directory.close();
+//    directory = null;
+//    qp = null;
+//}
+//    }
+//}


Mime
View raw message