Fixed unordered token distance spans (child spans).

Change-Id: Ied8e0a004c5c42d9d6c0bb9d864c734801fbb141
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedDistanceSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedDistanceSpans.java
index e08db92..06227e2 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedDistanceSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedDistanceSpans.java
@@ -126,25 +126,23 @@
             if (currentFirstSpan.getStart() < currentSecondSpan.getStart()
                     || isLastCandidateSmaller(currentFirstSpan,
                             currentSecondSpan)) {
-                // log.trace("current target: "
-                // + firstSpanList.get(0).getStart() + " "
-                // + firstSpanList.get(0).getEnd());
-                // System.out.println("candidates:");
-                // for (CandidateSpan cs: secondSpanList) {
-                // System.out.println(cs.getStart() +" "+ cs.getEnd());
-                // }
+//                log.trace("current target: " + firstSpanList.get(0).getStart()
+//                        + " " + firstSpanList.get(0).getEnd());
+//                System.out.println("candidates:");
+//                for (CandidateSpan cs : secondSpanList) {
+//                    System.out.println(cs.getStart() + " " + cs.getEnd());
+//                }
 
                 matchList = findMatches(currentFirstSpan, secondSpanList, true);
                 updateList(firstSpanList);
             }
             else {
-                // log.trace("current target: "
-                // + secondSpanList.get(0).getStart() + " "
-                // + secondSpanList.get(0).getEnd());
-                // System.out.println("candidates:");
-                // for (CandidateSpan cs: firstSpanList) {
-                // System.out.println(cs.getStart() +" "+ cs.getEnd());
-                // }
+//                log.trace("current target: " + secondSpanList.get(0).getStart()
+//                        + " " + secondSpanList.get(0).getEnd());
+//                System.out.println("candidates:");
+//                for (CandidateSpan cs : firstSpanList) {
+//                    System.out.println(cs.getStart() + " " + cs.getEnd());
+//                }
 
                 matchList = findMatches(currentSecondSpan, firstSpanList,
                         false);
@@ -264,7 +262,7 @@
      * @return a candidate span match
      */
     protected CandidateSpan createMatchCandidate (CandidateSpan target,
-            CandidateSpan cs, boolean isDistanceZero) {
+            CandidateSpan cs, boolean isDistanceZero, boolean isTargetFirstSpan) {
 
         int start = Math.min(target.getStart(), cs.getStart());
         int end = Math.max(target.getEnd(), cs.getEnd());
@@ -282,6 +280,14 @@
         }
         CandidateSpan match = new CandidateSpan(start, end, doc, cost,
                 payloads);
+        if (isTargetFirstSpan) {
+            match.setChildSpan(target);
+            match.setSecondChildSpan(cs);
+        }
+        else {
+            match.setChildSpan(cs);
+            match.setSecondChildSpan(target);
+        }
         //match.setChildSpan(cs);
         return match;
     }
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedElementDistanceSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedElementDistanceSpans.java
index 0e4b7fa..388e97b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedElementDistanceSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedElementDistanceSpans.java
@@ -261,38 +261,6 @@
         return matches;
     }
 
-
-    /**
-     * Creates a match from the two given spans (target and candidate)
-     * 
-     * @param target
-     *            the target span
-     * @param cs
-     *            the candidate span
-     * @param isDistanceZero
-     *            true if the distance between the two spans are zero,
-     *            false otherwise
-     * @param isTargetFirstSpan
-     *            true is the target span is of the first span, false
-     *            otherwise
-     * @return a match
-     */
-    private CandidateSpan createMatchCandidate (CandidateSpan target,
-            CandidateSpan cs, boolean isDistanceZero,
-            boolean isTargetFirstSpan) {
-        CandidateSpan match = createMatchCandidate(target, cs, isDistanceZero);
-        if (isTargetFirstSpan) {
-            match.setChildSpan(target);
-            match.setSecondChildSpan(cs);
-        }
-        else {
-            match.setChildSpan(cs);
-            match.setSecondChildSpan(target);
-        }
-        return match;
-    }
-
-
     @Override
     protected void updateList (List<CandidateSpan> candidateList) {
         updateElementList(candidateList.get(0).getPosition());
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedTokenDistanceSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedTokenDistanceSpans.java
index c0ac7f1..691f978 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedTokenDistanceSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/UnorderedTokenDistanceSpans.java
@@ -127,12 +127,14 @@
 
         List<CandidateSpan> matches = new ArrayList<>();
         int actualDistance;
+        CandidateSpan match;
         for (CandidateSpan cs : candidateList) {
             if (minDistance == 0 &&
             // intersection
                     target.getStart() < cs.getEnd()
                     && cs.getStart() < target.getEnd()) {
-                matches.add(createMatchCandidate(target, cs, true));
+                match = createMatchCandidate(target, cs, true, isTargetFirstSpan);
+                matches.add(match);
                 continue;
             }
 
@@ -146,7 +148,8 @@
             }
             if (minDistance <= actualDistance
                     && actualDistance <= maxDistance) {
-                matches.add(createMatchCandidate(target, cs, false));
+                match = createMatchCandidate(target, cs, false, isTargetFirstSpan);
+                matches.add(match);
             }
         }
         return matches;
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestMultipleDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestMultipleDistanceIndex.java
index c6363e5..c016b5f 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestMultipleDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestMultipleDistanceIndex.java
@@ -123,7 +123,6 @@
         SpanQuery mdq;
         mdq = createQuery("s:b", "s:c", constraints, false);
         kr = ki.search(mdq, (short) 10);
-        // System.out.println(mdq);
 
         assertEquals((long) 3, kr.getTotalResults());
         assertEquals(0, kr.getMatch(0).getStartPos());
@@ -267,7 +266,7 @@
 
 
     /**
-     * Same tokens: ordered and unordered yield the same results
+     * Same tokens: unordered yields twice the same results as ordered
      */
     @Test
     public void testCase5 () throws IOException {
@@ -276,14 +275,15 @@
         ki.addDoc(createFieldDoc1());
         ki.commit();
 
+        // ordered
         List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
-        constraints.add(createConstraint("w", 1, 2, false, false));
-        constraints.add(createConstraint("s", 1, 2, false, false));
+        constraints.add(createConstraint("w", 1, 2, true, false));
+        constraints.add(createConstraint("s", 1, 2, true, false));
 
         SpanQuery mdq;
         mdq = createQuery("s:c", "s:c", constraints, false);
         kr = ki.search(mdq, (short) 10);
-
+        
         assertEquals((long) 4, kr.getTotalResults());
         assertEquals(1, kr.getMatch(0).getStartPos());
         assertEquals(3, kr.getMatch(0).getEndPos());
@@ -294,6 +294,15 @@
         assertEquals(4, kr.getMatch(2).getEndPos());
         assertEquals(3, kr.getMatch(3).getStartPos());
         assertEquals(6, kr.getMatch(3).getEndPos());
+        
+        //unordered
+        constraints = new ArrayList<DistanceConstraint>();
+        constraints.add(createConstraint("w", 1, 2, false, false));
+        constraints.add(createConstraint("s", 1, 2, false, false));
+
+        mdq = createQuery("s:c", "s:c", constraints, false);
+        kr = ki.search(mdq, (short) 10);
+        assertEquals((long) 8, kr.getTotalResults());
 
     }