Rename SpanQuantifierQuery to SpanRepetitionQuery,
update SpanDistanceQuery to use DistanceConstraint
diff --git a/src/main/java/de/ids_mannheim/korap/query/DistanceConstraint.java b/src/main/java/de/ids_mannheim/korap/query/DistanceConstraint.java
index 29e191a..9a92307 100644
--- a/src/main/java/de/ids_mannheim/korap/query/DistanceConstraint.java
+++ b/src/main/java/de/ids_mannheim/korap/query/DistanceConstraint.java
@@ -1,6 +1,7 @@
 package de.ids_mannheim.korap.query;
 
-/**	Create a distance constraint for multiple distance query.
+/**	Specify constraints of distance in SpanDistanceQueries or 
+ * 	SpanMultipleDistanceQueries
  * 	 
  * 	@author margaretha
  * */
@@ -10,23 +11,43 @@
 	private String unit;
 	private SpanElementQuery elementQuery;
 	private boolean exclusion;
+	private boolean isOrdered;
 	
-	public DistanceConstraint(int min, int max, boolean exclusion) {
+	public DistanceConstraint(int min, int max, boolean isOrdered, boolean exclusion) {
 		this.unit = "w";
 		this.minDistance = min;
 		this.maxDistance = max;
-		this.exclusion = exclusion;
+		this.isOrdered = isOrdered;
+		this.exclusion = exclusion;		
 	}
 	
-	public DistanceConstraint(SpanElementQuery elementQuery, int min, int max, 
-			boolean exclusion) {
+	public DistanceConstraint(SpanElementQuery elementQuery, int min, int max, boolean 
+			isOrdered, boolean exclusion) {
 		this.unit = elementQuery.getElementStr();
 		this.minDistance = min;
 		this.maxDistance = max;
-		this.exclusion = exclusion;		
-		this.elementQuery = elementQuery;		
+		this.isOrdered = isOrdered;
+		this.exclusion = exclusion;
+		this.elementQuery = elementQuery;
 	}
 	
+	
+//	public DistanceConstraint(int min, int max, boolean exclusion) {
+//		this.unit = "w";
+//		this.minDistance = min;
+//		this.maxDistance = max;
+//		this.exclusion = exclusion;
+//	}
+//	
+//	public DistanceConstraint(SpanElementQuery elementQuery, int min, int max, 
+//			boolean exclusion) {
+//		this.unit = elementQuery.getElementStr();
+//		this.minDistance = min;
+//		this.maxDistance = max;
+//		this.exclusion = exclusion;		
+//		this.elementQuery = elementQuery;		
+//	}
+	
 	public int getMinDistance() {
 		return minDistance;
 	}
@@ -59,4 +80,12 @@
 	public void setExclusion(boolean exclusion) {
 		this.exclusion = exclusion;
 	}
+
+	public boolean isOrdered() {
+		return isOrdered;
+	}
+
+	public void setOrdered(boolean isOrdered) {
+		this.isOrdered = isOrdered;
+	}
 }
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanDistanceQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanDistanceQuery.java
index 7ce869a..6b01fdb 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanDistanceQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanDistanceQuery.java
@@ -34,32 +34,50 @@
 	private SpanElementQuery elementQuery; // element distance unit
 	private String distanceUnit;
 	private String spanName;
+	private DistanceConstraint constraint;
 
 	public SpanDistanceQuery(SpanQuery firstClause, SpanQuery secondClause, 
-			int minDistance, int maxDistance, boolean isOrdered, 
-			boolean collectPayloads) {		
-		super(firstClause, secondClause, collectPayloads); 
-		init(minDistance, maxDistance, isOrdered);
-		distanceUnit = "w";
-		spanName = "spanDistance";
-	}
-	
-	public SpanDistanceQuery(SpanElementQuery elementQuery, SpanQuery firstClause, 
-			SpanQuery secondClause, int minDistance, int maxDistance, 
-			boolean isOrdered, boolean collectPayloads) {
+			DistanceConstraint constraint, boolean collectPayloads) {
 		super(firstClause, secondClause, collectPayloads);
-    	init(minDistance, maxDistance, isOrdered);
-		this.elementQuery = elementQuery;
-		distanceUnit = elementQuery.getElementStr();
-		spanName = "spanElementDistance";
+		this.constraint = constraint;
+		this.minDistance = constraint.getMinDistance();
+		this.maxDistance = constraint.getMaxDistance();
+		this.isOrdered = constraint.isOrdered();
+		this.exclusion = constraint.isExclusion();
+		this.distanceUnit = constraint.getUnit();
+		
+		if (constraint.getElementQuery() != null){
+			spanName = "spanElementDistance";
+			this.elementQuery = constraint.getElementQuery();
+		}
+		else { spanName = "spanDistance"; }
 	}
 	
-	private void init(int minDistance, int maxDistance,boolean isOrdered){
-		this.minDistance = minDistance;
-		this.maxDistance = maxDistance;
-		this.isOrdered = isOrdered;
-		this.exclusion = false;
-	}
+//	public SpanDistanceQuery(SpanQuery firstClause, SpanQuery secondClause, 
+//			int minDistance, int maxDistance, boolean isOrdered, 
+//			boolean collectPayloads) {		
+//		super(firstClause, secondClause, collectPayloads); 
+//		init(minDistance, maxDistance, isOrdered);
+//		distanceUnit = "w";
+//		spanName = "spanDistance";
+//	}
+//	
+//	public SpanDistanceQuery(SpanElementQuery elementQuery, SpanQuery firstClause, 
+//			SpanQuery secondClause, int minDistance, int maxDistance, 
+//			boolean isOrdered, boolean collectPayloads) {
+//		super(firstClause, secondClause, collectPayloads);
+//    	init(minDistance, maxDistance, isOrdered);
+//		this.elementQuery = elementQuery;
+//		distanceUnit = elementQuery.getElementStr();
+//		spanName = "spanElementDistance";
+//	}
+//	
+//	private void init(int minDistance, int maxDistance,boolean isOrdered){
+//		this.minDistance = minDistance;
+//		this.maxDistance = maxDistance;
+//		this.isOrdered = isOrdered;
+//		this.exclusion = false;
+//	}
 	
 	@Override
 	public String toString(String field) {
@@ -84,20 +102,17 @@
     }
 	
 	@Override
-	public SpanDistanceQuery clone() {
+	public SpanDistanceQuery clone() {		
 		SpanDistanceQuery spanDistanceQuery = new SpanDistanceQuery(
 		    (SpanQuery) firstClause.clone(),
 		    (SpanQuery) secondClause.clone(),
-		    this.minDistance,
-		    this.maxDistance,
-		    this.isOrdered,
+		    this.constraint,
 		    this.collectPayloads
         );
 				
 		if (this.elementQuery != null) {
 			spanDistanceQuery.setElementQuery(this.elementQuery);
 		}
-		spanDistanceQuery.setExclusion(this.exclusion);
 		spanDistanceQuery.setBoost(getBoost());
 		return spanDistanceQuery;	
 	}
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanMultipleDistanceQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanMultipleDistanceQuery.java
index 473e9c6..43cff3f 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanMultipleDistanceQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanMultipleDistanceQuery.java
@@ -72,7 +72,7 @@
 			sb.append(":");
 			sb.append(c.getMaxDistance());
 			sb.append("], ");		
-			sb.append(isOrdered ? "ordered, " : "notOrdered, ");
+			sb.append(c.isOrdered() ? "ordered, " : "notOrdered, ");
 			sb.append(c.isExclusion() ? "excluded)]" : "notExcluded)");			
 			if (i < size-1) sb.append(", ");
 		}		
@@ -89,18 +89,17 @@
 	public Spans getSpans(AtomicReaderContext context, Bits acceptDocs,
 			Map<Term, TermContext> termContexts) throws IOException {			
 		
-		DistanceConstraint c;		
 		SpanDistanceQuery sdq,sdq2;		
 		Spans ds,ds2;
 		MultipleDistanceSpans mds = null;
 		boolean exclusion;
-		
-		c = constraints.get(0);
-		sdq = createSpanDistanceQuery(c);
+				
+		sdq = new SpanDistanceQuery(firstClause, secondClause, constraints.get(0), collectPayloads);
 		ds = sdq.getSpans(context, acceptDocs, termContexts);
 				
 		for (int i=1; i< constraints.size(); i++){
-			sdq2 = createSpanDistanceQuery(constraints.get(i));
+			sdq2 = new SpanDistanceQuery(firstClause, secondClause, constraints.get(i), 
+					collectPayloads);
 			ds2 = sdq2.getSpans(context, acceptDocs, termContexts);			
 			
 			exclusion = sdq.isExclusion() && sdq2.isExclusion();
@@ -110,26 +109,7 @@
 		}
 		
 		return mds;
-	}
-	
-	/** Create a SpanDistanceQuery based on the given constraint.
-	 * 	@return a SpanDistanceQuery 
-	 * */
-	private SpanDistanceQuery createSpanDistanceQuery(DistanceConstraint c) {		
-		SpanDistanceQuery sdq;		
-		if (c.getUnit().equals("w")){
-			sdq = new SpanDistanceQuery(firstClause, secondClause,
-					c.getMinDistance(), c.getMaxDistance(),isOrdered, 
-					collectPayloads);		
-		}
-		else {
-			sdq = new SpanDistanceQuery(c.getElementQuery(), firstClause, 
-					secondClause, c.getMinDistance(), c.getMaxDistance(),
-					isOrdered, collectPayloads);
-		}
-		sdq.setExclusion(c.isExclusion());
-		return sdq;
-	}
+	}	
 
 	public List<DistanceConstraint> getConstraints() {
 		return constraints;
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanQuantifierQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanRepetitionQuery.java
similarity index 82%
rename from src/main/java/de/ids_mannheim/korap/query/SpanQuantifierQuery.java
rename to src/main/java/de/ids_mannheim/korap/query/SpanRepetitionQuery.java
index 85b881b..e25e5fe 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanQuantifierQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanRepetitionQuery.java
@@ -11,13 +11,13 @@
 import org.apache.lucene.util.Bits;
 import org.apache.lucene.util.ToStringUtils;
 
-import de.ids_mannheim.korap.query.spans.QuantifierSpans;
+import de.ids_mannheim.korap.query.spans.RepetitionSpans;
 
-public class SpanQuantifierQuery extends SimpleSpanQuery{
+public class SpanRepetitionQuery extends SimpleSpanQuery{
 	
 	private int min, max;
 	
-	public SpanQuantifierQuery(SpanQuery sq, int min, int max,
+	public SpanRepetitionQuery(SpanQuery sq, int min, int max,
 			boolean collectPayloads) {
 		super(sq, collectPayloads);
 		this.min = min;
@@ -26,7 +26,7 @@
 
 	@Override
 	public SimpleSpanQuery clone() {
-		SpanQuantifierQuery sq = new SpanQuantifierQuery(
+		SpanRepetitionQuery sq = new SpanRepetitionQuery(
 				(SpanQuery) this.firstClause.clone(), 
 				this.min, 
 				this.max, 
@@ -38,7 +38,7 @@
 	@Override
 	public Spans getSpans(AtomicReaderContext context, Bits acceptDocs,
 			Map<Term, TermContext> termContexts) throws IOException {
-		return new QuantifierSpans(this, context, acceptDocs, termContexts);
+		return new RepetitionSpans(this, context, acceptDocs, termContexts);
 	}
 
 	@Override
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/QuantifierSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/RepetitionSpans.java
similarity index 92%
rename from src/main/java/de/ids_mannheim/korap/query/spans/QuantifierSpans.java
rename to src/main/java/de/ids_mannheim/korap/query/spans/RepetitionSpans.java
index 8c99ac0..923debc 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/QuantifierSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/RepetitionSpans.java
@@ -13,16 +13,16 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import de.ids_mannheim.korap.query.SpanQuantifierQuery;
+import de.ids_mannheim.korap.query.SpanRepetitionQuery;
 
-public class QuantifierSpans extends SimpleSpans{
+public class RepetitionSpans extends SimpleSpans{
 	
 	private int min,max;
 	private long matchCost;
 	private List<CandidateSpan> matchList;
-	private Logger log = LoggerFactory.getLogger(QuantifierSpans.class);
+	private Logger log = LoggerFactory.getLogger(RepetitionSpans.class);
 	
-	public QuantifierSpans(SpanQuantifierQuery query,
+	public RepetitionSpans(SpanRepetitionQuery query,
 			AtomicReaderContext context, Bits acceptDocs,
 			Map<Term, TermContext> termContexts) 
 			throws IOException {
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 b9ed42c..a0c4474 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
@@ -197,7 +197,7 @@
 			}
 			 
 			if (minDistance <= actualDistance && actualDistance <= maxDistance)
-				matches.add(createMatchCandidate(target, cs, false));			
+				matches.add(createMatchCandidate(target, cs, false));
 		}		
 		return matches;
 	}
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
index abf2ae4..5e1c91e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
@@ -96,7 +96,7 @@
     public SpanSequenceQueryWrapper withConstraint (int min, int max, boolean exclusion) {
 	if (this.constraints == null)
 	    this.constraints = new ArrayList<DistanceConstraint>(1);
-	this.constraints.add(new DistanceConstraint(min, max, exclusion));
+	this.constraints.add(new DistanceConstraint(min, max, isInOrder, exclusion));
 	return this;
     };
 
@@ -111,11 +111,11 @@
 	if (this.constraints == null)
 	    this.constraints = new ArrayList<DistanceConstraint>(1);
 	if (unit.equals("w"))
-	    this.constraints.add(new DistanceConstraint(min, max, exclusion));
+	    this.constraints.add(new DistanceConstraint(min, max, isInOrder, exclusion));
 	else
 	    this.constraints.add(
 		 new DistanceConstraint(
-                     new SpanElementQuery(this.field, unit), min, max, exclusion)
+                     new SpanElementQuery(this.field, unit), min, max, isInOrder, exclusion)
 	    );
 	return this;
     };
@@ -150,15 +150,11 @@
 	    if (!constraint.getUnit().equals("w")) {
 		for (int i = 1; i < this.segments.size(); i++) {
 		    SpanDistanceQuery sdquery = new SpanDistanceQuery(
-		        new SpanElementQuery(this.field, constraint.getUnit()),
-	                query,
-			this.segments.get(i),
-			constraint.getMinDistance(),
-			constraint.getMaxDistance(),
-			this.isInOrder(),
-			true
+			    query,
+				this.segments.get(i),
+				constraint,
+				true
 		    );
-		    sdquery.setExclusion(constraint.isExclusion());
 		    query = (SpanQuery) sdquery;
 		};
 	    }
@@ -169,12 +165,9 @@
 		    SpanDistanceQuery sdquery = new SpanDistanceQuery(
 	                query,
 			this.segments.get(i),
-			constraint.getMinDistance(),
-			constraint.getMaxDistance(),
-			this.isInOrder(),
+			constraint,
 			true
 		    );
-		    sdquery.setExclusion(constraint.isExclusion());
 		    query = (SpanQuery) sdquery;
 		};
 	    };
@@ -188,7 +181,7 @@
 	        query,
 		this.segments.get(i),
 		this.constraints,
-		this.isInOrder(),
+		isInOrder,
 		true
 	    );
 	};
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestDistanceExclusionIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestDistanceExclusionIndex.java
index 1ff0b64..8505452 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestDistanceExclusionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestDistanceExclusionIndex.java
@@ -11,6 +11,7 @@
 
 import de.ids_mannheim.korap.KorapIndex;
 import de.ids_mannheim.korap.KorapResult;
+import de.ids_mannheim.korap.query.DistanceConstraint;
 import de.ids_mannheim.korap.query.SpanDistanceQuery;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 
@@ -107,9 +108,9 @@
         SpanDistanceQuery sq;        
         sq = new SpanDistanceQuery(
         		new SpanElementQuery("base", "x"),
-        		new SpanElementQuery("base", "y")
-        		,0,1,false,true);
-        sq.setExclusion(true);
+        		new SpanElementQuery("base", "y"),
+        		new DistanceConstraint(0, 1, false, true),
+        		true);        
         
         kr = ki.search(sq, (short) 10);
         assertEquals(1, kr.getTotalResults());
@@ -130,12 +131,9 @@
     	SpanDistanceQuery sq = new SpanDistanceQuery(
         		new SpanTermQuery(new Term("base",x)),
         		new SpanTermQuery(new Term("base",y)),
-        		min,
-        		max,
-        		isOrdered,
+        		new DistanceConstraint(min, max, isOrdered,true),
         		true
-        );
-    	sq.setExclusion(true);
+        );    	
     	return sq;
     }
     
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
index 2273881..b124eff 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
@@ -13,6 +13,7 @@
 
 import de.ids_mannheim.korap.KorapIndex;
 import de.ids_mannheim.korap.KorapResult;
