You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by sy...@apache.org on 2016/09/11 21:30:51 UTC
[20/50] [abbrv] lucenenet git commit: Moved Lucene.Net.QueryParser
and Lucene.Net.Tests.QueryParser projects into src\ directory.
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/679ad24c/src/Lucene.Net.Tests.QueryParser/Simple/TestSimpleQueryParser.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Simple/TestSimpleQueryParser.cs b/src/Lucene.Net.Tests.QueryParser/Simple/TestSimpleQueryParser.cs
new file mode 100644
index 0000000..0a9d49f
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Simple/TestSimpleQueryParser.cs
@@ -0,0 +1,728 @@
+\ufeffusing Lucene.Net.Analysis;
+using Lucene.Net.Index;
+using Lucene.Net.Search;
+using Lucene.Net.Support;
+using Lucene.Net.Util;
+using Lucene.Net.Util.Automaton;
+using NUnit.Framework;
+using System.Collections.Generic;
+using System.Text;
+
+namespace Lucene.Net.QueryParser.Simple
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ /// <summary>
+ /// Tests for <see cref="SimpleQueryParser"/>
+ /// </summary>
+ [TestFixture]
+ public class TestSimpleQueryParser : LuceneTestCase
+ {
+ /// <summary>
+ /// helper to parse a query with whitespace+lowercase analyzer across "field",
+ /// with default operator of MUST
+ /// </summary>
+ /// <param name="text"></param>
+ /// <returns></returns>
+ private Query Parse(string text)
+ {
+ Analyzer analyzer = new MockAnalyzer(Random());
+ SimpleQueryParser parser = new SimpleQueryParser(analyzer, "field");
+ parser.DefaultOperator = BooleanClause.Occur.MUST;
+ return parser.Parse(text);
+ }
+
+ /// <summary>
+ /// helper to parse a query with whitespace+lowercase analyzer across "field",
+ /// with default operator of MUST
+ /// </summary>
+ /// <param name="text"></param>
+ /// <param name="flags"></param>
+ /// <returns></returns>
+ private Query Parse(string text, int flags)
+ {
+ Analyzer analyzer = new MockAnalyzer(Random());
+ SimpleQueryParser parser = new SimpleQueryParser(analyzer, new HashMap<string, float>() { { "field", 1f } }, flags);
+ parser.DefaultOperator = BooleanClause.Occur.MUST;
+ return parser.Parse(text);
+ }
+
+ /** test a simple term */
+ [Test]
+ public void TestTerm()
+ {
+ Query expected = new TermQuery(new Term("field", "foobar"));
+
+ assertEquals(expected, Parse("foobar"));
+ }
+
+ /** test a fuzzy query */
+ [Test]
+ public void TestFuzzy()
+ {
+ Query regular = new TermQuery(new Term("field", "foobar"));
+ Query expected = new FuzzyQuery(new Term("field", "foobar"), 2);
+
+ assertEquals(expected, Parse("foobar~2"));
+ assertEquals(regular, Parse("foobar~"));
+ assertEquals(regular, Parse("foobar~a"));
+ assertEquals(regular, Parse("foobar~1a"));
+
+ BooleanQuery @bool = new BooleanQuery();
+ FuzzyQuery fuzzy = new FuzzyQuery(new Term("field", "foo"), LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE);
+ @bool.Add(fuzzy, BooleanClause.Occur.MUST);
+ @bool.Add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.MUST);
+
+ assertEquals(@bool, Parse("foo~" + LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + 1 + " bar"));
+ }
+
+ /** test a simple phrase */
+ [Test]
+ public void TestPhrase()
+ {
+ PhraseQuery expected = new PhraseQuery();
+ expected.Add(new Term("field", "foo"));
+ expected.Add(new Term("field", "bar"));
+
+ assertEquals(expected, Parse("\"foo bar\""));
+ }
+
+ /** test a simple phrase with various slop settings */
+ [Test]
+ public void TestPhraseWithSlop()
+ {
+ PhraseQuery expectedWithSlop = new PhraseQuery();
+ expectedWithSlop.Add(new Term("field", "foo"));
+ expectedWithSlop.Add(new Term("field", "bar"));
+ expectedWithSlop.Slop = (2);
+
+ assertEquals(expectedWithSlop, Parse("\"foo bar\"~2"));
+
+ PhraseQuery expectedWithMultiDigitSlop = new PhraseQuery();
+ expectedWithMultiDigitSlop.Add(new Term("field", "foo"));
+ expectedWithMultiDigitSlop.Add(new Term("field", "bar"));
+ expectedWithMultiDigitSlop.Slop = (10);
+
+ assertEquals(expectedWithMultiDigitSlop, Parse("\"foo bar\"~10"));
+
+ PhraseQuery expectedNoSlop = new PhraseQuery();
+ expectedNoSlop.Add(new Term("field", "foo"));
+ expectedNoSlop.Add(new Term("field", "bar"));
+
+ assertEquals("Ignore trailing tilde with no slop", expectedNoSlop, Parse("\"foo bar\"~"));
+ assertEquals("Ignore non-numeric trailing slop", expectedNoSlop, Parse("\"foo bar\"~a"));
+ assertEquals("Ignore non-numeric trailing slop", expectedNoSlop, Parse("\"foo bar\"~1a"));
+ assertEquals("Ignore negative trailing slop", expectedNoSlop, Parse("\"foo bar\"~-1"));
+
+ PhraseQuery pq = new PhraseQuery();
+ pq.Add(new Term("field", "foo"));
+ pq.Add(new Term("field", "bar"));
+ pq.Slop = (12);
+
+ BooleanQuery expectedBoolean = new BooleanQuery();
+ expectedBoolean.Add(pq, BooleanClause.Occur.MUST);
+ expectedBoolean.Add(new TermQuery(new Term("field", "baz")), BooleanClause.Occur.MUST);
+
+ assertEquals(expectedBoolean, Parse("\"foo bar\"~12 baz"));
+ }
+
+ /** test a simple prefix */
+ [Test]
+ public void TestPrefix()
+ {
+ PrefixQuery expected = new PrefixQuery(new Term("field", "foobar"));
+
+ assertEquals(expected, Parse("foobar*"));
+ }
+
+ /** test some AND'd terms using '+' operator */
+ [Test]
+ public void TestAND()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
+ expected.Add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.MUST);
+
+ assertEquals(expected, Parse("foo+bar"));
+ }
+
+ /** test some AND'd phrases using '+' operator */
+ [Test]
+ public void TestANDPhrase()
+ {
+ PhraseQuery phrase1 = new PhraseQuery();
+ phrase1.Add(new Term("field", "foo"));
+ phrase1.Add(new Term("field", "bar"));
+ PhraseQuery phrase2 = new PhraseQuery();
+ phrase2.Add(new Term("field", "star"));
+ phrase2.Add(new Term("field", "wars"));
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(phrase1, BooleanClause.Occur.MUST);
+ expected.Add(phrase2, BooleanClause.Occur.MUST);
+
+ assertEquals(expected, Parse("\"foo bar\"+\"star wars\""));
+ }
+
+ /** test some AND'd terms (just using whitespace) */
+ [Test]
+ public void TestANDImplicit()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST);
+ expected.Add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.MUST);
+
+ assertEquals(expected, Parse("foo bar"));
+ }
+
+ /** test some OR'd terms */
+ [Test]
+ public void TestOR()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.SHOULD);
+ expected.Add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("foo|bar"));
+ assertEquals(expected, Parse("foo||bar"));
+ }
+
+ /** test some OR'd terms (just using whitespace) */
+ [Test]
+ public void TestORImplicit()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.SHOULD);
+ expected.Add(new TermQuery(new Term("field", "bar")), BooleanClause.Occur.SHOULD);
+
+ SimpleQueryParser parser = new SimpleQueryParser(new MockAnalyzer(Random()), "field");
+ assertEquals(expected, parser.Parse("foo bar"));
+ }
+
+ /** test some OR'd phrases using '|' operator */
+ [Test]
+ public void TestORPhrase()
+ {
+ PhraseQuery phrase1 = new PhraseQuery();
+ phrase1.Add(new Term("field", "foo"));
+ phrase1.Add(new Term("field", "bar"));
+ PhraseQuery phrase2 = new PhraseQuery();
+ phrase2.Add(new Term("field", "star"));
+ phrase2.Add(new Term("field", "wars"));
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(phrase1, BooleanClause.Occur.SHOULD);
+ expected.Add(phrase2, BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("\"foo bar\"|\"star wars\""));
+ }
+
+ /** test negated term */
+ [Test]
+ public void TestNOT()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "foo")), BooleanClause.Occur.MUST_NOT);
+ expected.Add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("-foo"));
+ assertEquals(expected, Parse("-(foo)"));
+ assertEquals(expected, Parse("---foo"));
+ }
+
+ /** test crazy prefixes with multiple asterisks */
+ [Test]
+ public void TestCrazyPrefixes1()
+ {
+ Query expected = new PrefixQuery(new Term("field", "st*ar"));
+
+ assertEquals(expected, Parse("st*ar*"));
+ }
+
+ /** test prefixes with some escaping */
+ [Test]
+ public void TestCrazyPrefixes2()
+ {
+ Query expected = new PrefixQuery(new Term("field", "st*ar\\*"));
+
+ assertEquals(expected, Parse("st*ar\\\\**"));
+ }
+
+ /** not a prefix query! the prefix operator is escaped */
+ [Test]
+ public void TestTermInDisguise()
+ {
+ Query expected = new TermQuery(new Term("field", "st*ar\\*"));
+
+ assertEquals(expected, Parse("sT*Ar\\\\\\*"));
+ }
+
+ // a number of test cases here have garbage/errors in
+ // the syntax passed in to test that the query can
+ // still be interpreted as a guess to what the human
+ // input was trying to be
+
+ [Test]
+ public void TestGarbageTerm()
+ {
+ Query expected = new TermQuery(new Term("field", "star"));
+
+ assertEquals(expected, Parse("star"));
+ assertEquals(expected, Parse("star\n"));
+ assertEquals(expected, Parse("star\r"));
+ assertEquals(expected, Parse("star\t"));
+ assertEquals(expected, Parse("star("));
+ assertEquals(expected, Parse("star)"));
+ assertEquals(expected, Parse("star\""));
+ assertEquals(expected, Parse("\t \r\n\nstar \n \r \t "));
+ assertEquals(expected, Parse("- + \"\" - star \\"));
+ }
+
+ [Test]
+ public void TestGarbageEmpty()
+ {
+ assertNull(Parse(""));
+ assertNull(Parse(" "));
+ assertNull(Parse(" "));
+ assertNull(Parse("\\ "));
+ assertNull(Parse("\\ \\ "));
+ assertNull(Parse("\"\""));
+ assertNull(Parse("\" \""));
+ assertNull(Parse("\" \"|\" \""));
+ assertNull(Parse("(\" \"|\" \")"));
+ assertNull(Parse("\" \" \" \""));
+ assertNull(Parse("(\" \" \" \")"));
+ }
+
+ [Test]
+ public void TestGarbageAND()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.MUST);
+ expected.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.MUST);
+
+ assertEquals(expected, Parse("star wars"));
+ assertEquals(expected, Parse("star+wars"));
+ assertEquals(expected, Parse(" star wars "));
+ assertEquals(expected, Parse(" star + wars "));
+ assertEquals(expected, Parse(" | star + + | wars "));
+ assertEquals(expected, Parse(" | star + + | wars \\"));
+ }
+
+ [Test]
+ public void TestGarbageOR()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.SHOULD);
+ expected.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("star|wars"));
+ assertEquals(expected, Parse(" star | wars "));
+ assertEquals(expected, Parse(" | star | + | wars "));
+ assertEquals(expected, Parse(" + star | + + wars \\"));
+ }
+
+ [Test]
+ public void TestGarbageNOT()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.MUST_NOT);
+ expected.Add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("-star"));
+ assertEquals(expected, Parse("---star"));
+ assertEquals(expected, Parse("- -star -"));
+ }
+
+ [Test]
+ public void TestGarbagePhrase()
+ {
+ PhraseQuery expected = new PhraseQuery();
+ expected.Add(new Term("field", "star"));
+ expected.Add(new Term("field", "wars"));
+
+ assertEquals(expected, Parse("\"star wars\""));
+ assertEquals(expected, Parse("\"star wars\\ \""));
+ assertEquals(expected, Parse("\"\" | \"star wars\""));
+ assertEquals(expected, Parse(" \"star wars\" \"\"\\"));
+ }
+
+ [Test]
+ public void TestGarbageSubquery()
+ {
+ Query expected = new TermQuery(new Term("field", "star"));
+
+ assertEquals(expected, Parse("(star)"));
+ assertEquals(expected, Parse("(star))"));
+ assertEquals(expected, Parse("((star)"));
+ assertEquals(expected, Parse(" -()(star) \n\n\r "));
+ assertEquals(expected, Parse("| + - ( + - | star \n ) \n"));
+ }
+
+ [Test]
+ public void TestCompoundAnd()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.MUST);
+ expected.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.MUST);
+ expected.Add(new TermQuery(new Term("field", "empire")), BooleanClause.Occur.MUST);
+
+ assertEquals(expected, Parse("star wars empire"));
+ assertEquals(expected, Parse("star+wars + empire"));
+ assertEquals(expected, Parse(" | --star wars empire \n\\"));
+ }
+
+ [Test]
+ public void TestCompoundOr()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.SHOULD);
+ expected.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.SHOULD);
+ expected.Add(new TermQuery(new Term("field", "empire")), BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("star|wars|empire"));
+ assertEquals(expected, Parse("star|wars | empire"));
+ assertEquals(expected, Parse(" | --star|wars|empire \n\\"));
+ }
+
+ [Test]
+ public void TestComplex00()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ BooleanQuery inner = new BooleanQuery();
+ inner.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.SHOULD);
+ inner.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.SHOULD);
+ expected.Add(inner, BooleanClause.Occur.MUST);
+ expected.Add(new TermQuery(new Term("field", "empire")), BooleanClause.Occur.MUST);
+
+ assertEquals(expected, Parse("star|wars empire"));
+ assertEquals(expected, Parse("star|wars + empire"));
+ assertEquals(expected, Parse("star| + wars + ----empire |"));
+ }
+
+ [Test]
+ public void TestComplex01()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ BooleanQuery inner = new BooleanQuery();
+ inner.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.MUST);
+ inner.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.MUST);
+ expected.Add(inner, BooleanClause.Occur.SHOULD);
+ expected.Add(new TermQuery(new Term("field", "empire")), BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("star wars | empire"));
+ assertEquals(expected, Parse("star + wars|empire"));
+ assertEquals(expected, Parse("star + | wars | ----empire +"));
+ }
+
+ [Test]
+ public void TestComplex02()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ BooleanQuery inner = new BooleanQuery();
+ inner.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.MUST);
+ inner.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.MUST);
+ expected.Add(inner, BooleanClause.Occur.SHOULD);
+ expected.Add(new TermQuery(new Term("field", "empire")), BooleanClause.Occur.SHOULD);
+ expected.Add(new TermQuery(new Term("field", "strikes")), BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("star wars | empire | strikes"));
+ assertEquals(expected, Parse("star + wars|empire | strikes"));
+ assertEquals(expected, Parse("star + | wars | ----empire | + --strikes \\"));
+ }
+
+ [Test]
+ public void TestComplex03()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ BooleanQuery inner = new BooleanQuery();
+ BooleanQuery inner2 = new BooleanQuery();
+ inner2.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.MUST);
+ inner2.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.MUST);
+ inner.Add(inner2, BooleanClause.Occur.SHOULD);
+ inner.Add(new TermQuery(new Term("field", "empire")), BooleanClause.Occur.SHOULD);
+ inner.Add(new TermQuery(new Term("field", "strikes")), BooleanClause.Occur.SHOULD);
+ expected.Add(inner, BooleanClause.Occur.MUST);
+ expected.Add(new TermQuery(new Term("field", "back")), BooleanClause.Occur.MUST);
+
+ assertEquals(expected, Parse("star wars | empire | strikes back"));
+ assertEquals(expected, Parse("star + wars|empire | strikes + back"));
+ assertEquals(expected, Parse("star + | wars | ----empire | + --strikes + | --back \\"));
+ }
+
+ [Test]
+ public void TestComplex04()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ BooleanQuery inner = new BooleanQuery();
+ BooleanQuery inner2 = new BooleanQuery();
+ inner.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.MUST);
+ inner.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.MUST);
+ inner2.Add(new TermQuery(new Term("field", "strikes")), BooleanClause.Occur.MUST);
+ inner2.Add(new TermQuery(new Term("field", "back")), BooleanClause.Occur.MUST);
+ expected.Add(inner, BooleanClause.Occur.SHOULD);
+ expected.Add(new TermQuery(new Term("field", "empire")), BooleanClause.Occur.SHOULD);
+ expected.Add(inner2, BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("(star wars) | empire | (strikes back)"));
+ assertEquals(expected, Parse("(star + wars) |empire | (strikes + back)"));
+ assertEquals(expected, Parse("(star + | wars |) | ----empire | + --(strikes + | --back) \\"));
+ }
+
+ [Test]
+ public void TestComplex05()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ BooleanQuery inner1 = new BooleanQuery();
+ BooleanQuery inner2 = new BooleanQuery();
+ BooleanQuery inner3 = new BooleanQuery();
+ BooleanQuery inner4 = new BooleanQuery();
+
+ expected.Add(inner1, BooleanClause.Occur.SHOULD);
+ expected.Add(inner2, BooleanClause.Occur.SHOULD);
+
+ inner1.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.MUST);
+ inner1.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.MUST);
+
+ inner2.Add(new TermQuery(new Term("field", "empire")), BooleanClause.Occur.SHOULD);
+ inner2.Add(inner3, BooleanClause.Occur.SHOULD);
+
+ inner3.Add(new TermQuery(new Term("field", "strikes")), BooleanClause.Occur.MUST);
+ inner3.Add(new TermQuery(new Term("field", "back")), BooleanClause.Occur.MUST);
+ inner3.Add(inner4, BooleanClause.Occur.MUST);
+
+ inner4.Add(new TermQuery(new Term("field", "jarjar")), BooleanClause.Occur.MUST_NOT);
+ inner4.Add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
+
+ assertEquals(expected, Parse("(star wars) | (empire | (strikes back -jarjar))"));
+ assertEquals(expected, Parse("(star + wars) |(empire | (strikes + back -jarjar) () )"));
+ assertEquals(expected, Parse("(star + | wars |) | --(--empire | + --(strikes + | --back + -jarjar) \"\" ) \""));
+ }
+
+ [Test]
+ public void TestComplex06()
+ {
+ BooleanQuery expected = new BooleanQuery();
+ BooleanQuery inner1 = new BooleanQuery();
+ BooleanQuery inner2 = new BooleanQuery();
+ BooleanQuery inner3 = new BooleanQuery();
+
+ expected.Add(new TermQuery(new Term("field", "star")), BooleanClause.Occur.MUST);
+ expected.Add(inner1, BooleanClause.Occur.MUST);
+
+ inner1.Add(new TermQuery(new Term("field", "wars")), BooleanClause.Occur.SHOULD);
+ inner1.Add(inner2, BooleanClause.Occur.SHOULD);
+
+ inner2.Add(inner3, BooleanClause.Occur.MUST);
+ inner3.Add(new TermQuery(new Term("field", "empire")), BooleanClause.Occur.SHOULD);
+ inner3.Add(new TermQuery(new Term("field", "strikes")), BooleanClause.Occur.SHOULD);
+ inner2.Add(new TermQuery(new Term("field", "back")), BooleanClause.Occur.MUST);
+ inner2.Add(new TermQuery(new Term("field", "jar+|jar")), BooleanClause.Occur.MUST);
+
+ assertEquals(expected, Parse("star (wars | (empire | strikes back jar\\+\\|jar))"));
+ assertEquals(expected, Parse("star + (wars |(empire | strikes + back jar\\+\\|jar) () )"));
+ assertEquals(expected, Parse("star + (| wars | | --(--empire | + --strikes + | --back + jar\\+\\|jar) \"\" ) \""));
+ }
+
+ /** test a term with field weights */
+ [Test]
+ public void TestWeightedTerm()
+ {
+ IDictionary<string, float> weights = new Dictionary<string, float>();
+ weights["field0"] = 5f;
+ weights["field1"] = 10f;
+
+ BooleanQuery expected = new BooleanQuery(true);
+ Query field0 = new TermQuery(new Term("field0", "foo"));
+ field0.Boost = (5f);
+ expected.Add(field0, BooleanClause.Occur.SHOULD);
+ Query field1 = new TermQuery(new Term("field1", "foo"));
+ field1.Boost = (10f);
+ expected.Add(field1, BooleanClause.Occur.SHOULD);
+
+ Analyzer analyzer = new MockAnalyzer(Random());
+ SimpleQueryParser parser = new SimpleQueryParser(analyzer, weights);
+ assertEquals(expected, parser.Parse("foo"));
+ }
+
+ /** test a more complex query with field weights */
+ [Test]
+ public void testWeightedOR()
+ {
+ IDictionary<string, float> weights = new Dictionary<string, float>();
+ weights["field0"] = 5f;
+ weights["field1"] = 10f;
+
+ BooleanQuery expected = new BooleanQuery();
+ BooleanQuery foo = new BooleanQuery(true);
+ Query field0 = new TermQuery(new Term("field0", "foo"));
+ field0.Boost = (5f);
+ foo.Add(field0, BooleanClause.Occur.SHOULD);
+ Query field1 = new TermQuery(new Term("field1", "foo"));
+ field1.Boost = (10f);
+ foo.Add(field1, BooleanClause.Occur.SHOULD);
+ expected.Add(foo, BooleanClause.Occur.SHOULD);
+
+ BooleanQuery bar = new BooleanQuery(true);
+ field0 = new TermQuery(new Term("field0", "bar"));
+ field0.Boost = (5f);
+ bar.Add(field0, BooleanClause.Occur.SHOULD);
+ field1 = new TermQuery(new Term("field1", "bar"));
+ field1.Boost = (10f);
+ bar.Add(field1, BooleanClause.Occur.SHOULD);
+ expected.Add(bar, BooleanClause.Occur.SHOULD);
+
+ Analyzer analyzer = new MockAnalyzer(Random());
+ SimpleQueryParser parser = new SimpleQueryParser(analyzer, weights);
+ assertEquals(expected, parser.Parse("foo|bar"));
+ }
+
+ /** helper to parse a query with keyword analyzer across "field" */
+ private Query ParseKeyword(string text, int flags)
+ {
+ Analyzer analyzer = new MockAnalyzer(Random(), MockTokenizer.KEYWORD, false);
+ SimpleQueryParser parser = new SimpleQueryParser(analyzer,
+ new HashMap<string, float>() { { "field", 1f } },
+ flags);
+ return parser.Parse(text);
+ }
+
+ /** test the ability to enable/disable phrase operator */
+ [Test]
+ public void TestDisablePhrase()
+ {
+ Query expected = new TermQuery(new Term("field", "\"test\""));
+ assertEquals(expected, ParseKeyword("\"test\"", SimpleQueryParser.PHRASE_OPERATOR));
+ }
+
+ /** test the ability to enable/disable prefix operator */
+ [Test]
+ public void TestDisablePrefix()
+ {
+ Query expected = new TermQuery(new Term("field", "test*"));
+ assertEquals(expected, ParseKeyword("test*", SimpleQueryParser.PREFIX_OPERATOR));
+ }
+
+ /** test the ability to enable/disable AND operator */
+ [Test]
+ public void TestDisableAND()
+ {
+ Query expected = new TermQuery(new Term("field", "foo+bar"));
+ assertEquals(expected, ParseKeyword("foo+bar", SimpleQueryParser.AND_OPERATOR));
+ expected = new TermQuery(new Term("field", "+foo+bar"));
+ assertEquals(expected, ParseKeyword("+foo+bar", SimpleQueryParser.AND_OPERATOR));
+ }
+
+ /** test the ability to enable/disable OR operator */
+ [Test]
+ public void TestDisableOR()
+ {
+ Query expected = new TermQuery(new Term("field", "foo|bar"));
+ assertEquals(expected, ParseKeyword("foo|bar", SimpleQueryParser.OR_OPERATOR));
+ expected = new TermQuery(new Term("field", "|foo|bar"));
+ assertEquals(expected, ParseKeyword("|foo|bar", SimpleQueryParser.OR_OPERATOR));
+ }
+
+ /** test the ability to enable/disable NOT operator */
+ [Test]
+ public void TestDisableNOT()
+ {
+ Query expected = new TermQuery(new Term("field", "-foo"));
+ assertEquals(expected, ParseKeyword("-foo", SimpleQueryParser.NOT_OPERATOR));
+ }
+
+ /** test the ability to enable/disable precedence operators */
+ [Test]
+ public void TestDisablePrecedence()
+ {
+ Query expected = new TermQuery(new Term("field", "(foo)"));
+ assertEquals(expected, ParseKeyword("(foo)", SimpleQueryParser.PRECEDENCE_OPERATORS));
+ expected = new TermQuery(new Term("field", ")foo("));
+ assertEquals(expected, ParseKeyword(")foo(", SimpleQueryParser.PRECEDENCE_OPERATORS));
+ }
+
+ /** test the ability to enable/disable escape operators */
+ [Test]
+ public void TestDisableEscape()
+ {
+ Query expected = new TermQuery(new Term("field", "foo\\bar"));
+ assertEquals(expected, ParseKeyword("foo\\bar", SimpleQueryParser.ESCAPE_OPERATOR));
+ assertEquals(expected, ParseKeyword("(foo\\bar)", SimpleQueryParser.ESCAPE_OPERATOR));
+ assertEquals(expected, ParseKeyword("\"foo\\bar\"", SimpleQueryParser.ESCAPE_OPERATOR));
+ }
+
+ [Test]
+ public void TestDisableWhitespace()
+ {
+ Query expected = new TermQuery(new Term("field", "foo foo"));
+ assertEquals(expected, ParseKeyword("foo foo", SimpleQueryParser.WHITESPACE_OPERATOR));
+ expected = new TermQuery(new Term("field", " foo foo\n "));
+ assertEquals(expected, ParseKeyword(" foo foo\n ", SimpleQueryParser.WHITESPACE_OPERATOR));
+ expected = new TermQuery(new Term("field", "\t\tfoo foo foo"));
+ assertEquals(expected, ParseKeyword("\t\tfoo foo foo", SimpleQueryParser.WHITESPACE_OPERATOR));
+ }
+
+ [Test]
+ public void TestDisableFuzziness()
+ {
+ Query expected = new TermQuery(new Term("field", "foo~1"));
+ assertEquals(expected, ParseKeyword("foo~1", SimpleQueryParser.FUZZY_OPERATOR));
+ }
+
+ [Test]
+ public void TestDisableSlop()
+ {
+ PhraseQuery expectedPhrase = new PhraseQuery();
+ expectedPhrase.Add(new Term("field", "foo"));
+ expectedPhrase.Add(new Term("field", "bar"));
+
+ BooleanQuery expected = new BooleanQuery();
+ expected.Add(expectedPhrase, BooleanClause.Occur.MUST);
+ expected.Add(new TermQuery(new Term("field", "~2")), BooleanClause.Occur.MUST);
+ assertEquals(expected, Parse("\"foo bar\"~2", SimpleQueryParser.NEAR_OPERATOR));
+ }
+
+ // we aren't supposed to barf on any input...
+ [Test]
+ public void TestRandomQueries()
+ {
+ for (int i = 0; i < 1000; i++)
+ {
+ string query = TestUtil.RandomUnicodeString(Random());
+ Parse(query); // no exception
+ ParseKeyword(query, TestUtil.NextInt(Random(), 0, 1024)); // no exception
+ }
+ }
+
+ [Test]
+ public void testRandomQueries2()
+ {
+ char[] chars = new char[] { 'a', '1', '|', '&', ' ', '(', ')', '"', '-', '~' };
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 1000; i++)
+ {
+ sb.Length = (0);
+ int queryLength = Random().Next(20);
+ for (int j = 0; j < queryLength; j++)
+ {
+ sb.append(chars[Random().Next(chars.Length)]);
+ }
+ Parse(sb.toString()); // no exception
+ ParseKeyword(sb.toString(), TestUtil.NextInt(Random(), 0, 1024)); // no exception
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/679ad24c/src/Lucene.Net.Tests.QueryParser/Surround/Query/BooleanQueryTst.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Surround/Query/BooleanQueryTst.cs b/src/Lucene.Net.Tests.QueryParser/Surround/Query/BooleanQueryTst.cs
new file mode 100644
index 0000000..6f7fcfc
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Surround/Query/BooleanQueryTst.cs
@@ -0,0 +1,142 @@
+\ufeffusing Lucene.Net.Index;
+using Lucene.Net.Search;
+using NUnit.Framework;
+using System;
+
+namespace Lucene.Net.QueryParser.Surround.Query
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ public class BooleanQueryTst
+ {
+ private string queryText;
+ private readonly int[] expectedDocNrs;
+ private SingleFieldTestDb dBase;
+ private string fieldName;
+ private Assert testCase;
+ private BasicQueryFactory qf;
+ private bool verbose = true;
+
+ public BooleanQueryTst(
+ string queryText,
+ int[] expectedDocNrs,
+ SingleFieldTestDb dBase,
+ string fieldName,
+ Assert testCase,
+ BasicQueryFactory qf)
+ {
+ this.queryText = queryText;
+ this.expectedDocNrs = expectedDocNrs;
+ this.dBase = dBase;
+ this.fieldName = fieldName;
+ this.testCase = testCase;
+ this.qf = qf;
+ }
+
+ public virtual bool Verbose { set { this.verbose = value; } }
+
+ public virtual string QueryText { get { return this.queryText; } }
+
+ public virtual int[] ExpectedDocNrs { get { return this.expectedDocNrs; } }
+
+ internal class TestCollector : Collector
+ { // FIXME: use check hits from Lucene tests
+ private int totalMatched;
+ private bool[] encountered;
+ private Scorer scorer = null;
+ private int docBase = 0;
+ private BooleanQueryTst parent;
+
+ public TestCollector(BooleanQueryTst parent)
+ {
+ totalMatched = 0;
+ encountered = new bool[parent.expectedDocNrs.Length];
+ this.parent = parent;
+ }
+
+ public override Scorer Scorer
+ {
+ set { this.scorer = value; }
+ }
+
+ public override bool AcceptsDocsOutOfOrder()
+ {
+ return true;
+ }
+
+ public override AtomicReaderContext NextReader
+ {
+ set { docBase = value.DocBase; }
+ }
+
+ public override void Collect(int docNr)
+ {
+ float score = scorer.Score();
+ docNr += docBase;
+ /* System.out.println(docNr + " '" + dBase.getDocs()[docNr] + "': " + score); */
+ Assert.True(score > 0.0, parent.QueryText + ": positive score");
+ Assert.True(totalMatched < parent.ExpectedDocNrs.Length, parent.QueryText + ": too many hits");
+ int i;
+ for (i = 0; i < parent.expectedDocNrs.Length; i++)
+ {
+ if ((!encountered[i]) && (parent.ExpectedDocNrs[i] == docNr))
+ {
+ encountered[i] = true;
+ break;
+ }
+ }
+ if (i == parent.ExpectedDocNrs.Length)
+ {
+ Assert.True(false, parent.QueryText + ": doc nr for hit not expected: " + docNr);
+ }
+ totalMatched++;
+ }
+
+ public void CheckNrHits()
+ {
+ Assert.AreEqual(parent.ExpectedDocNrs.Length, totalMatched, parent.QueryText + ": nr of hits");
+ }
+ }
+
+ public void DoTest()
+ {
+
+ if (verbose)
+ {
+ Console.WriteLine("");
+ Console.WriteLine("Query: " + queryText);
+ }
+
+ SrndQuery lq = Parser.QueryParser.Parse(queryText);
+
+ /* if (verbose) System.out.println("Srnd: " + lq.toString()); */
+
+ Search.Query query = lq.MakeLuceneQueryField(fieldName, qf);
+ /* if (verbose) System.out.println("Lucene: " + query.toString()); */
+
+ TestCollector tc = new TestCollector(this);
+ using (IndexReader reader = DirectoryReader.Open(dBase.Db))
+ {
+ IndexSearcher searcher = new IndexSearcher(reader);
+
+ searcher.Search(query, tc);
+ }
+ tc.CheckNrHits();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/679ad24c/src/Lucene.Net.Tests.QueryParser/Surround/Query/ExceptionQueryTst.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Surround/Query/ExceptionQueryTst.cs b/src/Lucene.Net.Tests.QueryParser/Surround/Query/ExceptionQueryTst.cs
new file mode 100644
index 0000000..7468ef9
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Surround/Query/ExceptionQueryTst.cs
@@ -0,0 +1,76 @@
+\ufeffusing Lucene.Net.QueryParser.Surround.Parser;
+using System;
+using System.Text;
+
+namespace Lucene.Net.QueryParser.Surround.Query
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ public class ExceptionQueryTst
+ {
+ private string queryText;
+ private bool verbose;
+
+ public ExceptionQueryTst(string queryText, bool verbose)
+ {
+ this.queryText = queryText;
+ this.verbose = verbose;
+ }
+
+ public void DoTest(StringBuilder failQueries)
+ {
+ bool pass = false;
+ SrndQuery lq = null;
+ try
+ {
+ lq = Parser.QueryParser.Parse(queryText);
+ if (verbose)
+ {
+ Console.WriteLine("Query: " + queryText + "\nParsed as: " + lq.ToString());
+ }
+ }
+ catch (ParseException e)
+ {
+ if (verbose)
+ {
+ Console.WriteLine("Parse exception for query:\n"
+ + queryText + "\n"
+ + e.Message);
+ }
+ pass = true;
+ }
+ if (!pass)
+ {
+ failQueries.append(queryText);
+ failQueries.append("\nParsed as: ");
+ failQueries.append(lq.toString());
+ failQueries.append("\n");
+ }
+ }
+
+ public static string GetFailQueries(string[] exceptionQueries, bool verbose)
+ {
+ StringBuilder failQueries = new StringBuilder();
+ for (int i = 0; i < exceptionQueries.Length; i++)
+ {
+ new ExceptionQueryTst(exceptionQueries[i], verbose).DoTest(failQueries);
+ }
+ return failQueries.toString();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/679ad24c/src/Lucene.Net.Tests.QueryParser/Surround/Query/SingleFieldTestDb.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Surround/Query/SingleFieldTestDb.cs b/src/Lucene.Net.Tests.QueryParser/Surround/Query/SingleFieldTestDb.cs
new file mode 100644
index 0000000..1221835
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Surround/Query/SingleFieldTestDb.cs
@@ -0,0 +1,55 @@
+\ufeffusing Lucene.Net.Analysis;
+using Lucene.Net.Documents;
+using Lucene.Net.Index;
+using Lucene.Net.Store;
+using Lucene.Net.Util;
+using System;
+
+namespace Lucene.Net.QueryParser.Surround.Query
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ public class SingleFieldTestDb
+ {
+ private Directory db;
+ private string[] docs;
+ private string fieldName;
+
+ public SingleFieldTestDb(Random random, string[] documents, string fName)
+ {
+ db = new MockDirectoryWrapper(random, new RAMDirectory());
+ docs = documents;
+ fieldName = fName;
+ using (IndexWriter writer = new IndexWriter(db, new IndexWriterConfig(
+ LuceneVersion.LUCENE_CURRENT,
+ new MockAnalyzer(random))))
+ {
+ for (int j = 0; j < docs.Length; j++)
+ {
+ Document d = new Document();
+ d.Add(new TextField(fieldName, docs[j], Field.Store.NO));
+ writer.AddDocument(d);
+ }
+ }
+ }
+
+ public Directory Db { get { return db; } }
+ public string[] Docs { get { return docs; } }
+ public string Fieldname { get { return fieldName; } }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/679ad24c/src/Lucene.Net.Tests.QueryParser/Surround/Query/SrndQueryTest.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Surround/Query/SrndQueryTest.cs b/src/Lucene.Net.Tests.QueryParser/Surround/Query/SrndQueryTest.cs
new file mode 100644
index 0000000..ebe7e2b
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Surround/Query/SrndQueryTest.cs
@@ -0,0 +1,48 @@
+\ufeffusing Lucene.Net.Search;
+using Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.QueryParser.Surround.Query
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ [TestFixture]
+ public class SrndQueryTest : LuceneTestCase
+ {
+ private void CheckEqualParsings(string s1, string s2)
+ {
+ string fieldName = "foo";
+ BasicQueryFactory qf = new BasicQueryFactory(16);
+ Search.Query lq1, lq2;
+ lq1 = Parser.QueryParser.Parse(s1).MakeLuceneQueryField(fieldName, qf);
+ lq2 = Parser.QueryParser.Parse(s2).MakeLuceneQueryField(fieldName, qf);
+ QueryUtils.CheckEqual(lq1, lq2);
+ }
+
+ [Test]
+ public void TestHashEquals()
+ {
+ //grab some sample queries from Test02Boolean and Test03Distance and
+ //check there hashes and equals
+ CheckEqualParsings("word1 w word2", " word1 w word2 ");
+ CheckEqualParsings("2N(w1,w2,w3)", " 2N(w1, w2 , w3)");
+ CheckEqualParsings("abc?", " abc? ");
+ CheckEqualParsings("w*rd?", " w*rd?");
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/679ad24c/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test01Exceptions.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test01Exceptions.cs b/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test01Exceptions.cs
new file mode 100644
index 0000000..6ebc87a
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test01Exceptions.cs
@@ -0,0 +1,72 @@
+\ufeffusing Lucene.Net.Util;
+using NUnit.Framework;
+
+namespace Lucene.Net.QueryParser.Surround.Query
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ [TestFixture]
+ public class Test01Exceptions_ : LuceneTestCase
+ {
+ /** Main for running test case by itself. */
+ //public static void Main(string[] args)
+ //{
+ // TestRunner.run(new TestSuite(Test01Exceptions.class));
+ //}
+
+ private bool verbose = false; /* to show actual parsing error messages */
+ private readonly string fieldName = "bi";
+
+ string[] exceptionQueries = {
+ "*",
+ "a*",
+ "ab*",
+ "?",
+ "a?",
+ "ab?",
+ "a???b",
+ "a?",
+ "a*b?",
+ "word1 word2",
+ "word2 AND",
+ "word1 OR",
+ "AND(word2)",
+ "AND(word2,)",
+ "AND(word2,word1,)",
+ "OR(word2)",
+ "OR(word2 ,",
+ "OR(word2 , word1 ,)",
+ "xx NOT",
+ "xx (a AND b)",
+ "(a AND b",
+ "a OR b)",
+ "or(word2+ not ord+, and xyz,def)",
+ ""
+ };
+
+ [Test]
+ public void Test01Exceptions()
+ {
+ string m = ExceptionQueryTst.GetFailQueries(exceptionQueries, verbose);
+ if (m.Length > 0)
+ {
+ fail("No ParseException for:\n" + m);
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/679ad24c/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test02Boolean.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test02Boolean.cs b/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test02Boolean.cs
new file mode 100644
index 0000000..aef9279
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test02Boolean.cs
@@ -0,0 +1,178 @@
+\ufeffusing Lucene.Net.Util;
+using NUnit.Framework;
+using System;
+
+namespace Lucene.Net.QueryParser.Surround.Query
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ [TestFixture]
+ public class Test02Boolean : LuceneTestCase
+ {
+ //public static void Main(string[] args) {
+ // TestRunner.run(new TestSuite(Test02Boolean.class));
+ //}
+
+ private readonly string fieldName = "bi";
+ private bool verbose = false;
+ private int maxBasicQueries = 16;
+
+ string[] docs1 = {
+ "word1 word2 word3",
+ "word4 word5",
+ "ord1 ord2 ord3",
+ "orda1 orda2 orda3 word2 worda3",
+ "a c e a b c"
+ };
+
+ public override void SetUp()
+ {
+ base.SetUp();
+ db1 = new SingleFieldTestDb(Random(), docs1, fieldName);
+ }
+
+ private SingleFieldTestDb db1;
+
+
+ public void NormalTest1(String query, int[] expdnrs)
+ {
+ BooleanQueryTst bqt = new BooleanQueryTst(query, expdnrs, db1, fieldName, this,
+ new BasicQueryFactory(maxBasicQueries));
+ bqt.Verbose = (verbose);
+ bqt.DoTest();
+ }
+
+ [Test]
+ public void Test02Terms01()
+ {
+ int[] expdnrs = { 0 }; NormalTest1("word1", expdnrs);
+ }
+ [Test]
+ public void Test02Terms02()
+ {
+ int[] expdnrs = { 0, 1, 3 }; NormalTest1("word*", expdnrs);
+ }
+ [Test]
+ public void Test02Terms03()
+ {
+ int[] expdnrs = { 2 }; NormalTest1("ord2", expdnrs);
+ }
+ [Test]
+ public void Test02Terms04()
+ {
+ int[] expdnrs = { }; NormalTest1("kxork*", expdnrs);
+ }
+ [Test]
+ public void Test02Terms05()
+ {
+ int[] expdnrs = { 0, 1, 3 }; NormalTest1("wor*", expdnrs);
+ }
+ [Test]
+ public void Test02Terms06()
+ {
+ int[] expdnrs = { }; NormalTest1("ab", expdnrs);
+ }
+
+ [Test]
+ public void Test02Terms10()
+ {
+ int[] expdnrs = { }; NormalTest1("abc?", expdnrs);
+ }
+ [Test]
+ public void Test02Terms13()
+ {
+ int[] expdnrs = { 0, 1, 3 }; NormalTest1("word?", expdnrs);
+ }
+ [Test]
+ public void Test02Terms14()
+ {
+ int[] expdnrs = { 0, 1, 3 }; NormalTest1("w?rd?", expdnrs);
+ }
+ [Test]
+ public void Test02Terms20()
+ {
+ int[] expdnrs = { 0, 1, 3 }; NormalTest1("w*rd?", expdnrs);
+ }
+ [Test]
+ public void Test02Terms21()
+ {
+ int[] expdnrs = { 3 }; NormalTest1("w*rd??", expdnrs);
+ }
+ [Test]
+ public void Test02Terms22()
+ {
+ int[] expdnrs = { 3 }; NormalTest1("w*?da?", expdnrs);
+ }
+ [Test]
+ public void Test02Terms23()
+ {
+ int[] expdnrs = { }; NormalTest1("w?da?", expdnrs);
+ }
+
+ [Test]
+ public void Test03And01()
+ {
+ int[] expdnrs = { 0 }; NormalTest1("word1 AND word2", expdnrs);
+ }
+ [Test]
+ public void Test03And02()
+ {
+ int[] expdnrs = { 3 }; NormalTest1("word* and ord*", expdnrs);
+ }
+ [Test]
+ public void Test03And03()
+ {
+ int[] expdnrs = { 0 }; NormalTest1("and(word1,word2)", expdnrs);
+ }
+ [Test]
+ public void Test04Or01()
+ {
+ int[] expdnrs = { 0, 3 }; NormalTest1("word1 or word2", expdnrs);
+ }
+ [Test]
+ public void Test04Or02()
+ {
+ int[] expdnrs = { 0, 1, 2, 3 }; NormalTest1("word* OR ord*", expdnrs);
+ }
+ [Test]
+ public void Test04Or03()
+ {
+ int[] expdnrs = { 0, 3 }; NormalTest1("OR (word1, word2)", expdnrs);
+ }
+ [Test]
+ public void Test05Not01()
+ {
+ int[] expdnrs = { 3 }; NormalTest1("word2 NOT word1", expdnrs);
+ }
+ [Test]
+ public void Test05Not02()
+ {
+ int[] expdnrs = { 0 }; NormalTest1("word2* not ord*", expdnrs);
+ }
+ [Test]
+ public void Test06AndOr01()
+ {
+ int[] expdnrs = { 0 }; NormalTest1("(word1 or ab)and or(word2,xyz, defg)", expdnrs);
+ }
+ [Test]
+ public void Test07AndOrNot02()
+ {
+ int[] expdnrs = { 0 }; NormalTest1("or( word2* not ord*, and(xyz,def))", expdnrs);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/lucenenet/blob/679ad24c/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test03Distance.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test03Distance.cs b/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test03Distance.cs
new file mode 100644
index 0000000..6a19cb7
--- /dev/null
+++ b/src/Lucene.Net.Tests.QueryParser/Surround/Query/Test03Distance.cs
@@ -0,0 +1,341 @@
+\ufeffusing Lucene.Net.Util;
+using NUnit.Framework;
+using System;
+
+namespace Lucene.Net.QueryParser.Surround.Query
+{
+ /*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+ [TestFixture]
+ public class Test03Distance : LuceneTestCase
+ {
+ //public static void Main(string[] args) {
+ // TestRunner.run(new TestSuite(Test03Distance.class));
+ //}
+
+ private bool verbose = false;
+ private int maxBasicQueries = 16;
+
+ private string[] exceptionQueries = {
+ "(aa and bb) w cc",
+ "(aa or bb) w (cc and dd)",
+ "(aa opt bb) w cc",
+ "(aa not bb) w cc",
+ "(aa or bb) w (bi:cc)",
+ "(aa or bb) w bi:cc",
+ "(aa or bi:bb) w cc",
+ "(aa or (bi:bb)) w cc",
+ "(aa or (bb and dd)) w cc"
+ };
+
+ [Test]
+ public void Test00Exceptions()
+ {
+ string m = ExceptionQueryTst.GetFailQueries(exceptionQueries, verbose);
+ if (m.Length > 0)
+ {
+ fail("No ParseException for:\n" + m);
+ }
+ }
+
+ private readonly string fieldName = "bi";
+
+ private string[] docs1 = {
+ "word1 word2 word3",
+ "word4 word5",
+ "ord1 ord2 ord3",
+ "orda1 orda2 orda3 word2 worda3",
+ "a c e a b c"
+ };
+
+ SingleFieldTestDb db1;
+
+ public override void SetUp()
+ {
+ base.SetUp();
+ db1 = new SingleFieldTestDb(Random(), docs1, fieldName);
+ db2 = new SingleFieldTestDb(Random(), docs2, fieldName);
+ db3 = new SingleFieldTestDb(Random(), docs3, fieldName);
+ }
+
+ private void DistanceTst(String query, int[] expdnrs, SingleFieldTestDb db)
+ {
+ BooleanQueryTst bqt = new BooleanQueryTst(query, expdnrs, db, fieldName, this,
+ new BasicQueryFactory(maxBasicQueries));
+ bqt.Verbose = (verbose);
+ bqt.DoTest();
+ }
+
+ public void DistanceTest1(string query, int[] expdnrs)
+ {
+ DistanceTst(query, expdnrs, db1);
+ }
+
+ [Test]
+ public void Test0W01()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word1 w word2", expdnrs);
+ }
+ [Test]
+ public void Test0N01()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word1 n word2", expdnrs);
+ }
+ [Test]
+ public void Test0N01r()
+ { /* r reverse */
+ int[] expdnrs = { 0 }; DistanceTest1("word2 n word1", expdnrs);
+ }
+ [Test]
+ public void Test0W02()
+ {
+ int[] expdnrs = { }; DistanceTest1("word2 w word1", expdnrs);
+ }
+ [Test]
+ public void Test0W03()
+ {
+ int[] expdnrs = { }; DistanceTest1("word2 2W word1", expdnrs);
+ }
+ [Test]
+ public void Test0N03()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word2 2N word1", expdnrs);
+ }
+ [Test]
+ public void Test0N03r()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word1 2N word2", expdnrs);
+ }
+
+ [Test]
+ public void Test0W04()
+ {
+ int[] expdnrs = { }; DistanceTest1("word2 3w word1", expdnrs);
+ }
+
+ [Test]
+ public void Test0N04()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word2 3n word1", expdnrs);
+ }
+ [Test]
+ public void Test0N04r()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word1 3n word2", expdnrs);
+ }
+
+ [Test]
+ public void Test0W05()
+ {
+ int[] expdnrs = { }; DistanceTest1("orda1 w orda3", expdnrs);
+ }
+ [Test]
+ public void Test0W06()
+ {
+ int[] expdnrs = { 3 }; DistanceTest1("orda1 2w orda3", expdnrs);
+ }
+
+ [Test]
+ public void Test1Wtrunc01()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word1* w word2", expdnrs);
+ }
+ [Test]
+ public void Test1Wtrunc02()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word* w word2", expdnrs);
+ }
+ [Test]
+ public void Test1Wtrunc02r()
+ {
+ int[] expdnrs = { 0, 3 }; DistanceTest1("word2 w word*", expdnrs);
+ }
+ [Test]
+ public void Test1Ntrunc02()
+ {
+ int[] expdnrs = { 0, 3 }; DistanceTest1("word* n word2", expdnrs);
+ }
+ [Test]
+ public void Test1Ntrunc02r()
+ {
+ int[] expdnrs = { 0, 3 }; DistanceTest1("word2 n word*", expdnrs);
+ }
+
+ [Test]
+ public void Test1Wtrunc03()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word1* w word2*", expdnrs);
+ }
+ [Test]
+ public void Test1Ntrunc03()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word1* N word2*", expdnrs);
+ }
+
+ [Test]
+ public void Test1Wtrunc04()
+ {
+ int[] expdnrs = { }; DistanceTest1("kxork* w kxor*", expdnrs);
+ }
+ [Test]
+ public void Test1Ntrunc04()
+ {
+ int[] expdnrs = { }; DistanceTest1("kxork* 99n kxor*", expdnrs);
+ }
+
+ [Test]
+ public void Test1Wtrunc05()
+ {
+ int[] expdnrs = { }; DistanceTest1("word2* 2W word1*", expdnrs);
+ }
+ [Test]
+ public void Test1Ntrunc05()
+ {
+ int[] expdnrs = { 0 }; DistanceTest1("word2* 2N word1*", expdnrs);
+ }
+
+ [Test]
+ public void Test1Wtrunc06()
+ {
+ int[] expdnrs = { 3 }; DistanceTest1("ord* W word*", expdnrs);
+ }
+ [Test]
+ public void Test1Ntrunc06()
+ {
+ int[] expdnrs = { 3 }; DistanceTest1("ord* N word*", expdnrs);
+ }
+ [Test]
+ public void Test1Ntrunc06r()
+ {
+ int[] expdnrs = { 3 }; DistanceTest1("word* N ord*", expdnrs);
+ }
+
+ [Test]
+ public void Test1Wtrunc07()
+ {
+ int[] expdnrs = { 3 }; DistanceTest1("(orda2 OR orda3) W word*", expdnrs);
+ }
+ [Test]
+ public void Test1Wtrunc08()
+ {
+ int[] expdnrs = { 3 }; DistanceTest1("(orda2 OR orda3) W (word2 OR worda3)", expdnrs);
+ }
+ [Test]
+ public void Test1Wtrunc09()
+ {
+ int[] expdnrs = { 3 }; DistanceTest1("(orda2 OR orda3) 2W (word2 OR worda3)", expdnrs);
+ }
+ [Test]
+ public void Test1Ntrunc09()
+ {
+ int[] expdnrs = { 3 }; DistanceTest1("(orda2 OR orda3) 2N (word2 OR worda3)", expdnrs);
+ }
+
+ string[] docs2 = {
+ "w1 w2 w3 w4 w5",
+ "w1 w3 w2 w3",
+ ""
+ };
+
+ SingleFieldTestDb db2;
+
+ public void DistanceTest2(string query, int[] expdnrs)
+ {
+ DistanceTst(query, expdnrs, db2);
+ }
+
+ [Test]
+ public void Test2Wprefix01()
+ {
+ int[] expdnrs = { 0 }; DistanceTest2("W (w1, w2, w3)", expdnrs);
+ }
+ [Test]
+ public void Test2Nprefix01a()
+ {
+ int[] expdnrs = { 0, 1 }; DistanceTest2("N(w1, w2, w3)", expdnrs);
+ }
+ [Test]
+ public void Test2Nprefix01b()
+ {
+ int[] expdnrs = { 0, 1 }; DistanceTest2("N(w3, w1, w2)", expdnrs);
+ }
+
+ [Test]
+ public void Test2Wprefix02()
+ {
+ int[] expdnrs = { 0, 1 }; DistanceTest2("2W(w1,w2,w3)", expdnrs);
+ }
+
+ [Test]
+ public void Test2Nprefix02a()
+ {
+ int[] expdnrs = { 0, 1 }; DistanceTest2("2N(w1,w2,w3)", expdnrs);
+ }
+ [Test]
+ public void Test2Nprefix02b()
+ {
+ int[] expdnrs = { 0, 1 }; DistanceTest2("2N(w2,w3,w1)", expdnrs);
+ }
+
+ [Test]
+ public void Test2Wnested01()
+ {
+ int[] expdnrs = { 0 }; DistanceTest2("w1 W w2 W w3", expdnrs);
+ }
+ [Test]
+ public void Test2Nnested01()
+ {
+ int[] expdnrs = { 0 }; DistanceTest2("w1 N w2 N w3", expdnrs);
+ }
+
+ [Test]
+ public void Test2Wnested02()
+ {
+ int[] expdnrs = { 0, 1 }; DistanceTest2("w1 2W w2 2W w3", expdnrs);
+ }
+ [Test]
+ public void Test2Nnested02()
+ {
+ int[] expdnrs = { 0, 1 }; DistanceTest2("w1 2N w2 2N w3", expdnrs);
+ }
+
+ string[] docs3 = {
+ "low pressure temperature inversion and rain",
+ "when the temperature has a negative height above a depression no precipitation gradient is expected",
+ "when the temperature has a negative height gradient above a depression no precipitation is expected",
+ ""
+ };
+
+ SingleFieldTestDb db3;
+
+ public void DistanceTest3(string query, int[] expdnrs)
+ {
+ DistanceTst(query, expdnrs, db3);
+ }
+
+ [Test]
+ public void Test3Example01()
+ {
+ int[] expdnrs = { 0, 2 }; // query does not match doc 1 because "gradient" is in wrong place there.
+ DistanceTest3("50n((low w pressure*) or depression*,"
+ + "5n(temperat*, (invers* or (negativ* 3n gradient*))),"
+ + "rain* or precipitat*)",
+ expdnrs);
+ }
+ }
+}