Mirror collection

Change-Id: Ib597547f09eaa1191b2c43f0d818d0f3002114c7
diff --git a/Changes b/Changes
index 3b86823..d9ed869 100644
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.52 2015-06-17
+0.52 2015-06-23
         - [bugfix] Fixed payload filtering in FocusSpans (margaretha)
 	- [workaround] Reintroduced empty collection support,
 	  as Koral still creates them (diewald)
@@ -7,6 +7,7 @@
 	- [feature] Made responses valid KoralQueries (diewald)
 	- [bugfix] Fixed sequence deserialization bug for simple unordered
 	  constraint (diewald)
+	- [bugfix] Mirror collection (diewald)
 
 0.51 2015-03-17
         - This is a major version (prepared for the GitHub release)
diff --git a/src/main/java/de/ids_mannheim/korap/Krill.java b/src/main/java/de/ids_mannheim/korap/Krill.java
index 112112d..8a5789a 100644
--- a/src/main/java/de/ids_mannheim/korap/Krill.java
+++ b/src/main/java/de/ids_mannheim/korap/Krill.java
@@ -166,7 +166,6 @@
         if (json.has("query")) {
             try {
                 KrillQuery kq = new KrillQuery("tokens");
-
                 SpanQueryWrapper qw = kq.fromJson(json.get("query"));
 
                 this.setQuery(kq);
@@ -209,11 +208,13 @@
         try {
             if (json.has("collection")) {
                 JsonNode collNode = json.get("collection");
+
                 // TODO: Temporary
-                if (collNode.fieldNames().hasNext())
-                    this.setCollection(
-                        new KrillCollection().fromJson(collNode)
-                    );
+                if (collNode.fieldNames().hasNext()) {
+                    KrillCollection kc = new KrillCollection()
+                            .fromJson(collNode);
+                    this.setCollection(kc);
+                };
             }
 
             // <legacycode>
@@ -303,14 +304,16 @@
 
         // Apply search
         else {
+
+            // This contains meta and matches
             kr = this.index.search(this);
-            this.getCollection().setIndex(this.index);
+            // this.getCollection().setIndex(this.index);
             kr.copyNotificationsFrom(this);
         };
 
-        // TODO: Only for development mode
-        kr.setRequest(this.request);
+        // kr.setRequest(this.request);
         kr.setQuery(this.getQuery());
+        kr.setCollection(this.getCollection());
 
         return kr;
     };
diff --git a/src/main/java/de/ids_mannheim/korap/KrillCollection.java b/src/main/java/de/ids_mannheim/korap/KrillCollection.java
index 539427b..5d7d736 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillCollection.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillCollection.java
@@ -59,6 +59,7 @@
     private String id;
     private ArrayList<FilterOperation> filter;
     private int filterCount = 0;
+    private JsonNode json;
 
     // Logger
     private final static Logger log = LoggerFactory
@@ -158,6 +159,7 @@
      * @throws QueryException
      */
     public KrillCollection fromJson (JsonNode json) throws QueryException {
+        this.json = json;
         this.filter(this._fromJson(json));
         return this;
     };
@@ -617,6 +619,22 @@
 
 
     /**
+     * Return the associated KoralQuery collection object
+     * as a {@link JsonNode}. This won't work,
+     * if the object was build using a CollectionBuilder,
+     * therefore it is limited to mirror a deserialized KoralQuery
+     * object.
+     * 
+     * @return The {@link JsonNode} representing the collection object
+     *         of a deserialized KoralQuery object.
+     */
+    public JsonNode toJsonNode () {
+        return this.json;
+    };
+
+
+
+    /**
      * Search in the virtual collection.
      * This is mostly used for testing purposes
      * and <strong>is not recommended</strong>
diff --git a/src/main/java/de/ids_mannheim/korap/KrillQuery.java b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
index 3fe6f28..3bf8bcd 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillQuery.java
@@ -121,6 +121,7 @@
         };
     };
 
+
     /**
      * Constructs a new object for query deserialization
      * and building. Expects the name of an index field
@@ -281,12 +282,14 @@
                     SpanSubspanQueryWrapper ssqw = new SpanSubspanQueryWrapper(
                             sqw, startOffset, length);
                     return ssqw;
-                };
+                }
+                ;
 
                 if (DEBUG)
                     log.trace("Wrap class reference {}", number);
 
-                return new SpanFocusQueryWrapper(this._fromJson(operands.get(0)), number);
+                return new SpanFocusQueryWrapper(
+                        this._fromJson(operands.get(0)), number);
 
             case "koral:token":
 
@@ -384,7 +387,8 @@
         if (DEBUG)
             log.trace("Operands are {}", operands);
 
-        SpanQueryWrapper spanReferenceQueryWrapper = _operationReferenceFromJSON(json, operands);
+        SpanQueryWrapper spanReferenceQueryWrapper = _operationReferenceFromJSON(
+                json, operands);
         if (spanReferenceQueryWrapper != null) {
             return spanReferenceQueryWrapper;
         }
@@ -428,8 +432,9 @@
         throw new QueryException(711, "Unknown group operation");
     };
 
-    private SpanQueryWrapper _operationReferenceFromJSON(JsonNode node, JsonNode operands)
-            throws QueryException {
+
+    private SpanQueryWrapper _operationReferenceFromJSON (JsonNode node,
+            JsonNode operands) throws QueryException {
         boolean isReference = false;
         int classNum = -1;
         int refOperandNum = -1;
@@ -464,7 +469,8 @@
         return null;
     }
 
-    private JsonNode _resolveReference(JsonNode node, JsonNode operands,
+
+    private JsonNode _resolveReference (JsonNode node, JsonNode operands,
             int refOperandNum, int classNum) throws QueryException {
         JsonNode referent = null;
         ObjectMapper m = new ObjectMapper();
@@ -474,7 +480,8 @@
             if (i != refOperandNum) {
                 if (!isReferentFound) {
                     referent = _extractReferentClass(operands.get(i), classNum);
-                    if (referent != null) isReferentFound = true;
+                    if (referent != null)
+                        isReferentFound = true;
                 }
                 newOperands.insert(i, operands.get(i));
             }
@@ -490,7 +497,8 @@
 
     }
 
-    private JsonNode _extractReferentClass(JsonNode node, int classNum) {
+
+    private JsonNode _extractReferentClass (JsonNode node, int classNum) {
         JsonNode referent;
         if (node.has("classOut") && node.get("classOut").asInt() == classNum) {
             // System.out.println("found: " + node.toString());
@@ -520,7 +528,7 @@
 
         SpanQueryWrapper operand1 = this._fromJson(operands.get(0));
         SpanQueryWrapper operand2 = this._fromJson(operands.get(1));
-        
+
         String direction = ">:";
         if (operand1.isEmpty() && !operand2.isEmpty()) {
             direction = "<:";
@@ -529,14 +537,13 @@
         if (!relation.has("@type"))
             throw new QueryException(701,
                     "JSON-LD group has no @type attribute");
-        
+
         if (relation.get("@type").asText().equals("koral:relation")) {
             if (!relation.has("wrap")) {
                 throw new QueryException(718, "Missing relation term");
             }
             SpanQueryWrapper relationWrapper = _termFromJson(
-                    relation.get("wrap"),
-                    direction);
+                    relation.get("wrap"), direction);
             return new SpanRelationWrapper(relationWrapper, operand1, operand2);
         }
         else {
@@ -730,7 +737,8 @@
                     "Span references are currently not supported");
         };
 
-        return new SpanFocusQueryWrapper(this._fromJson(operands.get(0)), number);
+        return new SpanFocusQueryWrapper(this._fromJson(operands.get(0)),
+                number);
     };
 
 
@@ -1039,7 +1047,7 @@
 
         // <legacy>
         if (json.has("caseInsensitive")
-            && json.get("caseInsensitive").asBoolean()) {
+                && json.get("caseInsensitive").asBoolean()) {
             isCaseInsensitive = true;
         }
         // </legacy>
diff --git a/src/main/java/de/ids_mannheim/korap/index/MultiTerm.java b/src/main/java/de/ids_mannheim/korap/index/MultiTerm.java
index ec5079b..b009e7b 100644
--- a/src/main/java/de/ids_mannheim/korap/index/MultiTerm.java
+++ b/src/main/java/de/ids_mannheim/korap/index/MultiTerm.java
@@ -304,7 +304,7 @@
      * Offsets are attached following a hash sign,
      * payloads are attached following a dollar sign.
      * All payloads are written as UTF-8 character sequences.
-     *
+     * 
      * <b>For the moment this is only for testing purposes!</b>
      * 
      * @see #toStringShort().
@@ -457,26 +457,29 @@
                 }
                 catch (NumberFormatException e) {
                     throw new CorpusDataException(952,
-                            "Given offset information is not numeric in " + termSurface[0]);
+                            "Given offset information is not numeric in "
+                                    + termSurface[0]);
                 };
             }
             else {
                 throw new CorpusDataException(953,
-                        "Given offset information is incomplete in " + termSurface[0]);
+                        "Given offset information is incomplete in "
+                                + termSurface[0]);
             };
         };
         this.term = _unescape(stringOffset[0]);
     };
 
+
     // Escape the term
     private String _escape (String term) {
         return term.replaceAll("([#\\$\\\\])", "\\\\$1");
     };
-    
+
+
     // Unescape the term
     private String _unescape (String term) {
-        return term.replace("\\\\","\\")
-            .replace("\\#", "#")
-            .replace("\\$", "$");
+        return term.replace("\\\\", "\\").replace("\\#", "#")
+                .replace("\\$", "$");
     };
 };
diff --git a/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java b/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java
index de1e4c8..1f28383 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SimpleSpanQuery.java
@@ -75,8 +75,10 @@
     protected String field;
     protected boolean collectPayloads;
 
+
     public SimpleSpanQuery () {}
 
+
     /**
      * Constructs a new SimpleSpanQuery using the specified
      * {@link SpanQuery} and set whether payloads are to be collected
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanClassFilterQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanClassFilterQuery.java
index 48790bc..799c87e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanClassFilterQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanClassFilterQuery.java
@@ -21,31 +21,33 @@
     private ClassOperation operation;
     private byte classNum1, classNum2;
 
+
     public SpanClassFilterQuery (SpanQuery sq, ClassOperation type,
-            int classNum1, int classNum2,
-            boolean collectPayloads) {
+                                 int classNum1, int classNum2,
+                                 boolean collectPayloads) {
         super(sq, collectPayloads);
         this.operation = type;
         this.classNum1 = (byte) classNum1;
         this.classNum2 = (byte) classNum2;
     }
 
+
     @Override
-    public SimpleSpanQuery clone() {
+    public SimpleSpanQuery clone () {
         return new SpanClassFilterQuery((SpanQuery) firstClause.clone(),
-                operation,
-                classNum1, classNum2, collectPayloads);
+                operation, classNum1, classNum2, collectPayloads);
     }
 
+
     @Override
-    public Spans getSpans(AtomicReaderContext context, Bits acceptDocs,
+    public Spans getSpans (AtomicReaderContext context, Bits acceptDocs,
             Map<Term, TermContext> termContexts) throws IOException {
-        return new ClassFilteredSpans(this, context, acceptDocs,
-                termContexts);
+        return new ClassFilteredSpans(this, context, acceptDocs, termContexts);
     }
 
+
     @Override
-    public String toString(String field) {
+    public String toString (String field) {
         StringBuilder sb = new StringBuilder();
         sb.append("spanClassFilter(");
         sb.append(firstClause.toString());
@@ -59,27 +61,33 @@
         return sb.toString();
     }
 
-    public ClassOperation getOperation() {
+
+    public ClassOperation getOperation () {
         return operation;
     }
 
-    public void setOperation(ClassOperation operation) {
+
+    public void setOperation (ClassOperation operation) {
         this.operation = operation;
     }
 
-    public byte getClassNum1() {
+
+    public byte getClassNum1 () {
         return classNum1;
     }
 
-    public void setClassNum1(byte classNum1) {
+
+    public void setClassNum1 (byte classNum1) {
         this.classNum1 = classNum1;
     }
 
-    public byte getClassNum2() {
+
+    public byte getClassNum2 () {
         return classNum2;
     }
 
-    public void setClassNum2(byte classNum2) {
+
+    public void setClassNum2 (byte classNum2) {
         this.classNum2 = classNum2;
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanFocusQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanFocusQuery.java
index c144a58..ab29d0f 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanFocusQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanFocusQuery.java
@@ -38,6 +38,7 @@
     private boolean matchTemporaryClass = false;
     private boolean removeTemporaryClasses = false;
 
+
     /**
      * Construct a new SpanFocusQuery.
      * 
@@ -80,7 +81,7 @@
     public String toString (String field) {
         StringBuffer buffer = new StringBuffer();
         buffer.append("focus(");
-        if (matchTemporaryClass){
+        if (matchTemporaryClass) {
             buffer.append("#");
         }
         if (classNumbers.size() > 1) {
@@ -188,19 +189,23 @@
         this.isSorted = isSorted;
     }
 
-    public boolean matchTemporaryClass() {
+
+    public boolean matchTemporaryClass () {
         return matchTemporaryClass;
     }
 
-    public void setMatchTemporaryClass(boolean matchTemporaryClass) {
+
+    public void setMatchTemporaryClass (boolean matchTemporaryClass) {
         this.matchTemporaryClass = matchTemporaryClass;
     }
 
-    public boolean removeTemporaryClasses() {
+
+    public boolean removeTemporaryClasses () {
         return removeTemporaryClasses;
     }
 
-    public void setRemoveTemporaryClasses(boolean rem) {
+
+    public void setRemoveTemporaryClasses (boolean rem) {
         this.removeTemporaryClasses = rem;
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanReferenceQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanReferenceQuery.java
index 9c2484d..3b23a18 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanReferenceQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanReferenceQuery.java
@@ -16,8 +16,9 @@
 
     private byte classNum;
 
+
     public SpanReferenceQuery (SpanQuery firstClause, byte classNum,
-            boolean collectPayloads) {
+                               boolean collectPayloads) {
         super(firstClause, collectPayloads);
         this.classNum = classNum;
     }
@@ -50,11 +51,12 @@
     }
 
 
-    public byte getClassNum() {
+    public byte getClassNum () {
         return classNum;
     }
 
-    public void setClassNum(byte classNum) {
+
+    public void setClassNum (byte classNum) {
         this.classNum = classNum;
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanRelationMatchQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanRelationMatchQuery.java
index 5bc962a..0fac2a3 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanRelationMatchQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanRelationMatchQuery.java
@@ -12,14 +12,15 @@
 
 import de.ids_mannheim.korap.query.spans.FocusSpans;
 
-public class SpanRelationMatchQuery extends SimpleSpanQuery{
+public class SpanRelationMatchQuery extends SimpleSpanQuery {
 
     private SpanQuery operandQuery;
     private SpanQuery operand2Query;
     private SpanRelationQuery relationQuery;
 
-    public SpanRelationMatchQuery (SpanRelationQuery relation, SpanQuery operand,
-            boolean collectPayloads) {
+
+    public SpanRelationMatchQuery (SpanRelationQuery relation,
+                                   SpanQuery operand, boolean collectPayloads) {
 
         checkVariables(relation, operand);
         SpanFocusQuery sq = new SpanFocusQuery(new SpanSegmentQuery(
@@ -34,8 +35,10 @@
         this.collectPayloads = collectPayloads;
     }
 
-    public SpanRelationMatchQuery (SpanRelationQuery relation, SpanQuery source,
-            SpanQuery target, boolean collectPayloads) {
+
+    public SpanRelationMatchQuery (SpanRelationQuery relation,
+                                   SpanQuery source, SpanQuery target,
+                                   boolean collectPayloads) {
 
         checkVariables(relation, source, target);
         SpanFocusQuery sq = null;
@@ -63,7 +66,8 @@
 
     }
 
-    public void checkVariables(SpanRelationQuery relation, SpanQuery operand) {
+
+    public void checkVariables (SpanRelationQuery relation, SpanQuery operand) {
         if (relation == null) {
             throw new IllegalArgumentException(
                     "The relation query cannot be null.");
@@ -80,8 +84,10 @@
         this.relationQuery = relation;
         this.operandQuery = operand;
     }
-    
-    public void checkVariables(SpanRelationQuery relation, SpanQuery operand, SpanQuery target) {
+
+
+    public void checkVariables (SpanRelationQuery relation, SpanQuery operand,
+            SpanQuery target) {
         checkVariables(relation, operand);
         if (target == null) {
             if (operand == null) {
@@ -96,8 +102,9 @@
         this.operand2Query = target;
     }
 
+
     @Override
-    public SimpleSpanQuery clone() {
+    public SimpleSpanQuery clone () {
         if (operand2Query != null) {
             return new SpanRelationMatchQuery(
                     (SpanRelationQuery) relationQuery.clone(),
@@ -110,16 +117,18 @@
                 (SpanQuery) operandQuery.clone(), collectPayloads);
     }
 
+
     @Override
-    public Spans getSpans(AtomicReaderContext context, Bits acceptDocs,
+    public Spans getSpans (AtomicReaderContext context, Bits acceptDocs,
             Map<Term, TermContext> termContexts) throws IOException {
 
         return new FocusSpans((SpanFocusQuery) firstClause, context,
                 acceptDocs, termContexts);
     }
 
+
     @Override
-    public String toString(String field) {
+    public String toString (String field) {
         return getFirstClause().toString();
     }
 }
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanRelationQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanRelationQuery.java
index 8d06a7e..65b8786 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanRelationQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanRelationQuery.java
@@ -62,7 +62,10 @@
     private byte sourceClass;
     private byte targetClass;
 
-    private List<Byte> tempClassNumbers = Arrays.asList(tempSourceNum, tempTargetNum);
+    private List<Byte> tempClassNumbers = Arrays.asList(tempSourceNum,
+            tempTargetNum);
+
+
     /**
      * Constructs a SpanRelationQuery based on the given span query.
      * 
@@ -83,14 +86,16 @@
         }
     }
 
+
     public SpanRelationQuery (SpanQuery firstClause, List<Byte> classNumbers,
-            boolean collectPayloads) {
+                              boolean collectPayloads) {
         this(firstClause, collectPayloads);
         this.tempClassNumbers = classNumbers;
         this.tempSourceNum = classNumbers.get(0);
         this.tempTargetNum = classNumbers.get(1);
     }
 
+
     @Override
     public SimpleSpanQuery clone () {
         SimpleSpanQuery sq = new SpanRelationQuery(
@@ -132,43 +137,53 @@
         return sb.toString();
     }
 
-    public int getDirection() {
+
+    public int getDirection () {
         return direction;
     }
 
-    public void setDirection(int direction) {
+
+    public void setDirection (int direction) {
         this.direction = direction;
     }
 
-    public List<Byte> getTempClassNumbers() {
+
+    public List<Byte> getTempClassNumbers () {
         return tempClassNumbers;
     }
 
-    public void setTempClassNumbers(List<Byte> classNumbers) {
+
+    public void setTempClassNumbers (List<Byte> classNumbers) {
         this.tempClassNumbers = classNumbers;
     }
 
-    public byte getTempSourceNum() {
+
+    public byte getTempSourceNum () {
         return tempSourceNum;
     }
 
-    public void setTempSourceNum(byte sourceNum) {
+
+    public void setTempSourceNum (byte sourceNum) {
         this.tempSourceNum = sourceNum;
     }
 
-    public byte getTempTargetNum() {
+
+    public byte getTempTargetNum () {
         return tempTargetNum;
     }
 
-    public void setTempTargetNum(byte targetNum) {
+
+    public void setTempTargetNum (byte targetNum) {
         this.tempTargetNum = targetNum;
     }
 
-    public byte getSourceClass() {
+
+    public byte getSourceClass () {
         return sourceClass;
     }
 
-    public void setSourceClass(byte sourceClass)
+
+    public void setSourceClass (byte sourceClass)
             throws IllegalArgumentException {
         if (sourceClass < 1) {
             throw new IllegalArgumentException(
@@ -178,11 +193,13 @@
         this.sourceClass = sourceClass;
     }
 
-    public byte getTargetClass() {
+
+    public byte getTargetClass () {
         return targetClass;
     }
 
-    public void setTargetClass(byte targetClass)
+
+    public void setTargetClass (byte targetClass)
             throws IllegalArgumentException {
         if (targetClass < 1) {
             throw new IllegalArgumentException(
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanSegmentQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanSegmentQuery.java
index 7e75543..29189e2 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanSegmentQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanSegmentQuery.java
@@ -46,6 +46,7 @@
         this(firstClause, secondClause, true);
     }
 
+
     /**
      * Constructs a SpanSegmentQuery from the two given SpanQueries.
      * 
@@ -60,7 +61,7 @@
      *            <code>false</code>.
      */
     public SpanSegmentQuery (SpanQuery firstClause, SpanQuery secondClause,
-            boolean collectPayloads) {
+                             boolean collectPayloads) {
         super(firstClause, secondClause, collectPayloads);
         if (firstClause instanceof SpanRelationQuery) {
             isRelation = true;
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ClassFilteredSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ClassFilteredSpans.java
index bfd5f11..28459cf 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ClassFilteredSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ClassFilteredSpans.java
@@ -20,9 +20,11 @@
     private ClassOperation operation;
     private byte classNum1, classNum2;
 
+
     public ClassFilteredSpans (SpanClassFilterQuery query,
-            AtomicReaderContext context, Bits acceptDocs,
-            Map<Term, TermContext> termContexts) throws IOException {
+                               AtomicReaderContext context, Bits acceptDocs,
+                               Map<Term, TermContext> termContexts)
+            throws IOException {
         super(query, context, acceptDocs, termContexts);
         this.operation = query.getOperation();
         this.classNum1 = query.getClassNum1();
@@ -30,8 +32,9 @@
         hasMoreSpans = firstSpans.next();
     }
 
+
     @Override
-    public boolean next() throws IOException {
+    public boolean next () throws IOException {
         while (hasMoreSpans) {
             matchPayload.clear();
             bitset1 = null;
@@ -49,17 +52,21 @@
         return false;
     }
 
-    private boolean isClassOperationValid() throws IOException {
+
+    private boolean isClassOperationValid () throws IOException {
         setBitsets();
-        int cardinality = Math.max(bitset1.cardinality(), bitset2.cardinality());
+        int cardinality = Math
+                .max(bitset1.cardinality(), bitset2.cardinality());
         bitset1.and(bitset2);
         // System.out.println("cardinality:" + cardinality);
         switch (operation) {
             case DISJOINT:
-                if (bitset1.cardinality() == 0) return true;
+                if (bitset1.cardinality() == 0)
+                    return true;
                 break;
             case EQUAL:
-                if (cardinality == bitset1.cardinality()) return true;
+                if (cardinality == bitset1.cardinality())
+                    return true;
                 break;
             case DIFFER:
                 if (cardinality == 0 || cardinality != bitset1.cardinality())
@@ -71,19 +78,21 @@
                 }
                 break;
             case INTERSECT:
-                if (bitset1.cardinality() > 0) return true;
+                if (bitset1.cardinality() > 0)
+                    return true;
                 break;
         }
 
         return false;
     }
 
-    private void setBitsets() throws IOException {
+
+    private void setBitsets () throws IOException {
         BitSet bs = new BitSet();
         int start, end;
         // System.out.println("------------------------");
         for (byte[] payload : firstSpans.getPayload()) {
-            if (payload.length == 9){
+            if (payload.length == 9) {
                 start = byte2int(payload, 0) + 1;
                 end = byte2int(payload, 4) + 1;
                 if (payload[8] == classNum1) {
@@ -118,14 +127,16 @@
 
     }
 
+
     @Override
-    public boolean skipTo(int target) throws IOException {
+    public boolean skipTo (int target) throws IOException {
         // TODO Auto-generated method stub
         return false;
     }
 
+
     @Override
-    public long cost() {
+    public long cost () {
         // TODO Auto-generated method stub
         return 0;
     }
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java
index ac83c4d..09cc05b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java
@@ -128,7 +128,7 @@
     };
 
 
-    protected boolean addClassPayload() throws IOException {
+    protected boolean addClassPayload () throws IOException {
         hasmorespans = true;
 
         classedPayload.clear();
@@ -151,7 +151,7 @@
 
         // Add highlight information as byte array
         classedPayload.add(bb.array());
-        
+
         if (spans instanceof SimpleSpans) {
             SimpleSpans ss = (SimpleSpans) spans;
             this.hasSpanId = ss.hasSpanId;
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ReferenceSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ReferenceSpans.java
index a860afd..cae4c5a 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ReferenceSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ReferenceSpans.java
@@ -16,6 +16,7 @@
 
     private byte classNum;
 
+
     public ReferenceSpans (SpanReferenceQuery query,
                            AtomicReaderContext context, Bits acceptDocs,
                            Map<Term, TermContext> termContexts)
@@ -43,7 +44,7 @@
 
 
     private boolean hasSameClassPosition () throws IOException {
-        int start=0, end=0;
+        int start = 0, end = 0;
         boolean isFound = false;
         boolean match = false;
 
@@ -52,7 +53,8 @@
         for (byte[] payload : firstSpans.getPayload()) {
             if (payload.length == 9 && payload[8] == classNum) {
                 if (isFound) {
-                    if (start == byte2int(payload, 0) && end == byte2int(payload, 4)){
+                    if (start == byte2int(payload, 0)
+                            && end == byte2int(payload, 4)) {
                         match = true;
                         continue;
                     }
@@ -86,7 +88,7 @@
 
 
     @Override
-    public long cost() {
+    public long cost () {
         return firstSpans.cost();
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/RelationSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/RelationSpans.java
index 66afdbe..979b359 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/RelationSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/RelationSpans.java
@@ -61,6 +61,7 @@
     private byte tempSourceNum, tempTargetNum;
     private byte sourceClass, targetClass;
 
+
     /**
      * Constructs RelationSpans from the given
      * {@link SpanRelationQuery}.
@@ -251,7 +252,8 @@
         cs.setPayloads(payload);
     }
 
-    private byte[] createClassPayload(int start, int end, byte classNumber,
+
+    private byte[] createClassPayload (int start, int end, byte classNumber,
             boolean keep) {
         ByteBuffer buffer = null;
         if (keep) {
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java
index 02a734f..8b4c54d 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java
@@ -10,8 +10,9 @@
     private SpanQueryWrapper subQuery;
     private byte classNum;
 
+
     public SpanReferenceQueryWrapper (SpanQueryWrapper subQueryWrapper,
-            byte classNum) {
+                                      byte classNum) {
         this.subQuery = subQueryWrapper;
         if (subQuery != null) {
             this.isNull = false;
@@ -30,17 +31,19 @@
         }
         this.classNum = classNum;
     }
-    
+
+
     @Override
-    public SpanQuery toQuery() throws QueryException {
+    public SpanQuery toQuery () throws QueryException {
 
         if (this.isNull() || this.isEmpty()) {
             return null;
         }
 
         SpanQuery sq = subQuery.retrieveNode(this.retrieveNode).toQuery();
-        if (sq == null) return null;
-        
+        if (sq == null)
+            return null;
+
         return new SpanReferenceQuery(sq, classNum, true);
     }
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java
index 2535ce9..992787c 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java
@@ -14,8 +14,10 @@
     private SpanQueryWrapper subQuery1;
     private SpanQueryWrapper subQuery2;
 
+
     public SpanRelationWrapper (SpanQueryWrapper relationWrapper,
-            SpanQueryWrapper operand1, SpanQueryWrapper operand2) {
+                                SpanQueryWrapper operand1,
+                                SpanQueryWrapper operand2) {
 
         this.relationQuery = relationWrapper;
         if (relationQuery != null) {
@@ -35,17 +37,17 @@
 
 
     @Override
-    public SpanQuery toQuery() throws QueryException {
+    public SpanQuery toQuery () throws QueryException {
 
         if (this.isNull() || this.isEmpty()) {
             return null;
         }
 
-        SpanTermQuery relationTermQuery = (SpanTermQuery) relationQuery.retrieveNode(
-                this.retrieveNode).toQuery();
+        SpanTermQuery relationTermQuery = (SpanTermQuery) relationQuery
+                .retrieveNode(this.retrieveNode).toQuery();
         if (relationTermQuery == null)
             return null;
-        
+
         SpanRelationQuery rq = new SpanRelationQuery(relationTermQuery, true);
         SpanQuery subq1, subq2;
 
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 08c33b1..211c113 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
@@ -477,8 +477,8 @@
             String unit, boolean exclusion) {
 
         if (DEBUG)
-            log.trace("With contraint {}-{} (unit {}, excl {})",
-                      min, max, unit, exclusion);
+            log.trace("With contraint {}-{} (unit {}, excl {})", min, max,
+                    unit, exclusion);
 
         // Word unit
         if (unit.equals("w")) {
@@ -537,8 +537,8 @@
             SpanElementQueryWrapper unit, boolean exclusion) {
 
         if (DEBUG)
-            log.trace("With contraint {}-{} (unit {}, excl {})",
-                      min, max, unit.toString(), exclusion);
+            log.trace("With contraint {}-{} (unit {}, excl {})", min, max,
+                    unit.toString(), exclusion);
 
         if (this.constraints == null)
             this.constraints = new ArrayList<DistanceConstraint>(1);
@@ -601,10 +601,8 @@
         // that will be optimized away later on
         if (this.constraints.size() == 1) {
             DistanceConstraint dc = this.constraints.get(0);
-            if (dc.getUnit().equals("w") &&
-                dc.getMinDistance() == 1 &&
-                dc.getMaxDistance() == 1 &&
-                this.isInOrder) {
+            if (dc.getUnit().equals("w") && dc.getMinDistance() == 1
+                    && dc.getMaxDistance() == 1 && this.isInOrder) {
                 return false;
             };
         };
diff --git a/src/main/java/de/ids_mannheim/korap/response/Response.java b/src/main/java/de/ids_mannheim/korap/response/Response.java
index 2af28ab..674cd0b 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Response.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Response.java
@@ -40,9 +40,9 @@
 
     private KrillMeta meta;
     private KrillCollection collection;
+    private KrillQuery query;
 
     private String version, name, node, listener;
-    private KrillQuery query;
 
     private long totalResources = -2, // Not set
             totalResults = -2; // Not set
@@ -403,7 +403,9 @@
         this.collection = collection;
 
         // Move messages from the collection
-        return (Response) this.moveNotificationsFrom(collection);
+        Response resp = (Response) this.moveNotificationsFrom(collection);
+
+        return resp;
     };
 
 
@@ -468,9 +470,8 @@
                 json.put("meta", metaNode);
         };
 
-        ObjectNode meta = json.has("meta") ?
-            (ObjectNode) json.get("meta") :
-            (ObjectNode) json.putObject("meta");
+        ObjectNode meta = json.has("meta") ? (ObjectNode) json.get("meta")
+                : (ObjectNode) json.putObject("meta");
 
         if (sb.length() > 0)
             meta.put("version", sb.toString());
@@ -503,8 +504,8 @@
         };
 
         // KoralQuery collection object
-        if (this.collection != null
-                && this.collection.getFilters().toArray().length > 0) {
+        if (this.collection != null) {
+            // && this.collection.getFilters().toArray().length > 0) {
             JsonNode collNode = this.collection.toJsonNode();
             if (collNode != null)
                 json.put("collection", collNode);
diff --git a/src/main/java/de/ids_mannheim/korap/response/Result.java b/src/main/java/de/ids_mannheim/korap/response/Result.java
index c120d43..bc57674 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Result.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Result.java
@@ -327,9 +327,8 @@
 
 
     private void _addMeta (ObjectNode json) {
-        ObjectNode meta = json.has("meta") ?
-            (ObjectNode) json.get("meta") :
-            (ObjectNode) json.putObject("meta");
+        ObjectNode meta = json.has("meta") ? (ObjectNode) json.get("meta")
+                : (ObjectNode) json.putObject("meta");
 
 
         // Relevant context setting
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
index bb35479..41ce2dd 100644
--- a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
@@ -4,6 +4,12 @@
 import java.io.*;
 
 import de.ids_mannheim.korap.KrillCollection;
+import de.ids_mannheim.korap.Krill;
+import de.ids_mannheim.korap.KrillIndex;
+import de.ids_mannheim.korap.response.Result;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.JsonNode;
 
 import static de.ids_mannheim.korap.TestSimple.*;
 
@@ -72,6 +78,41 @@
     };
 
 
+    @Test
+    public void collectionMirror () throws Exception {
+        // Construct index
+        KrillIndex ki = new KrillIndex();
+        // Indexing test files
+        for (String i : new String[] { "00001", "00002", "00003", "00004",
+                "00005", "00006", "02439" }) {
+            ki.addDoc(
+                    getClass().getResourceAsStream("/wiki/" + i + ".json.gz"),
+                    true);
+        };
+        ki.commit();
+
+        Krill krill = new Krill(_getJSONString("collection_6.jsonld"));
+        Result kr = krill.apply(ki);
+        assertEquals(kr.getTotalResults(), 86);
+
+        ObjectMapper mapper = new ObjectMapper();
+        JsonNode res = mapper.readTree(kr.toJsonString());
+        assertTrue(res.at("/collection").isMissingNode());
+
+        krill = new Krill(_getJSONString("collection_6_withCollection.jsonld"));
+
+        kr = krill.apply(ki);
+        assertEquals(kr.getTotalResults(), 57);
+
+        res = mapper.readTree(kr.toJsonString());
+        assertFalse(res.at("/collection").isMissingNode());
+        assertEquals("koral:doc", res.at("/collection/@type").asText());
+        assertEquals("textClass", res.at("/collection/key").asText());
+        assertEquals("reisen", res.at("/collection/value").asText());
+        assertEquals("match:eq", res.at("/collection/match").asText());
+    };
+
+
     private String _getJSONString (String file) {
         return getString(getClass().getResource(path + file).getFile());
     };
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestClassFilterIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestClassFilterIndex.java
index 77ef45d..5a2565e 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestClassFilterIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestClassFilterIndex.java
@@ -20,8 +20,9 @@
     private KrillIndex ki;
     private Result kr;
 
+
     @Test
-    public void testInclude() throws IOException {
+    public void testInclude () throws IOException {
         ki = new KrillIndex();
         ki.addDoc(TestReferenceIndex.createFieldDoc0());
         ki.commit();
@@ -36,8 +37,7 @@
         SpanClassFilterQuery sq = new SpanClassFilterQuery(sdq,
                 ClassOperation.INCLUDE, 2, 1, true);
 
-        assertEquals(
-                sq.toString(),
+        assertEquals(sq.toString(),
                 "spanClassFilter(spanDistance({1: <tokens:np />}, {2: <tokens:vp />}, "
                         + "[(w[0:1], notOrdered, notExcluded)]),INCLUDE,2,1)");
 
@@ -63,7 +63,8 @@
                 "Frankenstein, treat my daughter well. She [{2:is {1:the one that "
                         + "saved your master who you hold so dear}}].", kr
                         .getMatch(2).getSnippetBrackets());
-        assertEquals("Frankenstein, treat my daughter well. She [{2:is the one that "
+        assertEquals(
+                "Frankenstein, treat my daughter well. She [{2:is the one that "
                         + "saved {1:your master} who you hold so dear}].", kr
                         .getMatch(3).getSnippetBrackets());
         assertEquals(
@@ -72,8 +73,9 @@
 
     }
 
+
     @Test
-    public void testDisjoint() throws IOException {
+    public void testDisjoint () throws IOException {
         ki = new KrillIndex();
         ki.addDoc(TestReferenceIndex.createFieldDoc0());
         ki.commit();
@@ -94,7 +96,7 @@
 
         SpanClassFilterQuery sq = new SpanClassFilterQuery(sdq,
                 ClassOperation.DISJOINT, 2, 1, true);
-        
+
         kr = ki.search(sq, (short) 10);
         // for (Match km : kr.getMatches()) {
         // System.out.println(km.getStartPos() + "," + km.getEndPos()
@@ -119,10 +121,11 @@
                 kr.getMatch(2).getSnippetBrackets());
     }
 
+
     // Problem with SpanDistanceQuery - unordered distance spans,
     // -> unsorted
     @Test
-    public void testEqual() throws IOException {
+    public void testEqual () throws IOException {
         ki = new KrillIndex();
         ki.addDoc(TestReferenceIndex.createFieldDoc0());
         ki.commit();
@@ -155,8 +158,9 @@
 
     }
 
+
     @Test
-    public void testDiffer() throws IOException {
+    public void testDiffer () throws IOException {
         ki = new KrillIndex();
         ki.addDoc(TestReferenceIndex.createFieldDoc0());
         ki.commit();
@@ -194,7 +198,7 @@
 
 
     @Test
-    public void testIntersect() throws IOException {
+    public void testIntersect () throws IOException {
         ki = new KrillIndex();
         ki.addDoc(TestReferenceIndex.createFieldDoc0());
         ki.commit();
@@ -217,13 +221,14 @@
         SpanClassFilterQuery sq = new SpanClassFilterQuery(sdq3,
                 ClassOperation.INTERSECT, 1, 2, true);
 
-        assertEquals("spanClassFilter(spanDistance({1: spanDistance(<tokens:np />, " +
-        		"{3: <tokens:vb />}, [(w[0:1], notOrdered, notExcluded)])}, " +
-        		"{2: spanDistance(<tokens:prp />, <tokens:vb />, [(w[0:1], " +
-        		"notOrdered, notExcluded)])}, [(w[0:1], notOrdered, notExcluded)]),INTERSECT,1,2)",
+        assertEquals(
+                "spanClassFilter(spanDistance({1: spanDistance(<tokens:np />, "
+                        + "{3: <tokens:vb />}, [(w[0:1], notOrdered, notExcluded)])}, "
+                        + "{2: spanDistance(<tokens:prp />, <tokens:vb />, [(w[0:1], "
+                        + "notOrdered, notExcluded)])}, [(w[0:1], notOrdered, notExcluded)]),INTERSECT,1,2)",
                 sq.toString());
-        
-        
+
+
         kr = ki.search(sq, (short) 20);
 
         // for (Match km : kr.getMatches()) {
@@ -247,7 +252,7 @@
 
 
     @Test
-    public void testMultipleSameClasses() throws IOException {
+    public void testMultipleSameClasses () throws IOException {
 
         ki = new KrillIndex();
         ki.addDoc(TestReferenceIndex.createFieldDoc0());
@@ -269,10 +274,10 @@
 
         SpanClassFilterQuery sq = new SpanClassFilterQuery(sdq2,
                 ClassOperation.INCLUDE, 2, 1, true);
-        
+
         kr = ki.search(sdq2, (short) 20);
         assertEquals(6, kr.getTotalResults());
-        
+
         // for (Match km : kr.getMatches()) {
         // System.out.println(km.getStartPos() + "," + km.getEndPos()
         // + " "
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestIndex.java
index be5fd8c..aac981e 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestIndex.java
@@ -75,6 +75,7 @@
 
     private Directory index = new RAMDirectory();
 
+
     @Test
     public void multiTerm () throws CorpusDataException {
         MultiTerm test = new MultiTerm("test");
@@ -110,6 +111,7 @@
         assertTrue(test.toString().startsWith("xip/l:\\#normal#0-5$"));
     };
 
+
     @Test
     public void multiTermToken () throws CorpusDataException {
         MultiTermToken test = new MultiTermToken("hunde", "pos:n", "m:gen:pl");
@@ -123,6 +125,7 @@
         assertEquals(test.terms.get(2).term, "m:gen:pl");
     };
 
+
     private List initIndexer () throws IOException {
         List<Map<String, String>> list = new ArrayList<>();
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestReferenceIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestReferenceIndex.java
index 4a56a04..f718ce3 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestReferenceIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestReferenceIndex.java
@@ -28,6 +28,7 @@
     private KrillIndex ki;
     private Result kr;
 
+
     @Test
     public void testCase1 () throws IOException {
         ki = new KrillIndex();
@@ -55,7 +56,8 @@
         // SpanSegmentQuery ssq2 = new SpanSegmentQuery(sfq2, scq3,
         // true);
 
-        SpanRelationMatchQuery rq = new SpanRelationMatchQuery(srq, sfq1, scq3, true);
+        SpanRelationMatchQuery rq = new SpanRelationMatchQuery(srq, sfq1, scq3,
+                true);
 
         // System.out.println(rq.toString());
         SpanFocusQuery sfq3 = new SpanFocusQuery(rq, (byte) 1);
@@ -85,8 +87,9 @@
         assertEquals(7, kr.getMatch(0).getEndPos());
     }
 
+
     @Test
-    public void testCase2() throws IOException, QueryException {
+    public void testCase2 () throws IOException, QueryException {
 
         String filepath = getClass().getResource(
                 "/queries/reference/distance-reference.jsonld").getFile();
@@ -97,13 +100,12 @@
         // .{0,2} #3 & #3 -> #2
 
         assertEquals(
-            "spanReference(focus(#[1,2]spanSegment(focus(#2: "
-                    + "spanSegment(spanRelation(tokens:>:stanford/d:tag), "
-                    + "focus(3: spanDistance(focus(1: spanDistance({1: <tokens:vb />}, "
-                    + "{2: <tokens:prp />}, [(w[0:1], notOrdered, notExcluded)])), "
-                    + "{3: <tokens:nn />}, [(w[0:2], notOrdered, notExcluded)])))), "
-                    + "{2: <tokens:prp />})), 2)",
-            sq.toString());
+                "spanReference(focus(#[1,2]spanSegment(focus(#2: "
+                        + "spanSegment(spanRelation(tokens:>:stanford/d:tag), "
+                        + "focus(3: spanDistance(focus(1: spanDistance({1: <tokens:vb />}, "
+                        + "{2: <tokens:prp />}, [(w[0:1], notOrdered, notExcluded)])), "
+                        + "{3: <tokens:nn />}, [(w[0:2], notOrdered, notExcluded)])))), "
+                        + "{2: <tokens:prp />})), 2)", sq.toString());
 
         SpanElementQuery seq1 = new SpanElementQuery("tokens", "vb");
         // new SpanTermQuery(new Term("tokens", "c:vb"));
@@ -174,9 +176,10 @@
         // }
     }
 
+
     // multiple references
     @Test
-    public void testCase3() throws IOException, QueryException {
+    public void testCase3 () throws IOException, QueryException {
         ki = new KrillIndex();
         ki.addDoc(createFieldDoc0());
         ki.commit();
@@ -209,17 +212,17 @@
         assertEquals(13, kr.getMatch(1).getEndPos());
     }
 
+
     // multiple document
     @Test
-    public void testCase4() throws Exception {
+    public void testCase4 () throws Exception {
         ki = new KrillIndex();
         ki.addDoc(createFieldDoc0());
         ki.addDoc(createFieldDoc1());
         ki.commit();
 
         String filepath = getClass().getResource(
-                "/queries/reference/distance-reference.jsonld")
-                .getFile();
+                "/queries/reference/distance-reference.jsonld").getFile();
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
         SpanQuery sq = sqwi.toQuery();
 
@@ -231,7 +234,8 @@
         assertEquals(4, kr.getMatch(3).getEndPos());
     }
 
-    public static FieldDocument createFieldDoc1() {
+
+    public static FieldDocument createFieldDoc1 () {
         FieldDocument fd = new FieldDocument();
         fd.addString("ID", "doc-1");
         fd.addTV(
@@ -256,69 +260,69 @@
         return fd;
     }
 
+
     public static FieldDocument createFieldDoc0 () {
         FieldDocument fd = new FieldDocument();
         fd.addString("ID", "doc-0");
         fd.addTV(
                 "tokens",
                 "Frankenstein, treat my daughter well. She is the one that saved your master who you hold so dear.",
-                
+
                 "[(0-12)s:Frankenstein|_0#0-12|<>:nn#0-12$<i>1<s>18|<>:s#0-37$<i>5<s>1|"
                         + "<>:np#0-13$<i>1<s>2|"
                         + "<:stanford/d:tag$<i>1<s>1<s>18<s>19]"
 
-                + "[(14-19)s:treat|_1#14-19|<>:vb#14-19$<i>2<s>19|<>:vp#14-36$<i>5<s>3|"
+                        + "[(14-19)s:treat|_1#14-19|<>:vb#14-19$<i>2<s>19|<>:vp#14-36$<i>5<s>3|"
                         + ">:stanford/d:tag$<i>0<s>2<s>19<s>18|"
                         + ">:stanford/d:tag$<i>3<s>3<s>19<s>21|"
                         + ">:stanford/d:tag$<i>4<s>4<s>19<s>22]"
 
-                + "[(20-22)s:my|_2#20-22|<>:prp#20-22$<i>3<s>20|<>:np#20-31$<i>4<s>4]"
+                        + "[(20-22)s:my|_2#20-22|<>:prp#20-22$<i>3<s>20|<>:np#20-31$<i>4<s>4]"
 
-                + "[(23-31)s:daughter|_3#23-31|<>:nn#23-31$<i>4<s>21|"
+                        + "[(23-31)s:daughter|_3#23-31|<>:nn#23-31$<i>4<s>21|"
                         + ">:stanford/d:tag$<i>2<s>5<s>21<s>20]"
 
-                + "[(32-36)s:well|_4#32-36|<>:rb#32-36$<i>5<s>22|<>:advp#32-36$<i>5<s>5]"
+                        + "[(32-36)s:well|_4#32-36|<>:rb#32-36$<i>5<s>22|<>:advp#32-36$<i>5<s>5]"
 
-                + "[(38-41)s:She|_5#38-41|<>:prp#38-41$<i>6<s>23|<>:np#38-41$<i>6<s>36|<>:s#38-97$<i>18<s>6]"
+                        + "[(38-41)s:She|_5#38-41|<>:prp#38-41$<i>6<s>23|<>:np#38-41$<i>6<s>36|<>:s#38-97$<i>18<s>6]"
 
-                + "[(42-44)s:is|_6#42-44|<>:vb#42-44$<i>7<s>24|<>:vp#42-96$<i>18<s>7]"
+                        + "[(42-44)s:is|_6#42-44|<>:vb#42-44$<i>7<s>24|<>:vp#42-96$<i>18<s>7]"
 
-                + "[(45-48)s:the|_7#45-48|<>:dt#45-48$<i>8<s>25|<>:np#45-52$<i>9<s>8|<>:np#45-96$<i>18<s>9]"
+                        + "[(45-48)s:the|_7#45-48|<>:dt#45-48$<i>8<s>25|<>:np#45-52$<i>9<s>8|<>:np#45-96$<i>18<s>9]"
 
-                + "[(49-52)s:one|_8#49-52|<>:nn#49-52$<i>9<s>26|"
+                        + "[(49-52)s:one|_8#49-52|<>:nn#49-52$<i>9<s>26|"
                         + ">:stanford/d:tag$<i>5<s>6<s>26<s>23|"
                         + ">:stanford/d:tag$<i>6<s>7<s>26<s>24|"
                         + ">:stanford/d:tag$<i>7<s>8<s>26<s>25|"
                         + ">:stanford/d:tag$<i>10<s>9<s>26<s>28]"
-                
-                + "[(53-57)s:that|_9#53-57|<>:rp#53-57$<i>10<s>27|<>:sb#53-96$<i>18<s>10]"
-                                              
-                + "[(58-63)s:saved|_10#58-63|<>:vb#58-63$<i>11<s>28|<>:s#58-96$<i>18<s>11|"
+
+                        + "[(53-57)s:that|_9#53-57|<>:rp#53-57$<i>10<s>27|<>:sb#53-96$<i>18<s>10]"
+
+                        + "[(58-63)s:saved|_10#58-63|<>:vb#58-63$<i>11<s>28|<>:s#58-96$<i>18<s>11|"
                         + "<>:vp#58-96$<i>18<s>12|"
                         + ">:stanford/d:tag$<i>9<s>10<s>28<s>27|"
                         + ">:stanford/d:tag$<i>12<s>11<s>28<s>30|"
                         + ">:stanford/d:tag$<i>15<s>12<s>28<s>33]"
 
-                + "[(64-68)s:your|_11#64-68|<>:prp#64-68$<i>12<s>29|<>:np#64-75$<i>13<s>13]"
+                        + "[(64-68)s:your|_11#64-68|<>:prp#64-68$<i>12<s>29|<>:np#64-75$<i>13<s>13]"
 
-                + "[(69-75)s:master|_12#69-75|<>:nn#69-75$<i>13<s>30|"
+                        + "[(69-75)s:master|_12#69-75|<>:nn#69-75$<i>13<s>30|"
                         + ">:stanford/d:tag$<i>11<s>13<s>30<s>29]"
 
-                + "[(76-79)s:who|_13#76-79|<>:rp#76-79$<i>14<s>31|<>:sb#76-96$<i>18<s>14]"
+                        + "[(76-79)s:who|_13#76-79|<>:rp#76-79$<i>14<s>31|<>:sb#76-96$<i>18<s>14]"
 
-                + "[(80-83)s:you|_14#80-83|<>:prp#80-83$<i>15<s>32|<>:np#80-83$<i>15<s>37|<>:s#80-96$<i>18<s>15]"
+                        + "[(80-83)s:you|_14#80-83|<>:prp#80-83$<i>15<s>32|<>:np#80-83$<i>15<s>37|<>:s#80-96$<i>18<s>15]"
 
-                + "[(84-88)s:hold|_15#84-88|<>:vb#84-88$<i>16<s>33|<>:vp#84-96$<i>18<s>16|"
+                        + "[(84-88)s:hold|_15#84-88|<>:vb#84-88$<i>16<s>33|<>:vp#84-96$<i>18<s>16|"
                         + ">:stanford/d:tag$<i>13<s>14<s>33<s>31|"
                         + ">:stanford/d:tag$<i>14<s>15<s>33<s>32|"
                         + ">:stanford/d:tag$<i>17<s>16<s>33<s>35]"
 
-                + "[(89-91)s:so|_16#89-91|<>:rb#89-91$<i>17<s>341|<>:adjp#89-96$<i>18<s>17]"
+                        + "[(89-91)s:so|_16#89-91|<>:rb#89-91$<i>17<s>341|<>:adjp#89-96$<i>18<s>17]"
 
-                + "[(92-96)s:dear|_17#92-96|<>:jj#92-96$<i>18<s>35|"
-                        + ">:stanford/d:tag$<i>16<s>17<s>35<s>34]"
-                 );
-                        
+                        + "[(92-96)s:dear|_17#92-96|<>:jj#92-96$<i>18<s>35|"
+                        + ">:stanford/d:tag$<i>16<s>17<s>35<s>34]");
+
         return fd;
     }
 }
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 63d2a64..8618592 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestRelationIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestRelationIndex.java
@@ -289,6 +289,7 @@
         assertEquals(4, kr.getMatch(5).getEndPos());
     }
 
+
     /**
      * Relations only with/out attribute
      * */
@@ -307,9 +308,8 @@
 
         // child-of with attr func=sbj
         SpanWithAttributeQuery wq = new SpanWithAttributeQuery(fq,
-                new SpanAttributeQuery(
-                        new SpanTermQuery(new Term("base", "r@:func=sbj")),
-                        true), true);
+                new SpanAttributeQuery(new SpanTermQuery(new Term("base",
+                        "r@:func=sbj")), true), true);
 
         kr = ki.search(wq, (short) 20);
         assertEquals((long) 1, kr.getTotalResults());
@@ -385,11 +385,12 @@
         assertEquals(7, kr.getMatch(6).getEndPos());
     }
 
+
     /**
      * Dependency relations with attribute
      * */
     @Test
-    public void testCase5() throws IOException {
+    public void testCase5 () throws IOException {
         ki.addDoc(createFieldDoc2());
         ki.commit();
 
@@ -430,6 +431,7 @@
 
     }
 
+
     /**
      * Relation with specific sources, return any targets
      * */
@@ -485,11 +487,11 @@
         ki.commit();
 
         //return source of dep relations to pos:NN
-        
+
         SpanTermWithIdQuery tq = new SpanTermWithIdQuery(new Term("base",
                 "pos:NN"), true);
-        SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base",
-                "<:dep")), true);
+        SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(
+                new Term("base", "<:dep")), true);
         srq.setSourceClass((byte) 1);
         SpanRelationMatchQuery rm = new SpanRelationMatchQuery(srq, tq, true);
         SpanQuery rv = new SpanFocusQuery(rm, (byte) 1);
@@ -505,8 +507,7 @@
 
         // return target of dep relations from pos:NN
         srq = new SpanRelationQuery(
-                new SpanTermQuery(
-                new Term("base", ">:dep")), true);
+                new SpanTermQuery(new Term("base", ">:dep")), true);
         srq.setTargetClass((byte) 1);
         rm = new SpanRelationMatchQuery(srq, tq, true);
         rv = new SpanFocusQuery(rm, (byte) 1);
@@ -525,7 +526,8 @@
 
     /**
      * Relation with specific sources and return any targets <br/>
-     * Relation with specific sources and targets, return the targets <br/>
+     * Relation with specific sources and targets, return the targets
+     * <br/>
      * Relation with specific sources and targets, return the sources
      * 
      * @throws IOException
@@ -606,8 +608,9 @@
 
     }
 
+
     @Test
-    public void testCase10() throws IOException {
+    public void testCase10 () throws IOException {
         ki.addDoc(createFieldDoc2());
         ki.commit();
         SpanElementQuery seq1 = new SpanElementQuery("base", "np");
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
index c0e1cf5..df00673 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
@@ -245,6 +245,7 @@
         assertEquals(sqwi.toQuery().toString(), "tokens:base/p:foo");
     };
 
+
     @Test
     public void queryJSONBspClass () throws QueryException {
         SpanQueryWrapper sqwi = jsonQuery(getClass().getResource(
@@ -450,46 +451,40 @@
                 "spanOverlap(<tokens:s />, spanNext(tokens:tt/p:CARD, SpanMultiTermQueryWrapper(tokens:/tt/p:N.*/)))");
     };
 
+
     @Test
     public void queryJSONflags1 () throws QueryException {
         // buchstabe/i
         SpanQueryWrapper sqwi = jsonQuery(getClass().getResource(
                 "/queries/flags/caseInsensitive.jsonld").getFile());
 
-        assertEquals(
-                sqwi.toQuery().toString(),
-                "tokens:i:buchstabe");
+        assertEquals(sqwi.toQuery().toString(), "tokens:i:buchstabe");
     };
 
+
     @Test
     public void queryJSONflags2 () throws QueryException {
         // buchstabe/i
         try {
-            String json = getString(
-                getClass().getResource(
-                    "/queries/flags/unknown1.jsonld"
-                ).getFile()
-            );
+            String json = getString(getClass().getResource(
+                    "/queries/flags/unknown1.jsonld").getFile());
             KrillQuery kq = new KrillQuery("tokens");
-            assertEquals(kq.fromJson(json).toQuery().toString(),"tokens:s:buchstabe");
-            assertEquals(kq.getWarning(0).getCode(), 748);
-            
-            json = getString(
-                getClass().getResource(
-                    "/queries/flags/unknown2.jsonld"
-                ).getFile()
-            );
-            kq = new KrillQuery("tokens");
-            assertEquals(kq.fromJson(json).toQuery().toString(),"tokens:i:buchstabe");
+            assertEquals(kq.fromJson(json).toQuery().toString(),
+                    "tokens:s:buchstabe");
             assertEquals(kq.getWarning(0).getCode(), 748);
 
-            json = getString(
-                getClass().getResource(
-                    "/queries/flags/unknown3.jsonld"
-                ).getFile()
-            );
+            json = getString(getClass().getResource(
+                    "/queries/flags/unknown2.jsonld").getFile());
             kq = new KrillQuery("tokens");
-            assertEquals(kq.fromJson(json).toQuery().toString(),"tokens:i:buchstabe");
+            assertEquals(kq.fromJson(json).toQuery().toString(),
+                    "tokens:i:buchstabe");
+            assertEquals(kq.getWarning(0).getCode(), 748);
+
+            json = getString(getClass().getResource(
+                    "/queries/flags/unknown3.jsonld").getFile());
+            kq = new KrillQuery("tokens");
+            assertEquals(kq.fromJson(json).toQuery().toString(),
+                    "tokens:i:buchstabe");
             assertEquals(kq.getWarning(0).getCode(), 748);
 
         }
@@ -498,6 +493,7 @@
         };
     };
 
+
     public static String getString (String path) {
         StringBuilder contentBuilder = new StringBuilder();
         try {
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanReferenceQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanReferenceQueryJSON.java
index 57c29bc..a401916 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanReferenceQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanReferenceQueryJSON.java
@@ -14,7 +14,7 @@
 public class TestSpanReferenceQueryJSON {
 
     @Test
-    public void testFirstOperandRef() throws IOException, QueryException {
+    public void testFirstOperandRef () throws IOException, QueryException {
 
         String filepath = getClass().getResource(
                 "/queries/reference/first-operand-reference.jsonld").getFile();
@@ -30,8 +30,9 @@
                 sq.toString());
     }
 
+
     @Test
-    public void testSecondOperandRef() throws QueryException {
+    public void testSecondOperandRef () throws QueryException {
 
         String filepath = getClass().getResource(
                 "/queries/reference/second-operand-reference.jsonld").getFile();
@@ -47,8 +48,9 @@
                         + "{3: tokens:p:P}), 3)", sq.toString());
     }
 
+
     @Test
-    public void testMultipleReferences() throws QueryException {
+    public void testMultipleReferences () throws QueryException {
         String filepath = getClass().getResource(
                 "/queries/reference/multiple-references.jsonld").getFile();
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
@@ -60,55 +62,38 @@
                 "spanReference(focus(#[1,2]spanSegment(focus(#2: spanSegment(spanRelation(tokens:>:mate/d:HEAD), "
                         + "focus(2: spanReference(spanNext(focus(1: spanNext(focus(2: spanNext({1: <tokens:c:VP />}, "
                         + "{2: <tokens:c:NP />})), {3: <tokens:c:PP />})), {3: <tokens:c:PP />}), 3)))), "
-                        + "{1: <tokens:c:VP />})), 1)",
-                sq.toString());
+                        + "{1: <tokens:c:VP />})), 1)", sq.toString());
     }
 
+
     @Test
-    public void testDistanceReferences() throws QueryException {
+    public void testDistanceReferences () throws QueryException {
         String filepath = getClass().getResource(
-            "/queries/reference/bug-multiple-distance-simple.jsonld"
-        ).getFile();
+                "/queries/reference/bug-multiple-distance-simple.jsonld")
+                .getFile();
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
         SpanQuery sq = sqwi.toQuery();
 
-        assertEquals("spanDistance(<tokens:c:vb />, <tokens:c:prp />, [(w[1:1], notOrdered, notExcluded)])", sq.toString());
+        assertEquals(
+                "spanDistance(<tokens:c:vb />, <tokens:c:prp />, [(w[1:1], notOrdered, notExcluded)])",
+                sq.toString());
 
         // "/queries/reference/distance-reference.jsonld").getFile();
         filepath = getClass().getResource(
-            "/queries/reference/bug-multiple-distance.jsonld"
-        ).getFile();
+                "/queries/reference/bug-multiple-distance.jsonld").getFile();
         sqwi = getJSONQuery(filepath);
         sq = sqwi.toQuery();
 
         // 'cat="VP" & cat="NP" & cat="PP" & #1 . #2 & #2 . #3 & #1 .
         // #3 & #2 ->dep #1'
-        assertEquals(
-        "spanReference("+
-          "focus("+
-            "#[1,2]spanSegment("+
-              "focus("+
-                "#2: spanSegment("+
-                  "spanRelation(tokens:>:stanford/d:tag), " +
-                    "focus("+
-                      "2: spanDistance("+
-                        "focus("+
-                          "1: spanDistance("+
-                            "<tokens:c:vb />, "+
-                            "{1: <tokens:c:prp />}, " +
-                            "[(w[1:1], notOrdered, notExcluded)]"+
-                          ")"+
-                        "), "+
-                        "{2: <tokens:c:nn />}, "+
-                        "[(w[0:2], ordered, notExcluded)]"+
-                      ")"+
-                    ")"+
-                  ")"+
-                "), " +
-                "{1: <tokens:c:prp />}"+
-              ")"+
-            "), 1"+
-          ")",
-        sq.toString());
+        assertEquals("spanReference(" + "focus(" + "#[1,2]spanSegment("
+                + "focus(" + "#2: spanSegment("
+                + "spanRelation(tokens:>:stanford/d:tag), " + "focus("
+                + "2: spanDistance(" + "focus(" + "1: spanDistance("
+                + "<tokens:c:vb />, " + "{1: <tokens:c:prp />}, "
+                + "[(w[1:1], notOrdered, notExcluded)]" + ")" + "), "
+                + "{2: <tokens:c:nn />}, " + "[(w[0:2], ordered, notExcluded)]"
+                + ")" + ")" + ")" + "), " + "{1: <tokens:c:prp />}" + ")"
+                + "), 1" + ")", sq.toString());
     }
 }
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanRelationQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanRelationQueryJSON.java
index fccd34a..b051509 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanRelationQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanRelationQueryJSON.java
@@ -89,6 +89,7 @@
                 sq.toString());
     }
 
+
     @Test
     public void testFocusSource () throws QueryException {
         //
@@ -101,8 +102,9 @@
                 sq.toString());
     }
 
+
     @Test
-    public void testFocusTarget() throws QueryException {
+    public void testFocusTarget () throws QueryException {
         String filepath = getClass().getResource(
                 "/queries/relation/focus-target.json").getFile();
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
@@ -113,8 +115,9 @@
                 sq.toString());
     }
 
+
     @Test
-    public void testFocusEmptyTarget() throws QueryException {
+    public void testFocusEmptyTarget () throws QueryException {
         String filepath = getClass().getResource(
                 "/queries/relation/focus-empty-target.json").getFile();
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
@@ -124,8 +127,9 @@
                 sq.toString());
     }
 
+
     @Test
-    public void testFocusEmptyBoth() throws QueryException {
+    public void testFocusEmptyBoth () throws QueryException {
         String filepath = getClass().getResource(
                 "/queries/relation/focus-empty-both.json").getFile();
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
diff --git a/src/test/java/de/ids_mannheim/korap/response/TestResponse.java b/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
index 423af96..b93ddfb 100644
--- a/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
+++ b/src/test/java/de/ids_mannheim/korap/response/TestResponse.java
@@ -84,6 +84,7 @@
         assertEquals("Fehler 4", respJson.at("/errors/0/1").asText());
     };
 
+
     // TODO: Skip this for the moment and refactor later
     @Ignore
     public void testResponseDeserialzation () throws IOException {
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestKrill.java b/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
index 8c4cd40..fd2e7c3 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestKrill.java
@@ -188,10 +188,12 @@
         assertEquals(kr.getTotalResults(), 1);
     };
 
+
     // Todo: There SHOULD be a failure here, but Koral currently creates empty collections
     @Test
     public void queryJSONapiTest1 () {
-        Krill test = new Krill("{\"@context\":\"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld\",\"errors\":[],\"warnings\":[],\"messages\":[],\"collection\":{},\"query\":{\"@type\":\"koral:token\",\"wrap\":{\"@type\":\"koral:term\",\"layer\":\"orth\",\"key\":\"Baum\",\"match\":\"match:eq\"}},\"meta\":{}}");
+        Krill test = new Krill(
+                "{\"@context\":\"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld\",\"errors\":[],\"warnings\":[],\"messages\":[],\"collection\":{},\"query\":{\"@type\":\"koral:token\",\"wrap\":{\"@type\":\"koral:term\",\"layer\":\"orth\",\"key\":\"Baum\",\"match\":\"match:eq\"}},\"meta\":{}}");
         assertFalse(test.hasErrors());
     };
 
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestResult.java b/src/test/java/de/ids_mannheim/korap/search/TestResult.java
index 7425cfd..6d9a4d5 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestResult.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestResult.java
@@ -149,13 +149,13 @@
         Krill ks = new Krill(json);
         Result kr = ks.apply(ki);
         assertEquals((long) 7, kr.getTotalResults());
-        
+
         ObjectMapper mapper = new ObjectMapper();
         JsonNode res = mapper.readTree(kr.toJsonString());
 
         assertEquals(7, res.at("/meta/totalResults").asInt());
-        assertEquals("spanOr([tokens:s:a, tokens:s:b])", res.at("/meta/serialQuery")
-                .asText());
+        assertEquals("spanOr([tokens:s:a, tokens:s:b])",
+                res.at("/meta/serialQuery").asText());
         assertEquals(5, res.at("/meta/itemsPerPage").asInt());
         assertEquals(0, res.at("/meta/startIndex").asInt());
 
@@ -175,24 +175,19 @@
 
         // Query
         assertEquals("koral:group", res.at("/query/@type").asText());
-        assertEquals("operation:or", res.at("/query/operation")
-                .asText());
+        assertEquals("operation:or", res.at("/query/operation").asText());
 
-        assertEquals("koral:token", res.at("/query/operands/0/@type")
+        assertEquals("koral:token", res.at("/query/operands/0/@type").asText());
+        assertEquals("koral:term", res.at("/query/operands/0/wrap/@type")
                 .asText());
-        assertEquals("koral:term",
-                res.at("/query/operands/0/wrap/@type").asText());
-        assertEquals("orth", res.at("/query/operands/0/wrap/layer")
-                .asText());
+        assertEquals("orth", res.at("/query/operands/0/wrap/layer").asText());
         assertEquals("a", res.at("/query/operands/0/wrap/key").asText());
         assertEquals("match:eq", res.at("/query/operands/0/wrap/match")
                 .asText());
-        assertEquals("koral:token", res.at("/query/operands/1/@type")
+        assertEquals("koral:token", res.at("/query/operands/1/@type").asText());
+        assertEquals("koral:term", res.at("/query/operands/1/wrap/@type")
                 .asText());
-        assertEquals("koral:term",
-                res.at("/query/operands/1/wrap/@type").asText());
-        assertEquals("orth", res.at("/query/operands/1/wrap/layer")
-                .asText());
+        assertEquals("orth", res.at("/query/operands/1/wrap/layer").asText());
         assertEquals("b", res.at("/query/operands/1/wrap/key").asText());
         assertEquals("match:eq", res.at("/query/operands/1/wrap/match")
                 .asText());
@@ -240,8 +235,8 @@
         JsonNode res = mapper.readTree(kr.toTokenListJsonString());
 
         assertEquals(3, res.at("/meta/totalResults").asInt());
-        assertEquals("spanNext(base:s:a, base:s:b)", res.at("/meta/serialQuery")
-                .asText());
+        assertEquals("spanNext(base:s:a, base:s:b)", res
+                .at("/meta/serialQuery").asText());
         assertEquals(0, res.at("/meta/startIndex").asInt());
         assertEquals(25, res.at("/meta/itemsPerPage").asInt());
 
diff --git a/src/test/resources/queries/collections/collection_6.jsonld b/src/test/resources/queries/collections/collection_6.jsonld
new file mode 100644
index 0000000..5e60008
--- /dev/null
+++ b/src/test/resources/queries/collections/collection_6.jsonld
@@ -0,0 +1,19 @@
+{
+  "@context":"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+  "errors":[],
+  "warnings":[],
+  "messages":[],
+  "query":{
+    "@type":"koral:token",
+    "wrap":{
+      "@type":"koral:term",
+      "layer":"orth",
+      "key":"der",
+      "match":"match:eq",
+      "foundry":"opennlp"
+    }
+  },
+  "meta":{
+    "cutOff":null
+  }
+}
diff --git a/src/test/resources/queries/collections/collection_6_withCollection.jsonld b/src/test/resources/queries/collections/collection_6_withCollection.jsonld
new file mode 100644
index 0000000..91d960a
--- /dev/null
+++ b/src/test/resources/queries/collections/collection_6_withCollection.jsonld
@@ -0,0 +1,25 @@
+{
+  "@context":"http://korap.ids-mannheim.de/ns/koral/0.3/context.jsonld",
+  "errors":[],
+  "warnings":[],
+  "messages":[],
+  "collection":{
+    "@type":"koral:doc",
+    "key":"textClass",
+    "value":"reisen",
+    "match":"match:eq"
+  },
+  "query":{
+    "@type":"koral:token",
+    "wrap":{
+      "@type":"koral:term",
+      "layer":"orth",
+      "key":"der",
+      "match":"match:eq",
+      "foundry":"opennlp"
+    }
+  },
+  "meta":{
+    "cutOff":null
+  }
+}