+import de.ids_mannheim.korap.query.DistanceConstraint;
 import de.ids_mannheim.korap.query.SpanDistanceQuery;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 import de.ids_mannheim.korap.query.SpanSegmentQuery;
@@ -68,13 +69,11 @@
         return fd;
 	}
     
-    private SpanQuery createQuery(String x, String y, int min, int max, boolean isOrdered){
+    private SpanQuery createQuery(String x, String y, int min, int max, boolean isOrdered){    	
     	SpanQuery sq = new SpanDistanceQuery(
         		new SpanTermQuery(new Term("base",x)),
         		new SpanTermQuery(new Term("base",y)),
-        		min,
-        		max,
-        		isOrdered,
+        		new DistanceConstraint(min, max, isOrdered, false),
         		true
         );
     	return sq;
@@ -84,9 +83,7 @@
     	SpanQuery sq = new SpanDistanceQuery(
         		new SpanElementQuery("base",x),
         		new SpanElementQuery("base",y),
-        		min,
-        		max,
-        		isOrdered,
+        		new DistanceConstraint(min, max, isOrdered, false),
         		true
         );
     	return sq;
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceExclusionIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceExclusionIndex.java
index 49c1906..a1ffecf 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceExclusionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceExclusionIndex.java
@@ -11,6 +11,7 @@
 
 import de.ids_mannheim.korap.KorapIndex;
 import de.ids_mannheim.korap.KorapResult;
+import de.ids_mannheim.korap.query.DistanceConstraint;
 import de.ids_mannheim.korap.query.SpanDistanceQuery;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 import de.ids_mannheim.korap.query.SpanNextQuery;
@@ -22,16 +23,13 @@
     
     private SpanQuery createQuery(String e, String x, String y, int min, int max, boolean isOrdered,
     		boolean exclusion){
-    	SpanDistanceQuery sq = new SpanDistanceQuery(    			
-        		new SpanElementQuery("base",e),
+    	SpanElementQuery eq = new SpanElementQuery("base",e);
+    	SpanDistanceQuery sq = new SpanDistanceQuery(        		
         		new SpanTermQuery(new Term("base",x)),
         		new SpanTermQuery(new Term("base",y)),
-        		min,
-        		max,
-        		isOrdered,
-        		true        		
+        		new DistanceConstraint(eq, min, max, isOrdered, exclusion),
+        		true
         );
-    	sq.setExclusion(exclusion);
     	return sq;
     }
 	
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java
index 231e2eb..d8aed3d 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestElementDistanceIndex.java
@@ -12,8 +12,8 @@
 import org.junit.runners.JUnit4;
 
 import de.ids_mannheim.korap.KorapIndex;
-import de.ids_mannheim.korap.KorapQuery;
 import de.ids_mannheim.korap.KorapResult;
+import de.ids_mannheim.korap.query.DistanceConstraint;
 import de.ids_mannheim.korap.query.SpanDistanceQuery;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 import de.ids_mannheim.korap.query.SpanNextQuery;
@@ -82,14 +82,13 @@
 	}
 	
 	public SpanQuery createQuery(String elementType, String x, String y, 
-			int minDistance, int maxDistance, boolean isOrdered){        
-		return new SpanDistanceQuery(
-        		new SpanElementQuery("base", elementType), 
+			int min, int max, boolean isOrdered){
+		
+		SpanElementQuery e = new SpanElementQuery("base", elementType);
+		return new SpanDistanceQuery(        		 
         		new SpanTermQuery(new Term("base",x)), 
         		new SpanTermQuery(new Term("base",y)), 
-        		minDistance, 
-        		maxDistance, 
-        		isOrdered,
+        		new DistanceConstraint( e, min, max, isOrdered, false),
         		true);
 	}
 	
@@ -184,50 +183,5 @@
         assertEquals(5, kr.match(1).endPos);
         
     }	
-	
-	@Test
-	public void testCase5() throws IOException{
-		ki = new KorapIndex();		
-	    ki.addDocFile(getClass().getResource("/a00/SEP-62389.json.gz").getFile(), true);		
-		ki.commit();
 		
-//		KorapQuery kq = new KorapQuery("tokens");
-		SpanQuery sq = new SpanElementQuery("tokens", "s"); 
-//				//kq.seq(kq.tag("s"), kq.tag("s")).toQuery();
-//		System.out.println(sq.toString());
-		//assertEquals("spanNext(<tokens:xip/c:VERB />, <tokens:xip/c:DET />)", sq.toString());
-		kr = ki.search(sq, (short) 100);
-        
-		
-//		for (int i=0; i< kr.getTotalResults(); i++){
-//			System.out.println(
-//				kr.match(i).getLocalDocID()+" "+
-//				kr.match(i).startPos + " " +
-//				kr.match(i).endPos
-//		    );
-//		}
-		//System.out.println(sq.toString());
-		sq = new SpanDistanceQuery(
-				new SpanElementQuery("tokens", "s"),				
-				new SpanTermQuery(new Term("tokens","s:weg")),
-				new SpanTermQuery(new Term("tokens","s:fahren")),
-        		1,
-        		1,
-        		false,
-        		true
-        );   
-        kr = ki.search(sq, (short) 10);
-        
-        
-	/*	System.out.print(kr.getTotalResults()+"\n");
-		for (int i=0; i< kr.getTotalResults(); i++){
-			System.out.println(
-				kr.match(i).getLocalDocID()+" "+
-				kr.match(i).startPos + " " +
-				kr.match(i).endPos
-		    );
-		}*/
-    	   
-	}
-	
 }
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 2136f9c..038b157 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestMultipleDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestMultipleDistanceIndex.java
@@ -36,12 +36,14 @@
 		return new SpanMultipleDistanceQuery(sx, sy, constraints, isOrdered, true);
 	}
 	
