Fixed class payload in SpanRepetition
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/AttributeSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/AttributeSpans.java
index 0966e1d..6a32009 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/AttributeSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/AttributeSpans.java
@@ -56,6 +56,7 @@
 	@Override
 	public boolean next() throws IOException {		
 		isStartEnumeration=false;
+		matchPayload.clear();
 		return advance();
 	}
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/CandidateSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/CandidateSpans.java
index 9d0352d..deac119 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/CandidateSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/CandidateSpans.java
@@ -9,7 +9,7 @@
 /** A span kept as a candidate for matching with another Span
  * 	@author margaretha
  * */
-public class CandidateSpans implements Comparable<CandidateSpans>{	
+public class CandidateSpans implements Comparable<CandidateSpans>, Cloneable{	
 	private int doc,start,end;
 	private long cost;
 	private Collection<byte[]> payloads = new ArrayList<>();
@@ -36,6 +36,17 @@
 		}		*/
 	}	
 	
+	@Override
+	protected CandidateSpans clone() throws CloneNotSupportedException {
+		return new CandidateSpans(
+				this.start,
+				this.end,
+				this.doc,
+				this.cost,
+				this.payloads
+		);		
+	}
+	
 	public CandidateSpans(Spans span, int position) throws IOException {
 		this(span);
 		this.position = position;		
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/RepetitionSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/RepetitionSpans.java
index 8303956..c4412d9 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/RepetitionSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/RepetitionSpans.java
@@ -45,6 +45,7 @@
 	@Override
 	public boolean next() throws IOException {
 		isStartEnumeration = false;
+		matchPayload.clear();
 		return advance();
 	}
 
@@ -58,8 +59,7 @@
 				setMatchProperties(matchList.get(0));
 				matchList.remove(0);
 				return true;
-			}
-			matchPayload.clear();
+			}			
 			matchCost = 0;
 			
 			List<CandidateSpans> adjacentSpans = collectAdjacentSpans();
@@ -86,7 +86,7 @@
 			if (firstSpans.start() > prevSpan.getEnd()){
 				break;
 			}
-			else {
+			else if (firstSpans.start() == prevSpan.getEnd()){
 				prevSpan = new CandidateSpans(firstSpans);
 				adjacentSpans.add(prevSpan);
 			}
@@ -105,8 +105,14 @@
 			int endIndex;
 			while ((endIndex = j+i-1) < adjacentSpans.size()){
 				startSpan = adjacentSpans.get(j);
-				if (i == 1){
-					matchList.add(startSpan);
+				if (i == 1){					
+					try {
+						matchSpan = startSpan.clone();
+						matchSpan.setPayloads(computeMatchPayload(adjacentSpans, 0, endIndex-1));
+						matchList.add(matchSpan);		
+					} catch (CloneNotSupportedException e) {
+						e.printStackTrace();
+					}
 				}
 				else {
 					endSpan = adjacentSpans.get(endIndex);														
@@ -114,8 +120,8 @@
 							startSpan.getStart(), 
 							endSpan.getEnd(), 
 							startSpan.getDoc(), 
-							computeMatchCost(adjacentSpans, i, endIndex), 
-							computeMatchPayload(adjacentSpans, i, endIndex));
+							computeMatchCost(adjacentSpans, 0, endIndex), 
+							computeMatchPayload(adjacentSpans, 0, endIndex));
 					
 					//System.out.println("c:"+matchSpan.getCost() +" p:"+ matchSpan.getPayloads().size());
 					//System.out.println(startSpan.getStart() +","+endSpan.getEnd());
@@ -146,6 +152,7 @@
 			int start, int end){		
 		long matchCost = 0;
 		for (int i=start; i<= end; i++){
+			CandidateSpans c = adjacentSpans.get(i);
 			matchCost += adjacentSpans.get(i).getCost();
 		}		
 		return matchCost;
diff --git a/src/test/java/de/ids_mannheim/korap/highlight/TestClass.java b/src/test/java/de/ids_mannheim/korap/highlight/TestClass.java
index 5d3e0b5..04e1d3f 100644
--- a/src/test/java/de/ids_mannheim/korap/highlight/TestClass.java
+++ b/src/test/java/de/ids_mannheim/korap/highlight/TestClass.java
@@ -7,9 +7,11 @@
 import java.io.FileReader;
 import java.io.IOException;
 
+import org.apache.lucene.search.spans.SpanQuery;
 import org.junit.Test;
 
 import de.ids_mannheim.korap.KorapIndex;
+import de.ids_mannheim.korap.KorapMatch;
 import de.ids_mannheim.korap.KorapQuery;
 import de.ids_mannheim.korap.KorapResult;
 import de.ids_mannheim.korap.KorapSearch;
@@ -56,6 +58,36 @@
 				kr.getMatch(1).getSnippetBrackets());
 	}
 	
