Fixed longer left expansion match order.

Change-Id: Id541bf5a2e57f8315f31f6ae0a4984a9e58c4782
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedSpans.java
index eb3641f..ecdafc0 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ExpandedSpans.java
@@ -95,7 +95,8 @@
                 return true;
             }
             else {
-                setCandidateList(firstSpans.end());
+                setCandidateList();
+//                log.debug(candidateSpans.toString());
             }
         }
         return false;
@@ -107,14 +108,9 @@
      * all possible expansion with respect to the expansion length
      * (min,max) variables.
      * 
-     * @param lastPosition
-     *            is used in left expansion. The start position of
-     *            the candidates to collect must not beyond this
-     *            position.
-     * 
      * @throws IOException
      */
-    private void setCandidateList (int lastPosition) throws IOException {
+    private void setCandidateList () throws IOException {
         CandidateSpan cs;
         int counter, start, end;
 
@@ -130,10 +126,12 @@
                 counter--;
             }
             
+            int lastPosition = firstSpans.start();
             if (hasMoreSpans && (hasMoreSpans = firstSpans.next()) ) {
                 start = Math.max(0, firstSpans.start() - max);
+                log.debug("next candidate start: "+start+", lastPosition "+lastPosition);
                 if (start <= lastPosition) {
-                    setCandidateList(lastPosition);
+                    setCandidateList();
                 }
             }
         }
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 fc915c8..5e21d39 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
@@ -114,9 +114,11 @@
                 matchList.remove(0);
                 return true;
             }
+            
             // Forward firstspan
             hasMoreFirstSpan = firstSpans.next();
             if (hasMoreFirstSpan){
+                log.debug("FirstSpan "+firstSpans.start() +","+firstSpans.end());
                 setMatchList();
             }
             else {
@@ -170,6 +172,7 @@
      * @throws IOException
      */
     private void searchCandidates () throws IOException {
+        log.debug(candidateList.toString());
         Iterator<CandidateSpan> i = candidateList.iterator();
         CandidateSpan cs;
         while (i.hasNext()) {
@@ -200,6 +203,7 @@
     private void searchMatches () throws IOException {
 
         while (hasMoreSpans && candidateListDocNum == secondSpans.doc()) {
+            log.debug("SecondSpan " +secondSpans.start() + "," + secondSpans.end());
             if (secondSpans.start() > firstSpans.end()) {
                 break;
             }
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 edfc875..9ab5c62 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
@@ -637,6 +637,22 @@
         assertEquals(18, kr.getTotalResults());        
     }
     
+    public void testLeftExpansionWrongSorting () throws IOException {
+        KrillIndex ki = new KrillIndex();
+        ki.addDoc(simpleFieldDoc("B u d B R a d m d Z z s B d v", " "));
+        ki.commit();
+        
+        SpanTermQuery stq = new SpanTermQuery(new Term("base", "s:d"));
+        SpanExpansionQuery seq = new SpanExpansionQuery(stq, 0, 8, -1, true);
+        
+        Result kr = ki.search(seq, (short) 25);
+//        for (Match km : kr.getMatches()){
+//             System.out.println(km.getStartPos() +","+km.getEndPos()+" "
+//             +km.getSnippetBrackets()); }
+        // 2-3, 6-7, 8-9, 13-14
+        assertEquals("BudBR[[admdZzsBd]]v", kr.getMatch(15).getSnippetBrackets());
+        assertEquals(28, kr.getTotalResults());
+    }
     
     /** Tests left expansion over start doc boundary. Redundant matches should
      *  be omitted.
@@ -659,6 +675,7 @@
 
     }
     
+    
     private FieldDocument createFieldDoc6 () {
         FieldDocument fd = new FieldDocument();
         fd.addString("ID", "doc-6");