-	public DistanceConstraint createConstraint(String unit, int min, int max){		
+	public DistanceConstraint createConstraint(String unit, int min, int max, 
+			boolean isOrdered, boolean exclusion){
+		
 		if (unit.equals("w")){
-			return new DistanceConstraint(min, max,false);
+			return new DistanceConstraint(min, max,isOrdered,exclusion);
 		}		
 		return new DistanceConstraint(new SpanElementQuery("base", unit), 
-				min, max,false);	
+				min, max, isOrdered, exclusion);	
 	}
 	
     private FieldDocument createFieldDoc0() {
@@ -111,11 +113,11 @@
         ki.commit();
         
     	List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
- 	    constraints.add(createConstraint("w", 0, 2));	    
- 	    constraints.add(createConstraint("s", 0, 0));
+ 	    constraints.add(createConstraint("w", 0, 2, false, false));	    
+ 	    constraints.add(createConstraint("s", 0, 0, false, false));
  	    
  	    SpanQuery mdq;	   
-		mdq = createQuery("s:b", "s:c", constraints, false);
+		mdq = createQuery("s:b", "s:c", constraints,false);
 		kr = ki.search(mdq, (short) 10);
 		// System.out.println(mdq);
 		
@@ -139,37 +141,43 @@
         ki.addDoc(createFieldDoc0()); 
         ki.commit();
 		
+        // Ordered - two constraints
 	    List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
-	    constraints.add(createConstraint("w", 0, 2));	    
-	    constraints.add(createConstraint("s", 1, 1));
+	    constraints.add(createConstraint("w", 0, 2, true, false));	    
+	    constraints.add(createConstraint("s", 1, 1, true, false));
 	    
-	    SpanQuery mdq;
-	    // Ordered
-		mdq = createQuery("s:b", "s:c", constraints, true);
-		kr = ki.search(mdq, (short) 10);
-		
+	    SpanQuery mdq;	    
+		mdq = createQuery("s:b", "s:c", constraints,true);
+		kr = ki.search(mdq, (short) 10);		
 	    assertEquals(3, kr.getTotalResults());
 	    assertEquals(0, kr.getMatch(0).getStartPos());
 	    assertEquals(3, kr.getMatch(0).getEndPos());
 	    assertEquals(1, kr.getMatch(1).getStartPos());
 	    assertEquals(3, kr.getMatch(1).getEndPos());
 	    assertEquals(3, kr.getMatch(2).getStartPos());
-	    assertEquals(5, kr.getMatch(2).getEndPos());
-	    
-	    // Unordered
-	    mdq = createQuery("s:c", "s:b", constraints, false);
+	    assertEquals(5, kr.getMatch(2).getEndPos());	   
+	   				
+		// Three constraints
+		constraints.add(createConstraint("p", 0, 0, true, false));		
+		mdq = createQuery("s:b", "s:c", constraints,true);
+		kr = ki.search(mdq, (short) 10);
+		assertEquals(2, kr.getTotalResults());
+		
+		
+		// Unordered - two constraints
+		constraints.clear();
+	    constraints.add(createConstraint("w", 0, 2, false, false));	    
+	    constraints.add(createConstraint("s", 1, 1, false, false));
+	    	   
+	    mdq = createQuery("s:c", "s:b", constraints,false);
 	    kr = ki.search(mdq, (short) 10);
 	    assertEquals(4, kr.getTotalResults());
 	    assertEquals(1, kr.getMatch(2).getStartPos());
 	    assertEquals(4, kr.getMatch(2).getEndPos());
-				
-		// Three constraints
-		constraints.add(createConstraint("p", 0, 0));		
-		mdq = createQuery("s:b", "s:c", constraints, true);
-		kr = ki.search(mdq, (short) 10);
-		assertEquals(2, kr.getTotalResults());
 		
-		mdq = createQuery("s:b", "s:c", constraints, false);
+	    // Three constraints
+		constraints.add(createConstraint("p", 0, 0, false, false));
+		mdq = createQuery("s:b", "s:c", constraints,false);
 		kr = ki.search(mdq, (short) 10);		
 		assertEquals(3, kr.getTotalResults());		
 
@@ -187,11 +195,11 @@
     	ki.commit();
            
        	List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
-	    constraints.add(createConstraint("w", 1, 2));	    
-	    constraints.add(createConstraint("s", 1, 2));
+	    constraints.add(createConstraint("w", 1, 2, false, false));	    
+	    constraints.add(createConstraint("s", 1, 2, false, false));
     	    
 	    SpanQuery mdq;	   
-		mdq = createQuery("s:b", "s:e", constraints, false);
+		mdq = createQuery("s:b", "s:e", constraints,false);
 		kr = ki.search(mdq, (short) 10);		
 
 		assertEquals(5, kr.getTotalResults());		
@@ -228,11 +236,11 @@
     	ki.commit();
     	
     	List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
-	    constraints.add(createConstraint("w", 1, 2));	    
-	    constraints.add(createConstraint("s", 1, 2));
+	    constraints.add(createConstraint("w", 1, 2, false, false));	    
+	    constraints.add(createConstraint("s", 1, 2, false, false));
 		
 	    SpanQuery mdq;	   
-		mdq = createQuery("s:b", "s:c", constraints, false);
+		mdq = createQuery("s:b", "s:c", constraints,false);
 		
 		SpanQuery sq = new SpanNextQuery(mdq, 
 				new SpanTermQuery(new Term("base","s:e")));
@@ -257,11 +265,11 @@
     	ki.commit();
     	
     	List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
-	    constraints.add(createConstraint("w", 1, 2));	    
-	    constraints.add(createConstraint("s", 1, 2));
+	    constraints.add(createConstraint("w", 1, 2, false, false));	    
+	    constraints.add(createConstraint("s", 1, 2, false, false));
 		
 	    SpanQuery mdq;	   
-		mdq = createQuery("s:c", "s:c", constraints, false);
+		mdq = createQuery("s:c", "s:c", constraints,false);
 		kr = ki.search(mdq, (short) 10);
 		
 		assertEquals(4, kr.getTotalResults());
@@ -289,26 +297,28 @@
     	// First constraint - token exclusion
     	SpanQuery sx = new SpanTermQuery(new Term("base","s:b")); 
 		SpanQuery sy = new SpanTermQuery(new Term("base","s:c"));
-		SpanDistanceQuery sq = new SpanDistanceQuery(sx, sy, 0, 1, false, true);
-		sq.setExclusion(true);
+		
+		DistanceConstraint dc1 = createConstraint("w", 0, 1, false, true);
+		SpanDistanceQuery sq = new SpanDistanceQuery(sx, sy, dc1, true);
 		
 		kr = ki.search(sq, (short) 10);
 		assertEquals(1, kr.getTotalResults());
 		// 4-5
 		
     	// Second constraint - element distance
-		sq = new SpanDistanceQuery(new SpanElementQuery("base", "s"), sx,
-				sy, 1, 1, false, true);		
+		DistanceConstraint  dc2 = createConstraint("s", 1, 1, false, false);
+		sq = new SpanDistanceQuery(sx, sy, dc2, true);		
 		kr = ki.search(sq, (short) 10);		
 		// 0-3, 1-3, 1-4, 1-5, 3-7, 4-7
 		assertEquals(6, kr.getTotalResults());
 		
+		
     	List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
-	    constraints.add(new DistanceConstraint(0, 1,true));
-	    constraints.add(createConstraint("s", 1, 1));
+	    constraints.add(dc1);
+	    constraints.add(dc2);
 	    	    
 	    SpanQuery mdq;	   
-		mdq = createQuery("s:b", "s:c", constraints, false);
+		mdq = createQuery("s:b", "s:c", constraints,false);
 		kr = ki.search(mdq, (short) 10);
 		
 		assertEquals(2, kr.getTotalResults());
@@ -320,7 +330,6 @@
     
     
     /** Exclusion, multiple documents
-     * 	wait for element distance exclusion 
      * */
     @Test
     public void testCase7() throws IOException {
@@ -331,36 +340,38 @@
     	SpanQuery sx = new SpanTermQuery(new Term("base","s:b")); 
 		SpanQuery sy = new SpanTermQuery(new Term("base","s:c"));
     	// Second constraint
-		SpanDistanceQuery sq = new SpanDistanceQuery(new SpanElementQuery("base", "s"), sx,
-				sy, 0, 0, false, true);
-    	sq.setExclusion(true);
-		kr = ki.search(sq, (short) 10);	
+		SpanDistanceQuery sq = new SpanDistanceQuery(sx,sy, 
+				createConstraint("s", 0, 0, false, true),
+				true);
+		kr = ki.search(sq, (short) 10);
+		assertEquals(3, kr.getTotalResults());
     	// 0-1, 1-2, 6-7
 				
+	    // Exclusion within the same sentence
     	List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
-	    constraints.add(new DistanceConstraint(0, 2,true));
-	    constraints.add(new DistanceConstraint(new SpanElementQuery("base", "s"), 
-				0, 0,true));
-	    	    
+	    constraints.add(createConstraint("w", 0, 2,false,true));
+	    constraints.add(createConstraint("s", 0, 0,false,true));
+
 	    SpanQuery mdq;	   
-		mdq = createQuery("s:b", "s:c", constraints, false);
-		kr = ki.search(mdq, (short) 10);
-		
+		mdq = createQuery("s:b", "s:c", constraints,false);
+		kr = ki.search(mdq, (short) 10);		
 		assertEquals(2, kr.getTotalResults());
 	    assertEquals(0, kr.getMatch(0).getStartPos());
 	    assertEquals(1, kr.getMatch(0).getEndPos());
 	    assertEquals(6, kr.getMatch(1).getStartPos());
 	    assertEquals(7, kr.getMatch(1).getEndPos());
 		
+	    
 	    // Third constraint
-	    sq = new SpanDistanceQuery(new SpanElementQuery("base", "p"), sx,
-				sy, 0, 0, false, true);
-    	sq.setExclusion(true);
+	    sq = new SpanDistanceQuery(sx, sy, 
+				createConstraint("p", 0, 0, false, true), 
+				true);
+	    kr = ki.search(sq, (short) 10);
+		assertEquals(1, kr.getTotalResults());
 		// 6-7
 	    
-		constraints.add(new DistanceConstraint(new SpanElementQuery("base", "p"), 
-				0, 0,true));
-	    mdq = createQuery("s:b", "s:c", constraints, false);
+		constraints.add(createConstraint("p", 0, 0, false, true));
+	    mdq = createQuery("s:b", "s:c", constraints,false);
 		kr = ki.search(mdq, (short) 10);			
 		
 	    assertEquals(1, kr.getTotalResults());
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestQuantifierIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestRepetitionIndex.java
similarity index 92%
rename from src/test/java/de/ids_mannheim/korap/index/TestQuantifierIndex.java
rename to src/test/java/de/ids_mannheim/korap/index/TestRepetitionIndex.java
index d07718e..02df26c 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestQuantifierIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestRepetitionIndex.java
@@ -13,9 +13,9 @@
 import de.ids_mannheim.korap.KorapIndex;
 import de.ids_mannheim.korap.KorapResult;
 import de.ids_mannheim.korap.query.SpanNextQuery;
-import de.ids_mannheim.korap.query.SpanQuantifierQuery;
+import de.ids_mannheim.korap.query.SpanRepetitionQuery;
 
-public class TestQuantifierIndex {
+public class TestRepetitionIndex {
 	
     private KorapIndex ki;
 	private KorapResult kr;
@@ -91,7 +91,7 @@
         
         SpanQuery sq, sq2;
         // Quantifier only
-        sq = new SpanQuantifierQuery(new SpanTermQuery(new Term("base","s:c")),1,2, true);
+        sq = new SpanRepetitionQuery(new SpanTermQuery(new Term("base","s:c")),1,2, true);
         kr = ki.search(sq, (short) 10);
         // 0-1, 2-3, 2-4, 3-4, 5-6
         assertEquals(5,kr.getTotalResults());        
@@ -99,7 +99,7 @@
         // ec{1,2}
         sq = new SpanNextQuery(
         		new SpanTermQuery(new Term("base", "s:e")),
-        		new SpanQuantifierQuery(new SpanTermQuery(new Term("base","s:c")),1,2, true)
+        		new SpanRepetitionQuery(new SpanTermQuery(new Term("base","s:c")),1,2, true)
     		);
         
         kr = ki.search(sq, (short) 10);
@@ -133,14 +133,14 @@
         ki.commit();
         
         SpanQuery sq;
-        sq = new SpanQuantifierQuery(new SpanTermQuery(new Term("base","s:c")),2,2, true);
+        sq = new SpanRepetitionQuery(new SpanTermQuery(new Term("base","s:c")),2,2, true);
         kr = ki.search(sq, (short) 10);
         assertEquals(4,kr.getTotalResults());
 		
         kr = ki.search(sq, (short) 10); 
         sq = new SpanNextQuery(
         		new SpanTermQuery(new Term("base", "s:e")),
-        		new SpanQuantifierQuery(new SpanTermQuery(new Term("base","s:c")),2,2, true)
+        		new SpanRepetitionQuery(new SpanTermQuery(new Term("base","s:c")),2,2, true)
     		);
         
         kr = ki.search(sq, (short) 10); 
@@ -161,8 +161,8 @@
         sq = new SpanNextQuery(
         		new SpanTermQuery(new Term("base", "s:e")),
         		new SpanOrQuery(
-    				new SpanQuantifierQuery(new SpanTermQuery(new Term("base","s:c")),1,1, true),
-    				new SpanQuantifierQuery(new SpanTermQuery(new Term("base","s:b")),1,1, true)
+    				new SpanRepetitionQuery(new SpanTermQuery(new Term("base","s:c")),1,1, true),
+    				new SpanRepetitionQuery(new SpanTermQuery(new Term("base","s:b")),1,1, true)
 				)
     		);        
         kr = ki.search(sq, (short) 10); 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestUnorderedDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestUnorderedDistanceIndex.java
index 5ef1c96..b9f7065 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestUnorderedDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestUnorderedDistanceIndex.java
@@ -13,6 +13,7 @@
 
 import de.ids_mannheim.korap.KorapIndex;
 import de.ids_mannheim.korap.KorapResult;
+import de.ids_mannheim.korap.query.DistanceConstraint;
 import de.ids_mannheim.korap.query.SpanDistanceQuery;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 import de.ids_mannheim.korap.query.SpanNextQuery;
@@ -79,9 +80,7 @@
     	SpanQuery sq = new SpanDistanceQuery(
         		new SpanTermQuery(new Term("base",x)),
         		new SpanTermQuery(new Term("base",y)),
-        		min,
-        		max,
-        		isOrdered,
+        		new DistanceConstraint(min, max, isOrdered, false),        		
         		true
         );    	 
     	return sq;
@@ -92,9 +91,7 @@
     	SpanQuery sq = new SpanDistanceQuery(
         		new SpanElementQuery("base",x),
         		new SpanElementQuery("base",y),
-        		min,
-        		max,
-        		isOrdered,
+        		new DistanceConstraint(min, max, isOrdered, false),
         		true
         );
     	return sq;
@@ -135,8 +132,7 @@
 	    SpanQuery sq = createQuery("s:c","s:d",1,2,false);                
 	    kr = ki.search(sq, (short) 10);
 	    	    
-	    assertEquals(6, kr.totalResults());
-		
+	    assertEquals(6, kr.totalResults());	    
 	}
 	
 	/** Multiple documents 
@@ -265,12 +261,17 @@
 	    SpanQuery sq2 = new SpanDistanceQuery(
         		sq,
         		new SpanTermQuery(new Term("base","s:e")),
-        		1,
-        		2,
-        		false,
+        		new DistanceConstraint(1, 2, true, false),        		
         		true);
 	    kr = ki.search(sq2, (short) 10);	    
-	    //assertEquals(3, kr.totalResults());		
+	    assertEquals(3, kr.totalResults());	
+	    assertEquals(5, kr.getMatch(0).getStartPos());
+	    assertEquals(9, kr.getMatch(0).getEndPos());
+	    assertEquals(1, kr.getMatch(1).getLocalDocID());
+	    assertEquals(0, kr.getMatch(1).getStartPos());
+	    assertEquals(3, kr.getMatch(1).getEndPos());
+	    assertEquals(0, kr.getMatch(2).getStartPos());
+	    assertEquals(4, kr.getMatch(2).getEndPos());
 	}
 	
 	@Test
@@ -287,9 +288,7 @@
 		SpanQuery sq = new SpanDistanceQuery(
         		new SpanTermQuery(new Term("tokens","s:in")),
         		new SpanTermQuery(new Term("tokens","s:horrendem")),
-        		0,
-        		2,
-        		false,
+        		new DistanceConstraint(0, 2, false, false),        		
         		true
         );   
 		kr = ki.search(sq, (short) 10);
@@ -305,6 +304,8 @@
 	    assertEquals(73, kr.getMatch(2).endPos);	    
 	}
 	
+	/** Multiple NextSpans in the same first span position
+	 * */
 	@Test
 	public void testCase9() throws IOException{
 		ki = new KorapIndex();
@@ -315,16 +316,7 @@
 	    		createQuery("s:c","s:e",1,2,false)
 		);
 	    kr = ki.search(sq, (short) 10);
-//	    
-//	  	System.out.print(kr.getTotalResults()+"\n");
-//			for (int i=0; i< kr.getTotalResults(); i++){
-//				System.out.println(
-//					kr.match(i).getLocalDocID()+" "+
-//					kr.match(i).startPos + " " +
-//					kr.match(i).endPos
-//			    );
-//			}
-	    
+    
 	    assertEquals(3, kr.totalResults());
 	    assertEquals(0,kr.getMatch(1).getStartPos());
 	    assertEquals(4,kr.getMatch(1).getEndPos());
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestUnorderedElementDistanceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestUnorderedElementDistanceIndex.java
index fc9793c..952c325 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestUnorderedElementDistanceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestUnorderedElementDistanceIndex.java
@@ -13,6 +13,7 @@
 
 import de.ids_mannheim.korap.KorapIndex;
 import de.ids_mannheim.korap.KorapResult;
+import de.ids_mannheim.korap.query.DistanceConstraint;
 import de.ids_mannheim.korap.query.SpanDistanceQuery;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 import de.ids_mannheim.korap.query.SpanNextQuery;
@@ -93,14 +94,12 @@
 	}
 	
 	public SpanQuery createQuery(String elementType, String x, String y, 
-			int minDistance, int maxDistance, boolean isOrdered){        
-		return new SpanDistanceQuery(
-        		new SpanElementQuery("base", elementType), 
+			int minDistance, int maxDistance, boolean isOrdered){
+		SpanElementQuery e = new SpanElementQuery("base", elementType);
+		return new SpanDistanceQuery(        		 
         		new SpanTermQuery(new Term("base",x)), 
-        		new SpanTermQuery(new Term("base",y)), 
-        		minDistance, 
-        		maxDistance, 
-        		isOrdered,
+        		new SpanTermQuery(new Term("base",y)),
+        		new DistanceConstraint(e,minDistance, maxDistance, isOrdered, false),
         		true);
 	}
 	
@@ -215,15 +214,28 @@
 
     }
 	
-//	@Test
-//	public void testCase5() throws IOException{
-//		ki = new KorapIndex();		
-//        ki.addDoc(createFieldDoc0());
-//        ki.commit();
-//		SpanQuery sq, edq;
-//        edq = createQuery("s", "s:b", "s:c", 0, 2,false);
-//        kr = ki.search(edq, (short) 10);
-//        
+	/** Next */
+	@Test
+	public void testCase5() throws IOException{
+		ki = new KorapIndex();		
+        ki.addDoc(createFieldDoc0());
+        ki.commit();
+		
+        SpanQuery sq, edq;
+        edq = createQuery("s", "s:b", "s:c", 0, 2,false);
+        kr = ki.search(edq, (short) 10);
+        assertEquals(6, kr.getTotalResults());
+
+        sq = new SpanNextQuery( 
+        		new SpanTermQuery(new Term("base", "s:b"))
+        		,edq);
+        kr = ki.search(sq, (short) 10);
+        assertEquals(2, kr.getTotalResults());
+        assertEquals(1, kr.getMatch(0).getStartPos());
+        assertEquals(4, kr.getMatch(0).getEndPos());
+        assertEquals(2, kr.getMatch(1).getStartPos());
+        assertEquals(5, kr.getMatch(1).getEndPos());
+        
 //        System.out.print(kr.getTotalResults()+"\n");
 //		for (int i=0; i< kr.getTotalResults(); i++){
 //			System.out.println(
@@ -232,33 +244,5 @@
 //				kr.match(i).endPos
 //		    );
 //		}
-////		
-////		System.out.println("h");
-////		sq = new SpanTermQuery(new Term("base", "s:b"));
-////		
-////		kr = ki.search(sq, (short) 10);
-////        
-////        System.out.print(kr.getTotalResults()+"\n");
-////		for (int i=0; i< kr.getTotalResults(); i++){
-////			System.out.println(
-////				kr.match(i).getLocalDocID()+" "+
-////				kr.match(i).startPos + " " +
-////				kr.match(i).endPos
-////		    );
-////		}
-//		
-//        sq = new SpanNextQuery( 
-//        		new SpanTermQuery(new Term("base", "s:b"))
-//        		,edq);
-//        kr = ki.search(sq, (short) 10);
-//        
-//        System.out.print(kr.getTotalResults()+"\n");
-//		for (int i=0; i< kr.getTotalResults(); i++){
-//			System.out.println(
-//				kr.match(i).getLocalDocID()+" "+
-//				kr.match(i).startPos + " " +
-//				kr.match(i).endPos
-//		    );
-//		}
-//	}
+	}
 }
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestWPDIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestWPDIndex.java
index 0f897d8..a5a945e 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestWPDIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestWPDIndex.java
@@ -19,10 +19,11 @@
 import de.ids_mannheim.korap.KorapResult;
 import de.ids_mannheim.korap.KorapSearch;
 import de.ids_mannheim.korap.filter.BooleanFilter;
+import de.ids_mannheim.korap.query.DistanceConstraint;
 import de.ids_mannheim.korap.query.SpanDistanceQuery;
 import de.ids_mannheim.korap.query.SpanElementQuery;
 import de.ids_mannheim.korap.query.SpanNextQuery;
-import de.ids_mannheim.korap.query.SpanQuantifierQuery;
+import de.ids_mannheim.korap.query.SpanRepetitionQuery;
 
 public class TestWPDIndex {
 	long start, end;
@@ -31,26 +32,23 @@
 	KorapSearch ks;
 	
 	private SpanDistanceQuery createElementDistanceQuery(String e, String x, String y, 
-			int min, int max, boolean isOrdered){
-		SpanDistanceQuery sq = new SpanDistanceQuery(	
-				new SpanElementQuery("tokens", "s"),
+			int min, int max, boolean isOrdered, boolean exclusion){
+		SpanElementQuery eq = new SpanElementQuery("tokens", e);
+		SpanDistanceQuery sq = new SpanDistanceQuery(
 				new SpanTermQuery(new Term("tokens",x)),
 				new SpanTermQuery(new Term("tokens",y)),
-        		min,
-        		max,
-        		isOrdered,
+				new DistanceConstraint(eq, min, max, isOrdered, exclusion),
         		true
         ); 
 		return sq;
 	}
 	
-	private SpanDistanceQuery createDistanceQuery(String x, String y, int min, int max, boolean isOrdered){
+	private SpanDistanceQuery createDistanceQuery(String x, String y, int min, int max, 
+			boolean isOrdered, boolean exclusion){
 		SpanDistanceQuery sq = new SpanDistanceQuery(
         		new SpanTermQuery(new Term("tokens",x)),
         		new SpanTermQuery(new Term("tokens",y)),
-        		min,
-        		max,
-        		isOrdered,
+        		new DistanceConstraint(min, max, isOrdered, exclusion),
         		true
         );
     	return sq;
@@ -71,18 +69,18 @@
 	public void testCase1() throws IOException{
 		SpanDistanceQuery sq;
 		// ordered
-		sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, true);
+		sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, true,false);
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(8, kr.getTotalResults());
 
 		// unordered
-		sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, false);				
+		sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, false,false);				
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(11, kr.getTotalResults());
 		
-		sq = createDistanceQuery("s:kommen", "s:Wir", 1, 1, false);				
+		sq = createDistanceQuery("s:kommen", "s:Wir", 1, 1, false,false);				
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(11, kr.getTotalResults());
@@ -99,15 +97,13 @@
 		
 		SpanDistanceQuery sq;
 		// ordered
-		sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, true);				
-		sq.setExclusion(true);
+		sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, true, true);				
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(1899, kr.getTotalResults());
 		
 		// unordered
