Added some failing rewrite tests

Change-Id: I39ac7419ea196c42028c1db11558572e5ecef14c
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanClassQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanClassQuery.java
index 3c25751..2d56033 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanClassQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanClassQuery.java
@@ -9,7 +9,6 @@
 import org.apache.lucene.index.TermContext;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.spans.SpanQuery;
-import org.apache.lucene.search.spans.SpanTermQuery;
 import org.apache.lucene.search.spans.Spans;
 import org.apache.lucene.util.Bits;
 import org.apache.lucene.util.ToStringUtils;
@@ -62,11 +61,8 @@
 			// Rewritten spanquery is empty
 			if (query.getField() == null) {
 
-				// Return an artificially created null-query
-				// I guess there's a better way, but who knows
-				return new SpanTermQuery(
-					new Term(this.firstClause.getField(), "")
-					);
+				// Return the empty child query
+				return query;
 			};
 
 			SpanClassQuery clone = this.clone();
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestRegexWildcardIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestRegexWildcardIndex.java
index 46580ee..7ff35b2 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestRegexWildcardIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestRegexWildcardIndex.java
@@ -2,6 +2,9 @@
 
 import static org.junit.Assert.assertEquals;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.apache.lucene.search.spans.SpanQuery;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -12,6 +15,9 @@
 import de.ids_mannheim.korap.KrillIndex;
 import de.ids_mannheim.korap.query.QueryBuilder;
 import de.ids_mannheim.korap.response.Result;
+import de.ids_mannheim.korap.query.DistanceConstraint;
+import de.ids_mannheim.korap.query.SpanMultipleDistanceQuery;
+
 
 @RunWith(JUnit4.class)
 public class TestRegexWildcardIndex {
@@ -315,6 +321,111 @@
         assertEquals((long) 1, kr.getTotalResults());
         assertEquals("[[affe afffe]] baum ...",
                 kr.getMatch(0).getSnippetBrackets());
+
+
+		// Test without matches in sequence
+		sq = kq.seq(kq.re("s:z.*e")).append(kq.seg("s:affe")).toQuery();
+        assertEquals(
+			"spanNext(SpanMultiTermQueryWrapper(base:/s:z.*e/), base:s:affe)",
+			sq.toString());
+        kr = ki.search(new Krill(sq));
+        assertEquals((long) 0, kr.getTotalResults());
+
+		// Test without matches in segment
+		sq = kq.seg().with(kq.re("s:z.*e")).with("s:affe").toQuery();
+        assertEquals(
+			"spanSegment(SpanMultiTermQueryWrapper(base:/s:z.*e/), base:s:affe)",
+			sq.toString());
+        kr = ki.search(new Krill(sq));
+        assertEquals((long) 0, kr.getTotalResults());
+
+		// Test without matches in or
+		sq = kq.or(kq.re("s:z.*e"), kq.seg("s:affe")).toQuery();
+        assertEquals(
+			"spanOr([SpanMultiTermQueryWrapper(base:/s:z.*e/), base:s:affe])",
+			sq.toString());
+        kr = ki.search(new Krill(sq));
+        assertEquals((long) 1, kr.getTotalResults());
+
+		// Test without matches in within
+		sq = kq.within(kq.re("s:z.*e"), kq.seg("s:affe")).toQuery();
+        assertEquals(
+			"spanContain(SpanMultiTermQueryWrapper(base:/s:z.*e/), base:s:affe)",
+			sq.toString());
+        kr = ki.search(new Krill(sq));
+        assertEquals((long) 0, kr.getTotalResults());
+
+		// Test without matches in within (reversed)
+		sq = kq.within(kq.seg("s:affe"), kq.re("s:z.*e")).toQuery();
+        assertEquals(
+			"spanContain(base:s:affe, SpanMultiTermQueryWrapper(base:/s:z.*e/))",
+			sq.toString());
+        kr = ki.search(new Krill(sq));
+        assertEquals((long) 0, kr.getTotalResults());
+
+		// Test with classes
+		sq = kq._(kq.re("s:z.*e")).toQuery();
+        assertEquals(
+			"{1: SpanMultiTermQueryWrapper(base:/s:z.*e/)}",
+			sq.toString());
+        kr = ki.search(new Krill(sq));
+        assertEquals((long) 0, kr.getTotalResults());
+
+		// Test with nested classes
+		sq = kq.within(kq._(kq.re("s:z.*e")), kq.seg("s:affe")).toQuery();
+        assertEquals(
+			"spanContain({1: SpanMultiTermQueryWrapper(base:/s:z.*e/)}, base:s:affe)",
+			sq.toString());
+        kr = ki.search(new Krill(sq));
+        assertEquals((long) 0, kr.getTotalResults());
+
+		// Test with multiple distances
+		List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
+        constraints.add(
+			TestMultipleDistanceIndex.createConstraint(
+				"w",
+				1,
+				2,
+				true,
+				false
+				)
+			);
+        constraints.add(
+			TestMultipleDistanceIndex.createConstraint(
+				"tokens",
+				"base/s:s",
+				0,
+				0,
+				true,
+				false
+				)
+			);
+		sq = new SpanMultipleDistanceQuery(
+			kq.re("s:z.*e").toQuery(),
+			kq.seg("s:affe").toQuery(),
+			constraints,
+			true,
+			true
+			);
+        assertEquals(
+			"spanMultipleDistance(SpanMultiTermQueryWrapper(base:/s:z.*e/), base:s:affe, [(w[1:2], ordered, notExcluded), (base/s:s[0:0], ordered, notExcluded)])",
+			sq.toString());
+        kr = ki.search(new Krill(sq));
+        assertEquals((long) 0, kr.getTotalResults());
+
+		// Test with multiple distances and a class
+		sq = new SpanMultipleDistanceQuery(
+			kq._(kq.re("s:z.*e")).toQuery(),
+			kq.seg("s:affe").toQuery(),
+			constraints,
+			true,
+			true
+			);
+        assertEquals(
+			"spanMultipleDistance({1: SpanMultiTermQueryWrapper(base:/s:z.*e/)}, base:s:affe, [(w[1:2], ordered, notExcluded), (base/s:s[0:0], ordered, notExcluded)])",
+			sq.toString());
+        kr = ki.search(new Krill(sq));
+        assertEquals((long) 0, kr.getTotalResults());
     };
 
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestSampleIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestSampleIndex.java
index bbbb955..f7518cc 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestSampleIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestSampleIndex.java
@@ -14,6 +14,7 @@
 import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
 import org.apache.lucene.search.spans.SpanQuery;
 import org.apache.lucene.search.spans.SpanOrQuery;
