Fixed matching id problem for RelationPartSpans, 
updated special character payload starting with _0
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanRelationPartQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanRelationPartQuery.java
index bcdfca8..97f1de0 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanRelationPartQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanRelationPartQuery.java
@@ -29,9 +29,9 @@
 	public SpanRelationPartQuery(SpanRelationQuery spanRelationQuery,
 			SpanWithIdQuery secondClause, // match tokenWithIdQuery, ElementQuery, RelationQuery
 			boolean matchRight,
-			boolean returnRight,
+			boolean inverseRelation,
 			boolean collectPayloads) {
-		this(spanRelationQuery, secondClause, elementStr, matchRight, returnRight, collectPayloads);		
+		this(spanRelationQuery, secondClause, elementStr, matchRight, inverseRelation, collectPayloads);		
 	}
 	
 	public SpanRelationPartQuery(SpanRelationQuery spanRelationQuery,
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 2442f04..9d0352d 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
@@ -15,7 +15,7 @@
 	private Collection<byte[]> payloads = new ArrayList<>();
 	private int position;
 	private CandidateSpans childSpan; // used for multiple distance with unordered constraint 
-	private short elementRef;
+	protected short spanId;
 	
 	
 	public CandidateSpans(Spans span) throws IOException {
@@ -108,11 +108,11 @@
 	}
 
 	public short getSpanId() {
-		return elementRef;
+		return spanId;
 	}
 
 	public void setSpanId(short elementRef) {
-		this.elementRef = elementRef;
+		this.spanId = elementRef;
 	}
 
 	@Override
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/RelationPartSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/RelationPartSpans.java
index d3889f7..20fe1de 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/RelationPartSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/RelationPartSpans.java
@@ -73,23 +73,7 @@
 	protected boolean advance() throws IOException {
 		while (candidateRelations.size() > 0 || hasMoreSpans){
 			if (candidateRelations.size() > 0){
-				CandidateRelationSpan relationSpan = candidateRelations.get(0);				
-				matchDocNumber = relationSpan.getDoc();
-				if (!inverse){
-					matchStartPosition = relationSpan.getStart();
-					matchEndPosition = relationSpan.getEnd();
-					setRightStart(relationSpan.getRightStart());
-					setRightEnd(relationSpan.getRightEnd());
-				}					
-				else{ // maybe a bit confusing -- inverse relation
-					matchStartPosition = relationSpan.getRightStart();
-					matchEndPosition = relationSpan.getRightEnd();
-					setRightStart(relationSpan.getStart());
-					setRightEnd(relationSpan.getEnd());
-				}
-				setLeftId(relationSpans.getLeftId());
-				setRightId(relationSpan.getRightId());
-				setSpanId(relationSpan.getSpanId());
+				setMatchSpan(candidateRelations.get(0));
 				candidateRelations.remove(0);
 				return true;
 			}
@@ -100,7 +84,27 @@
 		}		
 		return false;
 	}
-
+	
+	private void setMatchSpan(CandidateRelationSpan relationSpan) {
+		matchDocNumber = relationSpan.getDoc();
+		if (!inverse){
+			matchStartPosition = relationSpan.getStart();
+			matchEndPosition = relationSpan.getEnd();
+			setRightStart(relationSpan.getRightStart());
+			setRightEnd(relationSpan.getRightEnd());
+		}					
+		else{ // maybe a bit confusing -- inverse relation
+			matchStartPosition = relationSpan.getRightStart();
+			matchEndPosition = relationSpan.getRightEnd();
+			setRightStart(relationSpan.getStart());
+			setRightEnd(relationSpan.getEnd());
+		}
+		
+		setLeftId(relationSpan.getLeftId());
+		setRightId(relationSpan.getRightId());
+		setSpanId(relationSpan.getSpanId());
+	}
+	
 	/** A window starts at the same token position as a relation span, 
 	 * 	and ends at the start + window length.
 	 * */
@@ -172,7 +176,8 @@
 			else{
 				/*System.out.println(r.getStart()+","+r.getEnd()+" "+
 						r.getRightStart()+","+r.getRightEnd()+" "
-						+matcheeSpans.start()+","+matcheeSpans.end());*/
+						+matcheeSpans.start()+","+matcheeSpans.end()+
+						" #"+matcheeSpans.getSpanId());*/
 				i = matchRelation(i, r,r.getStart(), r.getEnd());
 			}			
 		}
@@ -180,14 +185,26 @@
 		hasMoreSpans &= hasMoreMatchees;
 	}
 	