-		sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, false);				
-		sq.setExclusion(true);
+		sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, false, true);				
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(1896, kr.getTotalResults());	
@@ -123,19 +119,20 @@
 	@Test
 	public void testCase3() throws IOException{
 		// ordered
-		SpanDistanceQuery sq = createElementDistanceQuery("s","s:weg", "s:fahren", 0, 1, true);
+		SpanDistanceQuery sq = createElementDistanceQuery("s","s:weg", "s:fahren", 
+				0, 1, true, false);
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);		
 		assertEquals(3,kr.getTotalResults());
 		
 		// unordered
-		sq = createElementDistanceQuery("s","s:weg", "s:fahren", 0, 1, false);
+		sq = createElementDistanceQuery("s","s:weg", "s:fahren", 0, 1, false,false);
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(5,kr.getTotalResults());
 		
 		// only 0
-		sq = createElementDistanceQuery("s","s:weg", "s:fahren", 0, 0, false);
+		sq = createElementDistanceQuery("s","s:weg", "s:fahren", 0, 0, false,false);
 		kr = ki.search(sq, (short) 100);
 		assertEquals(2,kr.getTotalResults());
 		assertEquals("WPD_BBB.04463", kr.match(0).getDocID());
@@ -146,7 +143,7 @@
 		assertEquals(451,kr.getMatch(1).getEndPos());
 		
 		// only 1
