Fixed setting candidate span

Because spanOr cannot get doc when ends.

Change-Id: I8183d912a5b4497f0f5e3d1f3b39b2db89ab0c59
diff --git a/Changes b/Changes
index 69c85d5..1594189 100644
--- a/Changes
+++ b/Changes
@@ -1,3 +1,7 @@
+0.59.5 2021-09-28
+    - [bugfix] Fixed candidate settings in token distance spans
+      (margaretha, diewald)
+
 0.59.4 2021-07-27
     - [cleanup] Upgrade dependencies (diewald)
 
diff --git a/pom.xml b/pom.xml
index dd3872b..405301a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -35,7 +35,7 @@
 
   <groupId>de.ids_mannheim.korap</groupId>
   <artifactId>Krill</artifactId>
-  <version>0.59.4</version>
+  <version>0.59.5</version>
   <packaging>jar</packaging>
 
   <name>Krill</name>
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/TokenDistanceSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/TokenDistanceSpans.java
index dbdf130..09bebb0 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/TokenDistanceSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/TokenDistanceSpans.java
@@ -48,19 +48,23 @@
 
     @Override
     protected void setCandidateList () throws IOException {
-        if (candidateListDocNum == secondSpans.doc()) {
-            copyPossibleCandidates();
-            addNewCandidates();
-            candidateListIndex = -1;
-        }
-        else {
-            candidateList.clear();
-            if (hasMoreFirstSpans && ensureSameDoc(firstSpans, secondSpans)) {
-                candidateListDocNum = firstSpans.doc();
-                addNewCandidates();
-                candidateListIndex = -1;
-            }
-        }
+		if (hasMoreSpans) {
+			if (candidateListDocNum == secondSpans.doc()) {
+				copyPossibleCandidates();
+				addNewCandidates();
+				candidateListIndex = -1;
+			} else {
+				candidateList.clear();
+				if (hasMoreFirstSpans && ensureSameDoc(firstSpans, secondSpans)) {
+					candidateListDocNum = firstSpans.doc();
+					addNewCandidates();
+					candidateListIndex = -1;
+				}
+			}
+		}
+		else {
+			candidateList.clear();
+		}
     }
 
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
index 975afe0..07cb33e 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
@@ -42,7 +42,7 @@
     private FieldDocument createFieldDoc1 () {
         FieldDocument fd = new FieldDocument();
         fd.addString("ID", "doc-1");
-        fd.addTV("base", "text",
+        fd.addTV("base", "ceccdcdeed",
                 "[(0-1)s:c|_1$<i>0<i>1]"
                  + "[(1-2)s:e|_2$<i>1<i>2]"
                  + "[(2-3)s:c|_3$<i>2<i>3|<>:y$<b>64<i>2<i>4<i>4<b>0]"
@@ -68,6 +68,30 @@
     }
 
 
+    private FieldDocument createFieldDoc3 () {
+        FieldDocument fd = new FieldDocument();
+        fd.addString("ID", "doc-3");
+        fd.addTV("base", "cffe",
+                 "[(0-1)s:c|_1$<i>0<i>1]"
+                 + "[(1-2)s:f|_2$<i>1<i>2]"
+                 + "[(2-3)s:f|_3$<i>2<i>3]"
+                 + "[(3-4)s:e|_4$<i>3<i>4]");
+        return fd;
+    }
+
+    private FieldDocument createFieldDoc4 () {
+        FieldDocument fd = new FieldDocument();
+        fd.addString("ID", "doc-4");
+        fd.addTV("base", "eeef",
+                 "[(0-1)s:e|_1$<i>0<i>1]"
+                 + "[(1-2)s:e|_2$<i>1<i>2]"
+                 + "[(2-3)s:e|_3$<i>2<i>3]"
+                 + "[(3-4)s:f|_4$<i>3<i>4]");
+        return fd;
+    }
+
+    
+
     private SpanQuery createQuery (String x, String y, int min, int max,
             boolean isOrdered) {
         SpanQuery sq = new SpanDistanceQuery(
@@ -333,8 +357,10 @@
         ki = new KrillIndex();
         ki.addDoc(createFieldDoc1());
         ki.commit();
+        SpanQuery sq;
 
-        SpanQuery sq = new SpanDistanceQuery(
+        // (c or d) /+w1 e
+        sq = new SpanDistanceQuery(
             new SpanOrQuery(
                 new SpanTermQuery(new Term("base", "s:c")),
                 new SpanTermQuery(new Term("base", "s:d"))
@@ -343,8 +369,21 @@
             new DistanceConstraint(0, 1, true, false), true);
 
         kr = ki.search(sq, (short) 10);
-        assertEquals(kr.getTotalResults(), 2);
+        assertEquals(2,kr.getTotalResults());
 
+        // (c or d) /+w1 c
+        sq = new SpanDistanceQuery(
+            new SpanOrQuery(
+                new SpanTermQuery(new Term("base", "s:c")),
+                new SpanTermQuery(new Term("base", "s:d"))
+                ),
+            new SpanTermQuery(new Term("base", "s:c")),
+            new DistanceConstraint(0, 1, true, false), true);
+
+        kr = ki.search(sq, (short) 10);
+        assertEquals(6,kr.getTotalResults());
+
+        // (c or d) /+w1 (e or c)
         sq = new SpanDistanceQuery(
             new SpanOrQuery(
                 new SpanTermQuery(new Term("base", "s:c")),
@@ -357,7 +396,26 @@
             new DistanceConstraint(0, 1, true, false), true);
 
         kr = ki.search(sq, (short) 10);
-        assertEquals(kr.getTotalResults(), 4);
+        assertEquals(kr.getTotalResults(), 8);
+
+        ki.addDoc(createFieldDoc3());
+        ki.addDoc(createFieldDoc4());
+        ki.commit();
+
+        // (c or d) /+w1 (e or f)
+        sq = new SpanDistanceQuery(
+            new SpanOrQuery(
+                new SpanTermQuery(new Term("base", "s:c")),
+                new SpanTermQuery(new Term("base", "s:d"))
+                ),
+            new SpanOrQuery(
+                new SpanTermQuery(new Term("base", "s:e")),
+                new SpanTermQuery(new Term("base", "s:f"))
+                ),
+            new DistanceConstraint(0, 1, true, false), true);
+
+        kr = ki.search(sq, (short) 10);
+        assertEquals(3, kr.getTotalResults());
         ki.close();
     }
 }