+import org.apache.lucene.search.spans.SpanNearQuery;
 import org.apache.lucene.search.spans.SpanTermQuery;
 import org.apache.lucene.store.MMapDirectory;
 import org.junit.BeforeClass;
@@ -276,7 +277,7 @@
 
     @Test
     public void testWildcardPlusWithCollection () throws IOException {
-     // &Erfahrung
+		// &Erfahrung
         SpanTermQuery sq =
                 new SpanTermQuery(new Term("tokens", "tt/l:Erfahrung"));
 
@@ -292,6 +293,23 @@
         SpanMultiTermQueryWrapper<WildcardQuery> mtq = new SpanMultiTermQueryWrapper<WildcardQuery>(
 			new WildcardQuery(new Term("tokens", "s:mein+")));
 
+
+		// Just to make sure, Lucene internal queries treat SpanOr([]) correctly
+		SpanQuery soq = new SpanNearQuery(
+			new SpanQuery[]{mtq,sq},
+			1,
+			true
+			);
+		
+        krillAvailabilityAll.setSpanQuery(soq);
+        kr = sample.search(krillAvailabilityAll);
+
+		// As described in http://korap.github.io/Koral/, '+' is not a valid wildcard
+		assertEquals(0, kr.getMatches().size());
+
+
+		
+		// Check the reported classed query
 		SpanMultipleDistanceQuery mdsq = new SpanMultipleDistanceQuery(
 			new SpanClassQuery(mtq, (byte) 129),
 			new SpanClassQuery(sq, (byte) 129), constraints, true, true);
@@ -301,5 +319,17 @@
 
 		// As described in http://korap.github.io/Koral/, '+' is not a valid wildcard
 		assertEquals(0, kr.getMatches().size());
-    }
+
+		
+		// Check multiple distance query
+		mdsq = new SpanMultipleDistanceQuery(
+			mtq,
+			sq, constraints, true, true);
+		
+        krillAvailabilityAll.setSpanQuery(mdsq);
+        kr = sample.search(krillAvailabilityAll);
+
+		// As described in http://korap.github.io/Koral/, '+' is not a valid wildcard
+		assertEquals(0, kr.getMatches().size());
+	}
 }