Clear matchlist if skip fails in NextSpans

Change-Id: I4064b5e9cc5b6b0cb446697f76ceb2490d099cb1
diff --git a/Changes b/Changes
index ebc3112..f2e7488 100644
--- a/Changes
+++ b/Changes
@@ -1,6 +1,8 @@
 0.59.6 2021-10-26
     - [bugfix] Fixed skipping of focus spans (fixed #78; margaretha,
       diewald)
+    - [bugfix] Clear matchlist if skip fails in NextSpans
+      (margaretha, diewald)
 
 0.59.5 2021-10-26
     - [bugfix] Fixed candidate settings in token distance spans
diff --git a/pom.xml b/pom.xml
index d0f48e1..de649e0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -237,6 +237,12 @@
       <artifactId>cobertura-maven-plugin</artifactId>
       <version>${cobertura.version}</version>
       <scope>test</scope>
+      <exclusions>
+      	<exclusion>
+      		<groupId>com.sun</groupId>
+      		<artifactId>tools</artifactId>
+      	</exclusion>
+      </exclusions>
     </dependency>
   </dependencies>
   
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/FocusSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/FocusSpans.java
index c694f26..6a7d8c4 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/FocusSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/FocusSpans.java
@@ -107,6 +107,7 @@
         CandidateSpan cs;
 
         if (DEBUG) {
+            log.debug("FirstSpan [{}]", firstSpans.toString());
             log.debug("FirstSpan: ({}) {}-{}", firstSpans.doc(), firstSpans.start(), firstSpans.end());
         }
         
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java
index 1068e58..466548e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/NextSpans.java
@@ -156,6 +156,9 @@
                               firstSpans.doc(),
                               secondSpans.doc()
                         );
+                    log.debug("First span [{}]",firstSpans.toString());
+                    log.debug("Second span [{}]",secondSpans.toString());
+
                 }
                 candidateListDocNum = firstSpans.doc();
                 searchMatches();
@@ -267,7 +270,11 @@
             if (!firstSpans.skipTo(target)) {
                 hasMoreSpans = false;
                 return false;
-            };
+            }
+            else {
+                // removed all matches found in the previous doc.
+                matchList.clear();
+            }
 
             if (DEBUG) {
                 log.debug("Skip firstSpans to {}={} succeed with positions {}-{}",
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestFocusIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestFocusIndex.java
index 408332f..1bf03a3 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestFocusIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestFocusIndex.java
@@ -299,6 +299,96 @@
         assertEquals(1, kr.getTotalResults());
     }    
 
+    @Test
+    public void testFocusInNextBug2 () throws QueryException, IOException {
+        ki = new KrillIndex();
+
+        FieldDocument fd;
+        fd = new FieldDocument();
+        fd.addString("ID", "doc-1");
+        fd.addTV("base", "c",
+                 "[(0-1)s:c|<>:base/s:t$<b>64<i>0<i>1<i>1<b>0|_0$<i>0<i>1]"
+            );
+        ki.addDoc(fd);
+
+        fd = new FieldDocument();
+        fd.addString("ID", "doc-2");
+        fd.addTV("base", "bab",
+                 "[(0-1)s:b|<>:base/s:t$<b>64<i>0<i>3<i>3<b>0|_0$<i>0<i>1]"+
+                 "[(1-2)s:a|_1$<i>1<i>2]"+
+                 "[(2-3)s:b|a:b|a:b|_2$<i>2<i>3]"
+            );
+        ki.addDoc(fd);
+
+        fd = new FieldDocument();
+        fd.addString("ID", "doc-3");
+        fd.addTV("base", "ddbcebcea",
+                 "[(0-1)s:d|<>:base/s:t$<b>64<i>0<i>9<i>9<b>0|_0$<i>0<i>1]"+
+                 "[(1-2)s:d|_1$<i>1<i>2]"+
+                 "[(2-3)s:b|_2$<i>2<i>3]"+
+                 "[(3-4)s:c|_3$<i>3<i>4]"+
+                 "[(4-5)s:e|_4$<i>4<i>5]"+
+                 "[(5-6)s:b|_5$<i>5<i>6]"+
+                 "[(6-7)s:c|a:b|_6$<i>6<i>7]"+
+                 "[(7-8)s:e|_7$<i>7<i>8]"+
+                 "[(8-9)s:a|a:d|_8$<i>8<i>9]"
+            );
+        ki.addDoc(fd);
+
+        ki.commit();
+
+        // assertEquals("", kr.getMatch(0).getSnippetBrackets());
+
+        QueryBuilder kq = new QueryBuilder("base");
+        
+        SpanQueryWrapper focus = kq.seq(kq.seg("s:b"),kq.focus(kq.seq(kq.seg("s:a"),kq.seg("a:b"),kq.nr(1, kq.seg("s:c")))));
+        kr = ki.search(focus.toQuery(), (short) 10);
+
+        assertEquals(0, kr.getTotalResults());
+    }
+
+
+    @Test
+    public void testFocusInNextBug3 () throws QueryException, IOException {
+        ki = new KrillIndex();
+
+        FieldDocument fd;
+
+        fd = new FieldDocument();
+        fd.addString("ID", "doc-1");
+        fd.addTV("base", "e",
+                 "[(0-1)s:e|<>:base/s:t$<b>64<i>0<i>1<i>1<b>0|a:e|a:e|_0$<i>0<i>1]"
+            );
+        ki.addDoc(fd);
+
+        fd = new FieldDocument();
+        fd.addString("ID", "doc-2");
+        fd.addTV("base", "eae",
+                 "[(0-1)s:e|<>:base/s:t$<b>64<i>0<i>3<i>3<b>0|a:d|_0$<i>0<i>1]"+
+                 "[(1-2)s:a|a:d|_1$<i>1<i>2]"+
+                 "[(2-3)s:e|a:b|a:b|_2$<i>2<i>3]"
+            );
+        ki.addDoc(fd);
+        fd = new FieldDocument();
+        fd.addString("ID", "doc-3");
+        fd.addTV("base", "abbc",
+                 "[(0-1)s:a|<>:base/s:t$<b>64<i>0<i>4<i>4<b>0|_0$<i>0<i>1]"+
+                 "[(1-2)s:b|a:d|_1$<i>1<i>2]"+
+                 "[(2-3)s:b|a:a|a:b|_2$<i>2<i>3]"+
+                 "[(3-4)s:c|a:c|a:c|_3$<i>3<i>4]"
+            );
+        ki.addDoc(fd);
+        ki.commit();
+
+        QueryBuilder kq = new QueryBuilder("base");
+                 
+        SpanQueryWrapper focus = kq.seq(kq.seg("s:b"),kq.focus(kq.seq(kq.seg("s:a"),kq.seg("a:b"),kq.nr(1, kq.seg("s:c")))));
+        kr = ki.search(focus.toQuery(), (short) 10);
+
+        assertEquals(0, kr.getTotalResults());
+    }
+
+    
 
     // @Test
     public void testFocusInNextWithAnnotationsFuzzy () throws QueryException, IOException {