Fixed expansion query.

Change-Id: I35da26608ba4ec91f9e8942d4b34a1dfd1c8e461
diff --git a/Changes b/Changes
index 38072b5..360ac2a 100644
--- a/Changes
+++ b/Changes
@@ -2,6 +2,7 @@
     - Add totalResources to results (diewald)
     - [bugfix] Fix casefolding for case-insensitive queries
       (diewald).
+    - [bugfix] Fixed expansion query (margaretha)
 
 0.61.2 2023-04-05
     - [bugfix] Fix pagebreak retrieval (margaretha, diewald)
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedExclusionSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedExclusionSpans.java
index ecddc2a..56e9933 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedExclusionSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedExclusionSpans.java
@@ -206,7 +206,8 @@
 
         CandidateSpan firstNotClause = null;
         // System.out.println("main start:"+firstSpans.start());
-        while (hasMoreNotClause && notClause.start() < expansionEnd) {
+        while (hasMoreNotClause && notClause.doc() == firstSpans.doc() &&
+                notClause.start() < expansionEnd) {
             // between firstspan.end() and expansionEnd
             if (!isFound && notClause.start() >= firstSpans.end()) {
                 maxPos = notClause.start() - firstSpans.end() - 1;
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
index 9d5806c..ff3f6e9 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
@@ -27,6 +27,8 @@
 import de.ids_mannheim.korap.query.SpanExpansionQuery;
 import de.ids_mannheim.korap.query.SpanNextQuery;
 import de.ids_mannheim.korap.query.SpanRepetitionQuery;
+import de.ids_mannheim.korap.query.SpanSegmentQuery;
+import de.ids_mannheim.korap.query.SpanWithinQuery;
 import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
 import de.ids_mannheim.korap.response.Match;
 import de.ids_mannheim.korap.response.Result;
@@ -174,6 +176,8 @@
      */
     @Test
     public void testRightExpansionWithExclusion () throws IOException {
+        // [pos=tt/p:NN][orth=Buchstabe]
+        
         byte classNumber = 1;
         SpanTermQuery stq = new SpanTermQuery(new Term("tokens", "tt/p:NN"));
         SpanTermQuery notQuery =
@@ -195,12 +199,29 @@
         assertEquals(13, kr.getMatch(3).getEndPos());
         assertEquals(10, kr.getMatch(3).getStartPos(1));
         assertEquals(13, kr.getMatch(3).getEndPos(1));
+    }
+    
+    @Test
+    public void testNextRightExpansion () throws IOException {
+        KrillIndex ki = new KrillIndex();
+        //ki.addDoc(simpleFieldDoc("daaec"));
+        ki.addDoc(simpleFieldDoc("deaccaab"));
+        ki.addDoc(simpleFieldDoc("cabdadceedc"));
+        //ki.addDoc(simpleFieldDoc("aadaeaeea"));
+        ki.commit();
+        
+        SpanTermQuery a = new SpanTermQuery(new Term("base", "s:c"));
+        SpanTermQuery stq = new SpanTermQuery(new Term("base", "s:a"));
+        SpanTermQuery notQuery = new SpanTermQuery(new Term("base", "s:b"));
 
-        /*
-         * for (Match km : kr.getMatches()){
-         * System.out.println(km.getStartPos() +","+km.getEndPos()+" "
-         * +km.getSnippetBrackets()); }
-         */
+
+        SpanExpansionQuery seq = new SpanExpansionQuery(stq, notQuery, 1, 1, 0,
+                true);
+
+        SpanNextQuery nq = new SpanNextQuery(a, seq);
+
+        kr = ki.search(nq);
+        assertEquals(1, kr.getMatches().size());
     }
 
     /**