Fix bug in classed group queries

Change-Id: Ie7713203ecca31ce06d535054608fca3c4d445e5
diff --git a/Changes b/Changes
index 1b8d321..60ba114 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,5 @@
-0.59.1 2019-01-20
+0.59.1 2020-01-28
+    - [bugfix] Fix bug in classed group queries (diewald)
 
 0.59.0 2019-11-28
     - [bugfix] Fix offset retrieval in concurrent searches
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ElementDistanceExclusionSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ElementDistanceExclusionSpans.java
index 1c5ebdc..a5adc05 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ElementDistanceExclusionSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ElementDistanceExclusionSpans.java
@@ -10,8 +10,10 @@
 import org.apache.lucene.index.Term;
 import org.apache.lucene.index.TermContext;
 import org.apache.lucene.search.spans.Spans;
+import org.apache.lucene.search.DocIdSetIterator;
 import org.apache.lucene.util.Bits;
 
+
 import de.ids_mannheim.korap.query.SpanDistanceQuery;
 
 /**
@@ -68,7 +70,7 @@
                                           Map<Term, TermContext> termContexts)
             throws IOException {
         super(query, context, acceptDocs, termContexts);
-
+        
         elements = query.getElementQuery().getSpans(context, acceptDocs,
                 termContexts);
         hasMoreElements = elements.next();
@@ -79,7 +81,11 @@
         this.isOrdered = query.isOrdered();
         candidateList = new ArrayList<CandidateSpan>();
         targetList = new ArrayList<CandidateSpan>();
-        currentDocNum = firstSpans.doc();
+        
+        if (hasMoreSpans)
+            currentDocNum = firstSpans.doc();
+        else
+            currentDocNum = DocIdSetIterator.NO_MORE_DOCS;
 
         minDistance = query.getMinDistance();
         maxDistance = query.getMaxDistance();
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
index ceed514..62b5f7e 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
@@ -419,7 +419,7 @@
         SpanQuery sq = sqwi.toQuery();
 
         // (Pop-up OR Pop-ups) %s0 (Internet OR  Programm)
-        assertEquals(sq.toString(),"spanElementDistance({129: spanOr([tokens:s:Pop-up, tokens:s:Pop-ups])}, {129: spanOr([tokens:s:Internet, tokens:s:Programm])}, [(base/s:s[0:0], notOrdered, excluded)])");
+        assertEquals(sq.toString(),"spanElementDistance({129: spanOr([tokens:s:Blatt, tokens:s:Augen])}, {129: spanOr([tokens:s:Wald, tokens:s:Baum])}, [(base/s:s[0:0], notOrdered, excluded)])");
     };
 	
 
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestKrill.java b/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
index c407cce..32b665c 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
@@ -1230,6 +1230,36 @@
     };
 
 
+    @Test
+    public void queryJSONcosmasSentenceNegationBug () throws IOException {
+        KrillIndex ki = new KrillIndex();
+
+        // Indexing test files
+        for (String i : new String[] {
+                "00001",
+                "00002",
+                "00003",
+                "00004",
+                "00005",
+                "00006",
+                "02439"
+            }) {
+            ki.addDoc(getClass().getResourceAsStream("/wiki/" + i + ".json.gz"),
+                      true);
+        };
+
+        ki.commit();
+
+        String json = getJsonString(getClass()
+                .getResource("/queries/bugs/cosmas-exclude.jsonld")
+                .getFile());
+       
+        Result kr = new Krill(json).apply(ki);
+
+        assertEquals(0, kr.getTotalResults());
+    };
+   
+
     /**
      * This is a Schreibgebrauch ressource that didn't work for
      * element queries.
diff --git a/src/test/resources/queries/bugs/cosmas-exclude.jsonld b/src/test/resources/queries/bugs/cosmas-exclude.jsonld
index 57f50bc..67c53f2 100644
--- a/src/test/resources/queries/bugs/cosmas-exclude.jsonld
+++ b/src/test/resources/queries/bugs/cosmas-exclude.jsonld
@@ -7,7 +7,7 @@
           "wrap":{
             "@type":"koral:term",
             "match":"match:eq",
-            "key":"Pop-up",
+            "key":"Blatt",
             "layer":"orth"
           }
         },{
@@ -15,7 +15,7 @@
           "wrap":{
             "@type":"koral:term",
             "match":"match:eq",
-            "key":"Pop-ups",
+            "key":"Augen",
             "layer":"orth"
           }
         }],
@@ -33,7 +33,7 @@
           "wrap":{
             "@type":"koral:term",
             "match":"match:eq",
-            "key":"Internet",
+            "key":"Wald",
             "layer":"orth"
           }
         },{
@@ -41,7 +41,7 @@
           "wrap":{
             "@type":"koral:term",
             "match":"match:eq",
-            "key":"Programm",
+            "key":"Baum",
             "layer":"orth"
           }
         }],