Added bug in SpanWithinQuery

Change-Id: I73b3d1bb4aa29fddc20e28248cb3b9c21eed797c
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestSegmentIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestSegmentIndex.java
index b0c1305..74d611e 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestSegmentIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestSegmentIndex.java
@@ -175,7 +175,6 @@
         assertEquals("EndPos (0)", 2, kr.getMatch(1).endPos);
     }
 
-
     private FieldDocument createFieldDoc0 () {
         fd = new FieldDocument();
         fd.addString("ID", "doc-0");
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
index 11d05a5..103090c 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
@@ -1101,16 +1101,42 @@
         assertEquals(1, kr.getMatch(1).getLocalDocID());
         assertEquals(237, kr.getMatch(1).getStartPos());
         assertEquals(252, kr.getMatch(1).getEndPos());
-
-        /*
-        for (Match km : kr.getMatches()){		
-        	System.out.println(km.getStartPos() +","+km.getEndPos()+" "
-                               +km.getSnippetBrackets());
-        };	
-        */
     };
 
 
+    @Test
+    public void queryJSONcomplexSpanOrTerm () throws QueryException, IOException {
+        /*
+          at org.apache.lucene.search.spans.SpanOrQuery$1.doc(SpanOrQuery.java:234)
+          at de.ids_mannheim.korap.query.spans.WithinSpans.toSameDoc(WithinSpans.java:423)
+          at de.ids_mannheim.korap.query.spans.WithinSpans.next(WithinSpans.java:375)
+          at de.ids_mannheim.korap.KrillIndex.search(KrillIndex.java:1293)
+          at de.ids_mannheim.korap.Krill.apply(Krill.java:304)
+        */
+        // startsWith(<base/s=s>, { lassen | laufen })
+        String jsonPath = getClass().getResource("/queries/bugs/span_or_bug.jsonld")
+            .getFile();
+        String jsonPQuery = readFile(jsonPath);
+        SpanQueryWrapper sqwi = new KrillQuery("tokens").fromJson(jsonPQuery);
+
+        SpanWithinQuery sq = (SpanWithinQuery) sqwi.toQuery();
+
+        KrillIndex ki = new KrillIndex();
+
+        ki.addDoc(getClass().getResourceAsStream("/wiki/PPP-02924.json.gz"),
+                  true);
+        ki.addDoc(getClass().getResourceAsStream("/wiki/DDD-08370.json.gz"),
+                  true);
+        ki.addDoc(getClass().getResourceAsStream("/wiki/SSS-09803.json.gz"),
+                  true);
+        ki.commit();
+        Result kr = ki.search(sq, (short) 10);
+        assertEquals(2, kr.getTotalResults());
+
+    }
+
+
+
     private String readFile (String path) {
         StringBuilder sb = new StringBuilder();
         try {
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
index 5445ed9..4230d0d 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
@@ -554,6 +554,23 @@
     };
 
 
+    @Test
+    public void queryJSONcomplexSpanOrTerm () throws QueryException {
+        // startsWith(<base/s=s>, { lassen | laufen })
+        try {
+            String json = getString(getClass().getResource("/queries/bugs/span_or_bug.jsonld").getFile());
+            KrillQuery kq = new KrillQuery("tokens");
+
+            assertEquals(
+                         kq.fromJson(json).toQuery().toString(),
+                         "spanStartsWith(<tokens:base/s:s />, spanOr([tokens:s:Er, tokens:s:Sie]))");
+        }
+        catch (QueryException e) {
+            fail(e.getMessage());
+        };
+    };
+
+
     public static String getString (String path) {
         StringBuilder contentBuilder = new StringBuilder();
         try {
diff --git a/src/test/resources/queries/bugs/span_or_bug.jsonld b/src/test/resources/queries/bugs/span_or_bug.jsonld
new file mode 100644
index 0000000..49beb6f
--- /dev/null
+++ b/src/test/resources/queries/bugs/span_or_bug.jsonld
@@ -0,0 +1,49 @@
+{
+  "@context":"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+  "query":{
+    "@type":"koral:group",
+    "operation":"operation:position",
+    "frames":["frames:startsWith","frames:matches"],
+    "operands":[
+      {
+	"@type":"koral:span",
+	"wrap":{
+	  "@type":"koral:term",
+	  "foundry":"base",
+	  "layer":"s",
+	  "key":"s"
+	}
+      },
+      {
+	"@type":"koral:token",
+	"wrap":{
+	  "@type":"koral:termGroup",
+	  "relation":"relation:or",
+	  "operands":[
+	    {
+	      "@type":"koral:term",
+	      "key":"Er",
+	      "layer":"orth",
+	      "match":"match:eq"
+	    },
+	    {
+	      "@type":"koral:term",
+	      "key":"Sie",
+	      "layer":"orth",
+	      "match":"match:eq"
+	    }
+	  ]
+	}
+      }
+    ]
+  },
+  "meta":{
+    "startPage":1,
+    "count":25,
+    "cutOff":true,
+    "context":{
+      "left":["t",40],
+      "right":["t",40]
+    }
+  }
+}