+	@Test
+	public void queryJSONpoly4() throws QueryException, IOException {
+		
+		String jsonPath = getClass().getResource("/queries/poly4.json").getFile();
+		String jsonQuery = readFile(jsonPath);
+		SpanQueryWrapper sqwi = new KorapQuery("tokens").fromJSON(
+				jsonQuery
+		);
+		SpanQuery sq = sqwi.toQuery();
+		System.out.println(sq.toString());
+		
+		ki = new KorapIndex();
+	    ki.addDocFile(
+	    	getClass().getResource("/wiki/SSS-09803.json.gz").getFile(),true);
+	    
+	    ki.commit();
+		kr = ki.search(sq, (short) 10);
+		
+		assertEquals(827, kr.getTotalResults());
+		assertEquals(3, kr.getMatch(0).getStartPos());
+		assertEquals(5, kr.getMatch(0).getEndPos());
+		
+		for (KorapMatch km : kr.getMatches()){
+			System.out.println(km.getStartPos() +","+km.getEndPos()+" "
+					+km.getSnippetBrackets()
+			);
+		}
+	}
+	
+	
 	private String readFile(String path) {
 		StringBuilder sb = new StringBuilder();
 		try {
diff --git a/src/test/resources/queries/poly4.json b/src/test/resources/queries/poly4.json
new file mode 100644
index 0000000..8f1f1ec
--- /dev/null
+++ b/src/test/resources/queries/poly4.json
@@ -0,0 +1,85 @@
+{
+   "@context" : "http://ids-mannheim.de/ns/KorAP/json-ld/v0.2/context.jsonld",
+   "collection" : {},
+   "collections" : [
+      {
+         "@type" : "korap:meta-filter",
+         "@value" : {
+            "@field" : "korap:field#corpusID",
+            "@type" : "korap:term",
+            "@value" : "WPD"
+         }
+      }
+   ],
+   "errors" : [],
+   "messages" : [
+      [
+         303,
+         "Deprecated 2014-07-24: 'min' and 'max' to be supported until 3 months from deprecation date."
+      ],
+      [
+         303,
+         "Deprecated 2014-10-07: 'class' only to be supported until 3 months from deprecation date. Classes are now defined using the 'classOut' attribute."
+      ],
+      [
+         303,
+         "Deprecated 2014-07-24: 'min' and 'max' to be supported until 3 months from deprecation date."
+      ]
+   ],
+   "meta" : {},
+   "query" : {
+      "@type" : "korap:group",
+      "boundary" : {
+         "@type" : "korap:boundary",
+         "max" : 3,
+         "min" : 1
+      },
+      "max" : 3,
+      "min" : 1,
+      "operands" : [
+         {
+            "@type" : "korap:group",
+            "operands" : [
+               {
+                  "@type" : "korap:group",
+                  "class" : 1,
+                  "classOut" : 1,
+                  "operands" : [
+                     {
+                        "@type" : "korap:token",
+                        "wrap" : {
+                           "@type" : "korap:term",
+                           "foundry" : "cnx",
+                           "key" : "N",
+                           "layer" : "p",
+                           "match" : "match:eq"
+                        }
+                     }
+                  ],
+                  "operation" : "operation:class"
+               },
+               {
+                  "@type" : "korap:group",
+                  "boundary" : {
+                     "@type" : "korap:boundary",
+                     "max" : 5,
+                     "min" : 1
+                  },
+                  "max" : 5,
+                  "min" : 1,
+                  "operands" : [
+                     {
+                        "@type" : "korap:token"
+                     }
+                  ],
+                  "operation" : "operation:repetition"
+               }
+            ],
+            "operation" : "operation:sequence"
+         }
+      ],
+      "operation" : "operation:repetition"
+   },
+   "warnings" : []
+}
+  
\ No newline at end of file