-		sq = createElementDistanceQuery("s","s:weg", "s:fahren", 1, 1, false);
+		sq = createElementDistanceQuery("s","s:weg", "s:fahren", 1, 1, false,false);
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(3,kr.getTotalResults());		
@@ -155,9 +152,7 @@
 	/** Element distance exclusion */
 	@Test
 	public void testCase4() throws IOException{
-		SpanDistanceQuery sq = createElementDistanceQuery("s","s:weg", "s:fahren", 1, 1, false);
-		sq.setExclusion(true);
-		
+		SpanDistanceQuery sq = createElementDistanceQuery("s","s:weg", "s:fahren", 1, 1, false, true);
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(979,kr.getTotalResults());
@@ -178,18 +173,18 @@
 	@Test
 	public void testCase5() throws IOException{
 		SpanQuery sq;
-		sq = new SpanQuantifierQuery(new SpanTermQuery(new Term("tokens","mate/p:ADJA")),1,2, true);
+		sq = new SpanRepetitionQuery(new SpanTermQuery(new Term("tokens","mate/p:ADJA")),1,2, true);
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(4116416, kr.getTotalResults());
 		//0.9s
 		
-		sq = new SpanQuantifierQuery(new SpanTermQuery(new Term("tokens","mate/p:ADJA")),1,1, true);
+		sq = new SpanRepetitionQuery(new SpanTermQuery(new Term("tokens","mate/p:ADJA")),1,1, true);
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(3879671, kr.getTotalResults());
 		
-		sq = new SpanQuantifierQuery(new SpanTermQuery(new Term("tokens","mate/p:ADJA")),2,2, true);
+		sq = new SpanRepetitionQuery(new SpanTermQuery(new Term("tokens","mate/p:ADJA")),2,2, true);
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);
 		assertEquals(236745, kr.getTotalResults());
@@ -201,7 +196,7 @@
 	public void testCase6() throws IOException{
 		SpanQuery sq = new SpanNextQuery(
         		new SpanTermQuery(new Term("tokens", "tt/p:NN")),
-        		new SpanQuantifierQuery(new SpanTermQuery(new Term("tokens","mate/p:ADJA")),2,2, true)
+        		new SpanRepetitionQuery(new SpanTermQuery(new Term("tokens","mate/p:ADJA")),2,2, true)
     		);
 		ks = new KorapSearch(sq);
 		kr = ks.run(ki);