-	private int matchRelation(int i, CandidateRelationSpan r, int startPos, int endPos) throws IOException {
+	private int matchRelation(int i, CandidateRelationSpan r, int startPos, 
+			int endPos) throws IOException {
+		
 		if(startPos == matcheeSpans.start() ){
-			if 	(endPos == matcheeSpans.end()){		
-				if (!inverse && r.getRightId() == matcheeSpans.getSpanId()){
+			if 	(endPos == matcheeSpans.end()){
+				
+				int id;
+				if ( matcheeSpans instanceof RelationPartSpans){
+					if (matchRight) {
+						id = ((RelationPartSpans) matcheeSpans).getRightId();
+					}
+					else { id = ((RelationPartSpans) matcheeSpans).getLeftId(); }				
+				}
+				else { id = matcheeSpans.getSpanId(); }
+				
+				if (!inverse && r.getRightId() == id){
 					r.sortRight = false;
 					candidateRelations.add(r);
 				}
-				else if (inverse && r.getLeftId() == matcheeSpans.getSpanId()) {
+				else if (inverse && r.getLeftId() == id) {
 					r.sortRight = true;
 					candidateRelations.add(r);
 				}
@@ -224,22 +241,6 @@
 		// TODO Auto-generated method stub
 		return 0;
 	}
-	
-	/*public short getLeftId() {
-		return leftId;
-	}
-
-	public void setLeftId(short leftId) {
-		this.leftId = leftId;
-	}
-
-	public short getRightId() {
-		return rightId;
-	}
-
-	public void setRightId(short rightId) {
-		this.rightId = rightId;
-	}*/
 
 	class CandidateRelationSpan extends CandidateSpans implements Comparable<CandidateSpans>{
 		
@@ -247,6 +248,7 @@
 		private short leftId, rightId;
 		private boolean sortRight;
 		
+		
 		public CandidateRelationSpan(RelationBaseSpans span, boolean sortRight) 
 				throws IOException {
 			super(span);
@@ -255,6 +257,7 @@
 			this.sortRight = sortRight;
 			this.leftId = span.getLeftId();
 			this.rightId = span.getRightId();
+			this.spanId = span.getSpanId();
 		}
 		
 		@Override
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestRelationIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestRelationIndex.java
index ace4662..c44ffee 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestRelationIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestRelationIndex.java
@@ -71,16 +71,16 @@
         fd.addString("ID", "doc-0");
         fd.addTV("base",
             "text",             
-            "[(0-1)s:c$<s>1|_1#0-1|>:xip/syntax-dep_rel$<i>6<s>1<s>1<s>1]" +
-            "[(1-2)s:e$<s>1|_2#1-2|<:xip/syntax-dep_rel$<i>9<s>1<s>1<s>1|>:xip/syntax-dep_rel$<i>4<s>1<s>1<s>1]" +             
-            "[(2-3)s:c|_3#2-3]" +
-            "[(3-4)s:c$<s>1|s:b$<s>2|_4#3-4|<:xip/syntax-dep_rel$<i>9<s>1<s>1<s>1]" + 
-            "[(4-5)s:e$<s>1|s:d$<s>2|_5#4-5|<:xip/syntax-dep_rel$<i>1<s>1<s>1<s>1]" +
-            "[(5-6)s:c|_6#5-6]" +
-            "[(6-7)s:d$<s>1|_7#6-7|<:xip/syntax-dep_rel$<i>1<s>1<s>1<s>1]" +
-            "[(7-8)s:e|_8#7-8]" + 
-            "[(8-9)s:e|s:b|_9#8-9]" + 
-            "[(9-10)s:d$<s>1|_10#9-10|>:xip/syntax-dep_rel$<i>1<s>2<s>1<s>1|>:xip/syntax-dep_rel$<i>3<s>1<s>1<s>1]");
+            "[(0-1)s:c$<s>1|_0#0-1|>:xip/syntax-dep_rel$<i>6<s>1<s>1<s>1]" +
+            "[(1-2)s:e$<s>1|_1#1-2|<:xip/syntax-dep_rel$<i>9<s>1<s>1<s>1|>:xip/syntax-dep_rel$<i>4<s>1<s>1<s>1]" +             
+            "[(2-3)s:c|_2#2-3]" +
+            "[(3-4)s:c$<s>1|s:b$<s>2|_3#3-4|<:xip/syntax-dep_rel$<i>9<s>1<s>1<s>1]" + 
+            "[(4-5)s:e$<s>1|s:d$<s>2|_4#4-5|<:xip/syntax-dep_rel$<i>1<s>1<s>1<s>1]" +
+            "[(5-6)s:c|_5#5-6]" +
+            "[(6-7)s:d$<s>1|_6#6-7|<:xip/syntax-dep_rel$<i>1<s>1<s>1<s>1]" +
+            "[(7-8)s:e|_7#7-8]" + 
+            "[(8-9)s:e|s:b|_8#8-9]" + 
+            "[(9-10)s:d$<s>1|_9#9-10|>:xip/syntax-dep_rel$<i>1<s>2<s>1<s>1|>:xip/syntax-dep_rel$<i>3<s>1<s>1<s>1]");
         return fd;
     }
 	
@@ -89,23 +89,23 @@
         fd.addString("ID", "doc-1");
         fd.addTV("base",
             "text",             
-            "[(0-1)s:c$<s>2|<>:p$#0-3$<i>3<s>1|_1#0-1|" +
+            "[(0-1)s:c$<s>2|<>:p$#0-3$<i>3<s>1|_0#0-1|" +
             		">:xip/syntax-dep_rel$<i>3<i>6<i>9<s>2<s>1<s>1|" +
             		">:xip/syntax-dep_rel$<i>6<i>9<s>1<s>2<s>1|" +
             		"r@:func=subj$<s>2]" +
-            "[(1-2)s:e|_2#1-2|<>:p#1-3$<i>3<s>1]" +             
-            "[(2-3)s:c|_3#2-3]" +
-            "[(3-4)s:c|s:b|_4#3-4]" + 
-            "[(4-5)s:e|s:d|_5#4-5]" +
-            "[(5-6)s:c|_6#5-6]" +
-            "[(6-7)s:d$<s>2|<>:p$#6-9$<i>9<s>1|_7#6-7|" +
+            "[(1-2)s:e|_1#1-2|<>:p#1-3$<i>3<s>1]" +             
+            "[(2-3)s:c|_2#2-3]" +
+            "[(3-4)s:c|s:b|_3#3-4]" + 
+            "[(4-5)s:e|s:d|_4#4-5]" +
+            "[(5-6)s:c|_5#5-6]" +
+            "[(6-7)s:d$<s>2|<>:p$#6-9$<i>9<s>1|_6#6-7|" +
             	"<:xip/syntax-dep_rel$<i>9<b>0<i>1<s>1<s>1<s>2|" +
             	">:xip/syntax-dep_rel$<i>9<b>0<i>9<s>3<s>1<s>1|" +
             	"<:xip/syntax-dep_rel$<i>9<i>1<i>3<s>2<s>1<s>1|" +
             	"r@:func=obj$<s>2]" +
-            "[(7-8)s:e|_8#7-8]" + 
-            "[(8-9)s:e|s:b|_9#8-9]" + 
-            "[(9-10)s:d$<s>1|_10#9-10|<:xip/syntax-dep_rel$<i>6<i>9<s>2<s>1<s>1]");
+            "[(7-8)s:e|_7#7-8]" + 
+            "[(8-9)s:e|s:b|_8#8-9]" + 
+            "[(9-10)s:d$<s>1|_9#9-10|<:xip/syntax-dep_rel$<i>6<i>9<s>2<s>1<s>1]");
         return fd;
     }
 	
@@ -114,7 +114,7 @@
         fd.addString("ID", "doc-2");
         fd.addTV("base",
             "Ich kaufe die Blümen für meine Mutter.",             
-            "[(0-3)s:Ich|_1#0-3|pos:NN$<s>1|<>:s#0-38$<i>7<s>2|<>:np#0-3$<i>1<s>3|" +
+            "[(0-3)s:Ich|_0#0-3|pos:NN$<s>1|<>:s#0-38$<i>7<s>2|<>:np#0-3$<i>1<s>3|" +
             	">:child-of$<i>0<i>7<s>1<s>3<s>2|" +
             	">:child-of$<i>0<i>1<s>6<s>1<s>3|" +
             	"<:child-of$<i>1<b>0<i>1<s>3<s>3<s>1|" +
@@ -123,7 +123,7 @@
             	"<:dep$<i>2<s>2<s>1<s>1|" +
             	"r@:func=sbj$<s>1]" +
             	
-            "[(1-2)s:kaufe|_2#4-9|pos:V$<s>1|<>:vp#4-38$<i>7<s>2|" +
+            "[(1-2)s:kaufe|_1#4-9|pos:V$<s>1|<>:vp#4-38$<i>7<s>2|" +
             	">:child-of$<i>7<i>0<i>7<s>1<s>2<s>2|" +
             	">:child-of$<i>1<i>7<s>2<s>1<s>2|" +
             	"<:child-of$<i>7<b>0<i>2<s>5<s>2<s>1|" +
@@ -131,7 +131,7 @@
             	">:dep$<i>1<s>3<s>1<s>1|" +
             	">:dep$<i>4<s>4<s>1<s>1]" +
             	
-            "[(2-3)s:die|_3#10-13|pos:ART$<s>1|tt:DET$<s>2|<>:np#10-20$<i>4<s>3|<>:np#10-38$<i>7<s>4|" +
+            "[(2-3)s:die|_2#10-13|pos:ART$<s>1|tt:DET$<s>2|<>:np#10-20$<i>4<s>3|<>:np#10-38$<i>7<s>4|" +
             	">:child-of$<i>4<i>2<i>7<s>1<s>3<s>4|" +
             	">:child-of$<i>2<i>4<s>2<s>1<s>3|" +
             	">:child-of$<i>7<i>1<i>7<s>2<s>4<s>2|" +
@@ -143,14 +143,14 @@
             	"r@:func=obj$<s>1" +
             	"]" +
             
-            "[(3-4)s:Blümen|_4#14-20|pos:NN$<s>1|" +
+            "[(3-4)s:Blümen|_3#14-20|pos:NN$<s>1|" +
             	">:child-of$<i>2<i>4<s>1<s>1<s>3|" +            	
             	"<:dep$<i>2<s>2<s>1<s>2|" +
             	">:dep$<i>3<s>3<s>1<s>1|" +
             	">:dep$<i>5<s>4<s>1<s>1|" +
             	"r@:func=head$<s>2]" +
             	
-            "[(4-5)s:für|_5#21-24|pos:PREP$<s>1|<>:pp#21-38$<i>7<s>2|" +
+            "[(4-5)s:für|_4#21-24|pos:PREP$<s>1|<>:pp#21-38$<i>7<s>2|" +
             	">:child-of$<i>4<i>7<s>1<s>1<s>2|" +
             	">:child-of$<i>7<i>2<i>7<s>2<s>2<s>4|" +
             	"<:child-of$<i>7<b>0<i>5<s>4<s>2<s>1|" +
@@ -159,14 +159,14 @@
             	">:dep$<i>7<s>3<s>1<s>1" +
             	"]" +
             
-            "[(5-6)s:meine|_6#25-30|pos:ART$<s>1|<>:np#25-38$<i>7<s>2|" +
+            "[(5-6)s:meine|_5#25-30|pos:ART$<s>1|<>:np#25-38$<i>7<s>2|" +
             	">:child-of$<i>5<i>7<s>1<s>1<s>2|" +
             	">:child-of$<i>7<i>4<i>7<s>2<s>2<s>2|" +
             	"<:child-of$<i>7<b>0<i>6<s>4<s>2<s>1|" +
             	"<:child-of$<i>7<b>0<i>7<s>5<s>2<s>1|" +
             	"<:dep$<i>7<s>3<s>1<s>1" +
             	"]" +
-            "[(6-7)s:Mutter.|_7#31-38|pos:NN$<s>1|" +
+            "[(6-7)s:Mutter.|_6#31-38|pos:NN$<s>1|" +
             	">:child-of$<i>5<i>7<s>1<s>1<s>2|" +
             	">:dep$<i>6<s>2<s>1<s>1|" +
             	"<:dep$<i>5<s>3<s>1<s>1|" +
@@ -521,7 +521,7 @@
 		System.out.println(km.getStartPos() +","+km.getEndPos()+" "
 			+km.getSnippetBrackets());
 		}*/		
-
+		assertEquals(8, kr.getTotalResults());
 	}
 	
 	/** Relations whose source/target do not embed 
@@ -632,13 +632,21 @@
 				new SpanTermWithIdQuery(new Term("base","pos:NN"),true), 
 				3, false, true, true);
 		kr = ki.search(rv,(short) 10);
-		assertEquals(3, kr.getTotalResults());
 		
 		/*for (KorapMatch km : kr.getMatches()){
 			System.out.println(km.getStartPos() +","+km.getEndPos()
-				//+" "+km.getSnippetBrackets()
+				+" "+km.getSnippetBrackets()
 				);
 		}*/
+		
+		assertEquals(3, kr.getTotalResults());
+		assertEquals(1,kr.getMatch(0).getStartPos());
+		assertEquals(2,kr.getMatch(0).getEndPos());
+		assertEquals(1,kr.getMatch(1).getStartPos());
+		assertEquals(2,kr.getMatch(1).getEndPos());
+		assertEquals(4,kr.getMatch(2).getStartPos());
+		assertEquals(5,kr.getMatch(2).getEndPos());
+		
 	}
 }