Finalized test suite adoption from legacy collection

Change-Id: I594597f2364df68007c22c8c78fc54f4385d2ccf
diff --git a/src/main/java/de/ids_mannheim/korap/KrillCollection.java b/src/main/java/de/ids_mannheim/korap/KrillCollection.java
index f9fcc14..ceb6671 100644
--- a/src/main/java/de/ids_mannheim/korap/KrillCollection.java
+++ b/src/main/java/de/ids_mannheim/korap/KrillCollection.java
@@ -48,7 +48,7 @@
     private KrillIndex index;
     private JsonNode json;
     private CollectionBuilder cb = new CollectionBuilder();
-    private CollectionBuilder.CollectionBuilderInterface cbi;
+    private CollectionBuilder.Interface cbi;
     private byte[] pl = new byte[4];
     private static ByteBuffer bb = ByteBuffer.allocate(4);
 
@@ -155,7 +155,7 @@
     };
 
 
-    private CollectionBuilder.CollectionBuilderInterface _fromJson (JsonNode json) throws QueryException {
+    private CollectionBuilder.Interface _fromJson (JsonNode json) throws QueryException {
 
         if (!json.has("@type")) {
             throw new QueryException(701,
@@ -261,7 +261,7 @@
             if (!json.has("operands") || !json.get("operands").isArray())
                 throw new QueryException(842, "Document group needs operand list");
 
-            CollectionBuilder.CollectionBuilderGroup group;
+            CollectionBuilder.Group group;
 
             String operation = "operation:and";
             if (json.has("operation"))
@@ -296,12 +296,12 @@
      * 
      * @param cb The CollectionBuilder object.
      */
-    public KrillCollection fromBuilder (CollectionBuilder.CollectionBuilderInterface cbi) {
+    public KrillCollection fromBuilder (CollectionBuilder.Interface cbi) {
         this.cbi = cbi;
         return this;
     };
 
-    public CollectionBuilder.CollectionBuilderInterface getBuilder () {
+    public CollectionBuilder.Interface getBuilder () {
         return this.cbi;
     };
 
@@ -310,11 +310,11 @@
         return this.cb;
     };
 
-    public KrillCollection filter (CollectionBuilder.CollectionBuilderInterface filter) {
+    public KrillCollection filter (CollectionBuilder.Interface filter) {
         return this.fromBuilder(this.cb.andGroup().with(this.cbi).with(filter));
     };
 
-    public KrillCollection extend (CollectionBuilder.CollectionBuilderInterface extension) {
+    public KrillCollection extend (CollectionBuilder.Interface extension) {
         return this.fromBuilder(this.cb.orGroup().with(this.cbi).with(extension));
     };
 
@@ -331,28 +331,13 @@
      * @return The {@link KrillCollection} object for chaining.
      */
     public KrillCollection filterUIDs (String ... uids) {
-        CollectionBuilder.CollectionBuilderInterface root = this.getBuilder();
-        CollectionBuilder.CollectionBuilderGroup cbg = this.cb.orGroup();
-        CollectionBuilder.CollectionBuilderGroup filter = this.cb.andGroup();
+        CollectionBuilder.Interface root = this.getBuilder();
+        CollectionBuilder.Group cbg = this.cb.orGroup();
+        CollectionBuilder.Group filter = this.cb.andGroup();
         for (String uid : uids) {
             cbg.with(this.cb.term("UID", uid));
         };
-        this.filter(cbg);
-        /*
-        if (this.getBuilder() != null)
-            filter.with(this.getBuilder());
-        filter.with(cbg);
-
-        this.fromBuilder(filter);
-        */
-        /*
-        BooleanFilter filter = new BooleanFilter();
-        filter.or("UID", uids);
-        if (DEBUG)
-            log.debug("UID based filter: {}", filter.toString());
-        return this.filter(filter);
-        */
-        return this;
+        return this.filter(cbg);
     };
 
 
@@ -684,13 +669,13 @@
 
 
     /*
-    @Deprecated
-    public HashMap getTermRelation (String field) throws Exception {
-        return this.getTermRelation(new KrillCollection(this), field);
-    };
-*/
+      @Deprecated
+      public HashMap getTermRelation (String field) throws Exception {
+          return this.getTermRelation(new KrillCollection(this), field);
+      };
+    */
 
-    /**
+    /*
      * Analyze how terms relate
      */
     /*
diff --git a/src/main/java/de/ids_mannheim/korap/collection/CollectionBuilder.java b/src/main/java/de/ids_mannheim/korap/collection/CollectionBuilder.java
index fb26168..836aaa1 100644
--- a/src/main/java/de/ids_mannheim/korap/collection/CollectionBuilder.java
+++ b/src/main/java/de/ids_mannheim/korap/collection/CollectionBuilder.java
@@ -17,8 +17,9 @@
 
 /*
  * TODO: Optimize!
- * - Remove multiple times the same object in Boolean groups.
+ * - Remove identical object in Boolean groups
  * - Flatten boolean groups
+ * - create "between" ranges for multiple date objects
  */
 
 public class CollectionBuilder {
@@ -29,30 +30,30 @@
     // This advices the java compiler to ignore all loggings
     public static final boolean DEBUG = false;
 
-    public CollectionBuilderInterface term (String field, String term) {
-        return new CollectionBuilderTerm(field, term);
+    public CollectionBuilder.Interface term (String field, String term) {
+        return new CollectionBuilder.Term(field, term);
     };
 
-    public CollectionBuilderInterface re (String field, String term) {
-        return new CollectionBuilderTerm(field, term, true);
+    public CollectionBuilder.Interface re (String field, String term) {
+        return new CollectionBuilder.Term(field, term, true);
     };
 
-    public CollectionBuilderInterface since (String field, String date) {
+    public CollectionBuilder.Interface since (String field, String date) {
         int since = new KrillDate(date).floor();
 
         if (since == 0 || since == KrillDate.BEGINNING)
             return null;
 
-        return new CollectionBuilderRange(field, since, KrillDate.END);
+        return new CollectionBuilder.Range(field, since, KrillDate.END);
     };
 
-    public CollectionBuilderInterface till (String field, String date) {
+    public CollectionBuilder.Interface till (String field, String date) {
         try {
             int till = new KrillDate(date).ceil();
             if (till == 0 || till == KrillDate.END)
                 return null;
 
-            return new CollectionBuilderRange(field, KrillDate.BEGINNING, till);
+            return new CollectionBuilder.Range(field, KrillDate.BEGINNING, till);
         }
         catch (NumberFormatException e) {
             log.warn("Parameter of till(date) is invalid");
@@ -60,7 +61,20 @@
         return null;
     };
 
-    public CollectionBuilderInterface date (String field, String date) {
+    // This will be optimized away in future versions
+    public CollectionBuilder.Interface between (String field, String start, String end) {
+        CollectionBuilder.Interface startObj = this.since(field, start);
+        if (startObj == null)
+            return null;
+
+        CollectionBuilder.Interface endObj = this.till(field, end);
+        if (endObj == null)
+            return null;
+
+        return this.andGroup().with(startObj).with(endObj);
+    };
+
+    public CollectionBuilder.Interface date (String field, String date) {
         KrillDate dateDF = new KrillDate(date);
 
         if (dateDF.year == 0)
@@ -74,39 +88,39 @@
                 || (begin == KrillDate.BEGINNING && end == KrillDate.END))
                 return null;
 
-            return new CollectionBuilderRange(field, begin, end);
+            return new CollectionBuilder.Range(field, begin, end);
         };
 
-        return new CollectionBuilderRange(field, dateDF.floor(), dateDF.ceil());
+        return new CollectionBuilder.Range(field, dateDF.floor(), dateDF.ceil());
     };
 
-    public CollectionBuilderGroup andGroup () {
-        return new CollectionBuilderGroup(false);
+    public CollectionBuilder.Group andGroup () {
+        return new CollectionBuilder.Group(false);
     };
 
-    public CollectionBuilderGroup orGroup () {
-        return new CollectionBuilderGroup(true);
+    public CollectionBuilder.Group orGroup () {
+        return new CollectionBuilder.Group(true);
     };
 
-    public interface CollectionBuilderInterface {
+    public interface Interface {
         public String toString ();
         public Filter toFilter ();
         public boolean isNegative ();
-        public CollectionBuilderInterface not ();
+        public CollectionBuilder.Interface not ();
     };
 
-    public class CollectionBuilderTerm implements CollectionBuilderInterface {
+    public class Term implements CollectionBuilder.Interface {
         private boolean isNegative = false;
         private boolean regex = false;
         private String field;
         private String term;
 
-        public CollectionBuilderTerm (String field, String term) {
+        public Term (String field, String term) {
             this.field = field;
             this.term = term;
         };
 
-        public CollectionBuilderTerm (String field, String term, boolean regex) {
+        public Term (String field, String term, boolean regex) {
             this.field = field;
             this.term = term;
             this.regex = regex;
@@ -116,15 +130,18 @@
             // Regular expression
             if (this.regex)
                 return new QueryWrapperFilter(
-                    new RegexpQuery(new Term(this.field, this.term))
+                    new RegexpQuery(new org.apache.lucene.index.Term(this.field, this.term))
                 );
             
             // Simple term
-            return new TermsFilter(new Term(this.field, this.term));
+            return new TermsFilter(new org.apache.lucene.index.Term(this.field, this.term));
         };
 
         public String toString () {
-            return this.toFilter().toString();
+            Filter filter = this.toFilter();
+            if (filter == null)
+                return "";
+            return filter.toString();
         };
 
         public boolean isNegative () {
@@ -132,13 +149,13 @@
         };
 
 
-        public CollectionBuilderInterface not () {
+        public CollectionBuilder.Interface not () {
             this.isNegative = true;
             return this;
         };
     };
 
-    public class CollectionBuilderGroup implements CollectionBuilderInterface {
+    public class Group implements CollectionBuilder.Interface {
         private boolean isOptional = false;
         private boolean isNegative = true;
 
@@ -150,14 +167,14 @@
             return this.isOptional;
         };
 
-        private ArrayList<CollectionBuilderInterface> operands;
+        private ArrayList<CollectionBuilder.Interface> operands;
 
-        public CollectionBuilderGroup (boolean optional) {
+        public Group (boolean optional) {
             this.isOptional = optional;
-            this.operands = new ArrayList<CollectionBuilderInterface>(3);
+            this.operands = new ArrayList<CollectionBuilder.Interface>(3);
         };
 
-        public CollectionBuilderGroup with (CollectionBuilderInterface cb) {
+        public Group with (CollectionBuilder.Interface cb) {
             if (cb == null)
                 return this;
 
@@ -167,6 +184,11 @@
             return this;
         };
 
+        public Group with (String field, String term) {
+            if (field == null || term == null)
+                return this;
+            return this.with(new CollectionBuilder.Term(field, term));
+        };
 
         public Filter toFilter () {
             if (this.operands == null || this.operands.isEmpty())
@@ -178,9 +200,9 @@
             // BooleanFilter bool = new BooleanFilter();
             BooleanGroupFilter bool = new BooleanGroupFilter(this.isOptional);
 
-            Iterator<CollectionBuilderInterface> i = this.operands.iterator();
+            Iterator<CollectionBuilder.Interface> i = this.operands.iterator();
             while (i.hasNext()) {
-                CollectionBuilderInterface cb = i.next();
+                CollectionBuilder.Interface cb = i.next();
                 if (cb.isNegative()) {
                     bool.without(cb.toFilter());
                 }
@@ -193,21 +215,24 @@
         };
 
         public String toString () {
-            return this.toFilter().toString();
+            Filter filter = this.toFilter();
+            if (filter == null)
+                return "";
+            return filter.toString();
         };
 
-        public CollectionBuilderInterface not () {
+        public CollectionBuilder.Interface not () {
             this.isNegative = true;
             return this;
         };
     };
 
-    public class CollectionBuilderRange implements CollectionBuilderInterface {
+    public class Range implements CollectionBuilder.Interface {
         private boolean isNegative = false;
         private String field;
         private int start, end;
 
-        public CollectionBuilderRange (String field, int start, int end) {
+        public Range (String field, int start, int end) {
             this.field = field;
             this.start = start;
             this.end = end;
@@ -218,7 +243,10 @@
         };
 
         public String toString () {
-            return this.toFilter().toString();
+            Filter filter = this.toFilter();
+            if (filter == null)
+                return "";
+            return filter.toString();
         };
 
         public Filter toFilter () {
@@ -229,7 +257,7 @@
                                                   true);
         };
 
-        public CollectionBuilderInterface not () {
+        public CollectionBuilder.Interface not () {
             this.isNegative = true;
             return this;
         };
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilder.java b/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilder.java
new file mode 100644
index 0000000..9dd7baf
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilder.java
@@ -0,0 +1,416 @@
+package de.ids_mannheim.korap.collection;
+
+import java.util.*;
+import java.io.*;
+
+import de.ids_mannheim.korap.collection.CollectionBuilder;
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+import org.junit.Ignore;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+// TODO: More extensive testing!
+
+
+@RunWith(JUnit4.class)
+public class TestCollectionBuilder {
+
+    @Test
+    public void builderTerm () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("author:tree",
+                     kc.term("author", "tree").toString());
+    };
+
+    @Test
+    public void builderRegex () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("QueryWrapperFilter(author:/tre*?/)",
+                     kc.re("author", "tre*?").toString());
+    };
+
+    @Test
+    public void builderDateYear () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("pubDate:[20050000 TO 20059999]",
+                     kc.date("pubDate", "2005").toString());
+    };
+
+    @Test
+    public void builderDateMonth () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("pubDate:[20051000 TO 20051099]",
+                     kc.date("pubDate", "2005-10").toString());
+    };
+
+    @Test
+    public void builderDateDay () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("pubDate:[20051011 TO 20051011]",
+                     kc.date("pubDate", "2005-10-11").toString());
+    };
+
+    @Test
+    public void builderDateBorders () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        // CollectionBuilderNew.Interface kbi = ;
+        assertNull(kc.date("pubDate", ""));
+
+        assertEquals("pubDate:[20051580 TO 20051580]",
+                     kc.date("pubDate", "2005-15-80").toString());
+
+        assertNull(kc.date("pubDate", "2005-15-8"));
+        assertNull(kc.date("pubDate", "2005-5-18"));
+        assertNull(kc.date("pubDate", "200-05-18"));
+    };
+
+    @Test
+    public void builderSince () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("pubDate:[20050000 TO 99999999]",
+                     kc.since("pubDate", "2005").toString());
+
+        assertEquals("pubDate:[20051000 TO 99999999]",
+                     kc.since("pubDate", "2005-10").toString());
+
+        assertEquals("pubDate:[20051012 TO 99999999]",
+                     kc.since("pubDate", "2005-10-12").toString());
+    };
+
+
+    @Test
+    public void builderTill () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("pubDate:[0 TO 20059999]",
+                     kc.till("pubDate", "2005").toString());
+
+        assertEquals("pubDate:[0 TO 20051299]",
+                     kc.till("pubDate", "2005-12").toString());
+
+        assertEquals("pubDate:[0 TO 20051204]",
+                     kc.till("pubDate", "2005-12-04").toString());
+    };
+
+
+    @Test
+    public void builderAndSimple () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("author:tree", kc.andGroup().with(kc.term("author", "tree")).toString());
+    };
+
+    @Test
+    public void builderOrSimple () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("author:tree", kc.orGroup().with(kc.term("author", "tree")).toString());
+    };
+
+    @Test
+    public void builderAndCombined () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("AndGroup(author:tree title:name)",
+                     kc.andGroup().with(kc.term("author", "tree"))
+                     .with(kc.term("title", "name")).toString());
+    };
+
+    @Test
+    public void builderAndNestedSimple () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("AndGroup(author:tree title:name)",
+                     kc.andGroup().with(kc.andGroup().with(kc.term("author", "tree")).with(kc.term("title", "name"))).toString());
+    };
+
+
+    @Test
+    public void builderOrCombined () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("OrGroup(author:tree title:name)",
+                     kc.orGroup().with(kc.term("author", "tree"))
+                     .with(kc.term("title", "name")).toString());
+    };
+
+    @Test
+    public void builderOrNestedSimple () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        assertEquals("OrGroup(author:tree title:name)",
+                     kc.orGroup().with(kc.orGroup().with(kc.term("author", "tree"))
+                                .with(kc.term("title", "name"))).toString()
+                     );
+    };
+
+    @Test
+    public void builderGroups () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        String g = kc.orGroup().with(
+                                     kc.orGroup().with(kc.term("author", "tree1")).with(kc.term("title", "name1"))
+        ).with(
+               kc.andGroup().with(kc.term("author", "tree2")).with(kc.term("title", "name2"))
+               ).toString();
+        assertEquals("OrGroup(OrGroup(author:tree1 title:name1) AndGroup(author:tree2 title:name2))", g);
+    };
+
+    @Test
+    public void builderNegationRoot () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        CollectionBuilder.Interface kbi = kc.orGroup().with(kc.term("author", "tree1")).with(kc.term("title", "name1"));
+        assertEquals(
+                     "OrGroup(author:tree1 title:name1)",
+                     kbi.toString());
+        assertFalse(kbi.isNegative());
+
+        kbi = kc.andGroup().with(
+                                 kc.orGroup().with(kc.term("author", "tree1")).with(kc.term("title", "name1"))
+              ).not();
+        assertEquals("OrGroup(author:tree1 title:name1)", kbi.toString());
+        assertTrue(kbi.isNegative());
+    };
+
+
+    @Test
+    public void builderNegation () throws IOException {
+        CollectionBuilder kc = new CollectionBuilder();
+        CollectionBuilder.Interface kbi =
+            kc.term("author", "tree").not();
+        assertEquals("author:tree", kbi.toString());
+        assertTrue(kbi.isNegative());
+
+        kbi = kc.andGroup().with(kc.term("author", "tree").not());
+        assertEquals("author:tree", kbi.toString());
+        assertTrue(kbi.isNegative());
+
+        kbi = kc.orGroup().with(kc.term("author", "tree").not());
+        assertEquals("author:tree", kbi.toString());
+        assertTrue(kbi.isNegative());
+    };
+
+    // The legacy tests were adopted from the legacy collection builder and reformuated
+
+    @Test
+    public void LegacyFilterExample () throws IOException {
+        CollectionBuilder kf = new CollectionBuilder();
+
+        /*
+        assertEquals("+textClass:tree", kf.and("textClass", "tree").toString());
+        */
+        assertEquals("textClass:tree", kf.andGroup().with(kf.term("textClass", "tree")).toString());
+
+        /*
+        assertEquals("+textClass:tree +textClass:sport",
+                kf.and("textClass", "tree").and("textClass", "sport")
+                        .toString());
+        */
+        assertEquals("AndGroup(textClass:tree textClass:sport)", kf.andGroup().with(kf.term("textClass", "tree")).with(kf.term("textClass", "sport")).toString());
+
+        /*
+        assertEquals(
+                "+textClass:tree +textClass:sport textClass:news",
+                kf.and("textClass", "tree").and("textClass", "sport")
+                        .or("textClass", "news").toString());
+        */
+        assertEquals("OrGroup(AndGroup(textClass:tree textClass:sport) textClass:news)", kf.orGroup().with(kf.andGroup().with("textClass", "tree").with("textClass", "sport")).with("textClass", "news").toString());
+
+        /*
+        assertEquals("+textClass:tree +textClass:sport +textClass:news", kf
+                .and("textClass", "tree", "sport", "news").toString());
+        */
+        assertEquals("AndGroup(textClass:tree textClass:sport textClass:news)", kf.andGroup().with("textClass", "tree").with("textClass", "sport").with("textClass", "news").toString());
+
+        /*
+        assertEquals("corpusID:c-1 corpusID:c-2 corpusID:c-3",
+                kf.or("corpusID", "c-1", "c-2", "c-3").toString());
+        */
+        assertEquals("OrGroup(corpusID:c-1 corpusID:c-2 corpusID:c-3)", kf.orGroup().with("corpusID", "c-1").with("corpusID", "c-2").with("corpusID", "c-3").toString());
+
+    };
+
+    @Test
+    public void LegacyRangeExample () throws IOException {
+        CollectionBuilder kf = new CollectionBuilder();
+        /*
+        assertEquals("+pubDate:[20030604 TO 20030899]",
+                kf.between("2003-06-04", "2003-08-99").toString());
+        */
+        // This will be optimized and probably crash
+        assertEquals("AndGroup(pubDate:[20030604 TO 99999999] pubDate:[0 TO 20030899])", kf.andGroup().with(kf.since("pubDate", "2003-06-04")).with(kf.till("pubDate", "2003-08-99")).toString());
+
+        /*
+        assertEquals("+pubDate:[0 TO 20030604]", kf.till("2003-06-04")
+                .toString());
+        */
+        assertEquals("pubDate:[0 TO 20030604]", kf.till("pubDate", "2003-06-04")
+                .toString());
+
+
+        /*
+        assertEquals("+pubDate:[20030604 TO 99999999]", kf.since("2003-06-04")
+                .toString());
+        */
+        assertEquals("pubDate:[20030604 TO 99999999]", kf.since("pubDate", "2003-06-04")
+                .toString());
+
+        /*
+        assertEquals("+pubDate:20030604", kf.date("2003-06-04").toString());
+        */
+        assertEquals("pubDate:[20030604 TO 20030604]", kf.date("pubDate", "2003-06-04").toString());
+    };
+
+
+    @Test
+    public void LegacyRangeLimited () throws IOException {
+        CollectionBuilder kf = new CollectionBuilder();
+        /*
+        assertEquals("+pubDate:[20050000 TO 20099999]",
+                kf.between("2005", "2009").toString());
+        */
+        assertEquals("AndGroup(pubDate:[20050000 TO 99999999] pubDate:[0 TO 20099999])",
+                     kf.between("pubDate", "2005", "2009").toString());
+
+        /*
+        assertEquals("+pubDate:[20051000 TO 20090899]",
+                kf.between("200510", "200908").toString());
+        */
+        assertEquals("AndGroup(pubDate:[20051000 TO 99999999] pubDate:[0 TO 20090899])",
+                     kf.between("pubDate", "200510", "200908").toString());
+
+        /*
+        assertEquals("+pubDate:[20051000 TO 20090899]",
+                kf.between("2005-10", "2009-08").toString());
+        */
+        assertEquals("AndGroup(pubDate:[20051000 TO 99999999] pubDate:[0 TO 20090899])",
+                     kf.between("pubDate", "2005-10", "2009-08").toString());
+
+
+        /*
+        assertEquals("+pubDate:[20051006 TO 20090803]",
+                kf.between("2005-1006", "2009-0803").toString());
+         */
+        assertEquals("AndGroup(pubDate:[20051006 TO 99999999] pubDate:[0 TO 20090803])",
+                     kf.between("pubDate", "2005-1006", "2009-0803").toString());
+
+        /*
+        assertEquals("+pubDate:[20051006 TO 20090803]",
+                kf.between("2005-10-06", "2009-08-03").toString());
+        */
+        assertEquals("AndGroup(pubDate:[20051006 TO 99999999] pubDate:[0 TO 20090803])",
+                     kf.between("pubDate", "2005-10-06", "2009-08-03").toString());
+
+        /*
+        assertEquals("+pubDate:[0 TO 20059999]", kf.till("2005").toString());
+        */
+        assertEquals("pubDate:[0 TO 20059999]", kf.till("pubDate", "2005").toString());
+
+        /*
+        assertEquals("+pubDate:[0 TO 20051099]", kf.till("200510").toString());
+        */
+        assertEquals("pubDate:[0 TO 20051099]", kf.till("pubDate", "200510").toString());
+
+        /*
+        assertEquals("+pubDate:[0 TO 20051099]", kf.till("200510").toString());
+        */
+        assertEquals("pubDate:[0 TO 20051099]", kf.till("pubDate", "200510").toString());
+
+        /*
+        assertEquals("+pubDate:[0 TO 20051099]", kf.till("2005-10").toString());
+        */
+        assertEquals("pubDate:[0 TO 20051099]", kf.till("pubDate", "2005-10").toString());
+
+        /*
+        assertEquals("+pubDate:[0 TO 20051006]", kf.till("2005-1006")
+                .toString());
+         */
+        assertEquals("pubDate:[0 TO 20051006]", kf.till("pubDate", "2005-1006")
+                .toString());
+
+        /*
+        assertEquals("+pubDate:[0 TO 20051006]", kf.till("2005-10-06")
+                .toString());
+        */
+        assertEquals("pubDate:[0 TO 20051006]", kf.till("pubDate", "2005-10-06")
+                .toString());
+
+        /*
+        assertEquals("+pubDate:[20050000 TO 99999999]", kf.since("2005")
+                .toString());
+        */
+        assertEquals("pubDate:[20050000 TO 99999999]", kf.since("pubDate", "2005")
+                .toString());
+
+        /*
+        assertEquals("+pubDate:[20051000 TO 99999999]", kf.since("200510")
+                .toString());
+        */
+        assertEquals("pubDate:[20051000 TO 99999999]", kf.since("pubDate", "200510")
+                .toString());
+
+
+        /*
+        assertEquals("+pubDate:[20051000 TO 99999999]", kf.since("2005-10")
+                .toString());
+        */
+        assertEquals("pubDate:[20051000 TO 99999999]", kf.since("pubDate", "2005-10")
+                .toString());
+
+        /*
+        assertEquals("+pubDate:[20051006 TO 99999999]", kf.since("2005-1006")
+                .toString());
+        */
+        assertEquals("pubDate:[20051006 TO 99999999]", kf.since("pubDate", "2005-1006")
+                .toString());
+
+        /*
+        assertEquals("+pubDate:[20051006 TO 99999999]", kf.since("2005-10-06")
+                .toString());
+        */
+        assertEquals("pubDate:[20051006 TO 99999999]", kf.since("pubDate", "2005-10-06")
+                .toString());
+
+        /*
+        assertEquals("+pubDate:[20050000 TO 20059999]", kf.date("2005")
+                .toString());
+        */
+        assertEquals("pubDate:[20050000 TO 20059999]", kf.date("pubDate", "2005")
+                .toString());
+
+
+        /*
+        assertEquals("+pubDate:[20051000 TO 20051099]", kf.date("200510")
+                .toString());
+        */
+        assertEquals("pubDate:[20051000 TO 20051099]", kf.date("pubDate", "200510")
+                .toString());
+
+        /*
+        assertEquals("+pubDate:[20051000 TO 20051099]", kf.date("2005-10")
+                .toString());
+        */
+        assertEquals("pubDate:[20051000 TO 20051099]", kf.date("pubDate", "2005-10")
+                .toString());
+
+        /*
+        assertEquals("+pubDate:20051006", kf.date("2005-1006").toString());
+        */
+        assertEquals("pubDate:[20051006 TO 20051006]", kf.date("pubDate", "2005-1006").toString());
+
+        /*
+        assertEquals("+pubDate:20051006", kf.date("2005-10-06").toString());
+        */
+        assertEquals("pubDate:[20051006 TO 20051006]", kf.date("pubDate", "2005-10-06").toString());
+    };
+
+
+    @Test
+    public void LegacyRangeFailure () throws IOException {
+        CollectionBuilder kf = new CollectionBuilder();
+        /*
+        assertEquals("", kf.between("aaaa-bb-cc", "aaaabbcc").toString());
+        assertEquals("", kf.till("aaaa-bb-cc").toString());
+        assertEquals("", kf.since("aaaa-bb-cc").toString());
+        assertEquals("", kf.date("aaaa-bb-cc").toString());
+         */
+        assertNull(kf.between("pubDate", "aaaa-bb-cc", "aaaabbcc"));
+        assertNull(kf.till("pubDate", "aaaa-bb-cc"));
+        assertNull(kf.since("pubDate", "aaaa-bb-cc"));
+        assertNull(kf.date("pubDate", "aaaa-bb-cc"));
+    };
+};
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilderLegacy.java b/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilderLegacy.java
index b8f0019..a52a575 100644
--- a/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilderLegacy.java
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestCollectionBuilderLegacy.java
@@ -19,6 +19,7 @@
 public class TestCollectionBuilderLegacy {
 
     @Test
+    @Ignore
     public void filterExample () throws IOException {
         CollectionBuilderLegacy kf = new CollectionBuilderLegacy();
 
@@ -39,6 +40,7 @@
 
 
     @Test
+    @Ignore
     public void rangeExample () throws IOException {
         CollectionBuilderLegacy kf = new CollectionBuilderLegacy();
         assertEquals("+pubDate:[20030604 TO 20030899]",
@@ -52,6 +54,7 @@
 
 
     @Test
+    @Ignore
     public void rangeLimited () throws IOException {
         CollectionBuilderLegacy kf = new CollectionBuilderLegacy();
         assertEquals("+pubDate:[20050000 TO 20099999]",
@@ -96,6 +99,7 @@
 
 
     @Test
+    @Ignore
     public void rangeFailure () throws IOException {
         CollectionBuilderLegacy kf = new CollectionBuilderLegacy();
         assertEquals("", kf.between("aaaa-bb-cc", "aaaabbcc").toString());
@@ -103,7 +107,4 @@
         assertEquals("", kf.since("aaaa-bb-cc").toString());
         assertEquals("", kf.date("aaaa-bb-cc").toString());
     };
-
-
-    // TODO: More extensive testing!
 };
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 da311c0..a504bf2 100644
--- a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSON.java
@@ -147,6 +147,92 @@
     };
 
 
+    @Test
+    public void metaQuery1Legacy () {
+        String metaQuery = getString(getClass().getResource(
+                "/queries/metaquery.jsonld").getFile());
+        KrillCollection kc = new KrillCollection(metaQuery);
+
+        /*
+        assertEquals("filter with QueryWrapperFilter(+textClass:wissenschaft)",
+                kc.getFilter(0).toString());
+        assertEquals(
+                "filter with QueryWrapperFilter(+(+pubPlace:Erfurt +author:Hesse))",
+                kc.getFilter(1).toString());
+        assertEquals(
+                "extend with QueryWrapperFilter(+(+pubDate:[20110429 TO 20131231] +textClass:freizeit))",
+                kc.getFilter(2).toString());
+        assertEquals(3, kc.getCount());
+        */
+
+        // This will and should fail on optimization
+        assertEquals("OrGroup(AndGroup(textClass:wissenschaft AndGroup(pubPlace:Erfurt author:Hesse)) AndGroup(AndGroup(pubDate:[20110429 TO 99999999] pubDate:[0 TO 20131231]) textClass:freizeit))", kc.toString());
+    };
+
+    @Test
+    public void metaQuery2Legacy () {
+        String metaQuery = getString(getClass().getResource(
+                "/queries/metaquery2.jsonld").getFile());
+        KrillCollection kc = new KrillCollection(metaQuery);
+        /*
+        assertEquals(1, kc.getCount());
+        assertEquals(
+                "filter with QueryWrapperFilter(+(+author:Hesse +pubDate:[0 TO 20131205]))",
+                kc.getFilter(0).toString());
+        */
+        assertEquals("AndGroup(author:Hesse pubDate:[0 TO 20131205])", kc.toString());
+    };
+
+
+    @Test
+    public void metaQuery3Legacy () {
+        String metaQuery = getString(getClass().getResource(
+                "/queries/metaquery4.jsonld").getFile());
+        KrillCollection kc = new KrillCollection(metaQuery);
+        /*
+        assertEquals(1, kc.getCount());
+        assertEquals(
+                     // "filter with QueryWrapperFilter(+pubDate:[20000101 TO 20131231])"
+                     "filter with QueryWrapperFilter(+(+pubDate:[20000101 TO 99999999] +pubDate:[0 TO 20131231]))",
+                kc.getFilter(0).toString());
+        */
+        assertEquals("AndGroup(pubDate:[20000101 TO 99999999] pubDate:[0 TO 20131231])", kc.toString());
+    };
+
+
+    @Test
+    public void metaQuery7Legacy () {
+        String metaQuery = getString(getClass().getResource(
+                "/queries/metaquery7.jsonld").getFile());
+        KrillCollection kc = new KrillCollection(metaQuery);
+        /*
+        assertEquals(2, kc.getCount());
+        assertEquals(
+                "filter with QueryWrapperFilter(+(corpusID:c-1 corpusID:c-2))",
+                kc.getFilter(0).toString());
+        assertEquals(
+                "filter with QueryWrapperFilter(+(+corpusID:d-1 +corpusID:d-2))",
+                kc.getFilter(1).toString());
+        */
+        // TODO: This is subject to optimization and may change in further versions
+        assertEquals("AndGroup(OrGroup(corpusID:c-1 corpusID:c-2) AndGroup(corpusID:d-1 corpusID:d-2))", kc.toString());
+    };
+
+
+    @Test
+    public void metaQuery9 () {
+        String metaQuery = getString(getClass().getResource(
+                "/queries/metaquery9.jsonld").getFile());
+        KrillCollection kc = new KrillCollection(metaQuery);
+        /*
+        assertEquals(1, kc.getCount());
+        assertEquals("filter with QueryWrapperFilter(+corpusID:WPD)", kc
+                .getFilter(0).toString());
+        */
+        assertEquals("corpusID:WPD", kc.toString());
+    };
+
+
     private String _getJSONString (String file) {
         return getString(getClass().getResource(path + file).getFile());
     };
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSONLegacy.java b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSONLegacy.java
index bfa1098..d9a8cff 100644
--- a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSONLegacy.java
+++ b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionJSONLegacy.java
@@ -17,6 +17,7 @@
 public class TestKrillCollectionJSONLegacy {
 
     @Test
+    @Ignore
     public void metaQuery1 () {
         String metaQuery = getString(getClass().getResource(
                 "/queries/metaquery.jsonld").getFile());
@@ -35,6 +36,7 @@
 
 
     @Test
+    @Ignore
     public void metaQuery2 () {
         String metaQuery = getString(getClass().getResource(
                 "/queries/metaquery2.jsonld").getFile());
@@ -47,6 +49,7 @@
 
 
     @Test
+    @Ignore
     public void metaQuery3 () {
         String metaQuery = getString(getClass().getResource(
                 "/queries/metaquery4.jsonld").getFile());
@@ -60,6 +63,7 @@
 
 
     @Test
+    @Ignore
     public void metaQuery7 () {
         String metaQuery = getString(getClass().getResource(
                 "/queries/metaquery7.jsonld").getFile());
@@ -75,6 +79,7 @@
 
 
     @Test
+    @Ignore
     public void metaQuery9 () {
         String metaQuery = getString(getClass().getResource(
                 "/queries/metaquery9.jsonld").getFile());
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionLegacy.java b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionLegacy.java
deleted file mode 100644
index 0a3f51f..0000000
--- a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionLegacy.java
+++ /dev/null
@@ -1,256 +0,0 @@
-package de.ids_mannheim.korap.collection;
-
-import java.io.*;
-
-import de.ids_mannheim.korap.KrillIndex;
-import de.ids_mannheim.korap.index.FieldDocument;
-import de.ids_mannheim.korap.KrillCollectionLegacy;
-import de.ids_mannheim.korap.response.Result;
-import de.ids_mannheim.korap.KrillQuery;
-import de.ids_mannheim.korap.query.QueryBuilder;
-import de.ids_mannheim.korap.collection.BooleanFilter;
-
-import org.apache.lucene.index.Term;
-import org.apache.lucene.search.spans.SpanOrQuery;
-import org.apache.lucene.search.spans.SpanQuery;
-import org.apache.lucene.search.spans.SpanTermQuery;
-
-import static org.junit.Assert.*;
-import org.junit.Test;
-import org.junit.Ignore;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
-@RunWith(JUnit4.class)
-public class TestKrillCollectionLegacy {
-
-    @Test
-    @Ignore
-    public void filterExample () 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();
-
-        CollectionBuilderLegacy kf = new CollectionBuilderLegacy();
-
-        // Create Virtual collections:
-        KrillCollectionLegacy kc = new KrillCollectionLegacy(ki);
-
-        assertEquals("Documents", 7, kc.numberOf("documents"));
-
-        // The virtual collection consists of all documents that have
-        // the textClass "reisen" and "freizeit"
-
-        kc.filter(kf.and("textClass", "reisen").and("textClass",
-                "freizeit-unterhaltung"));
-
-        assertEquals("Documents", 5, kc.numberOf("documents"));
-        assertEquals("Tokens", 1678, kc.numberOf("tokens"));
-        assertEquals("Sentences", 194, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 139, kc.numberOf("paragraphs"));
-
-        // Subset this to all documents that have also the text
-        kc.filter(kf.and("textClass", "kultur"));
-
-        assertEquals("Documents", 1, kc.numberOf("documents"));
-        assertEquals("Tokens", 405, kc.numberOf("tokens"));
-        assertEquals("Sentences", 75, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 48, kc.numberOf("paragraphs"));
-
-        kc.filter(kf.and("corpusID", "WPD"));
-
-        assertEquals("Documents", 1, kc.numberOf("documents"));
-        assertEquals("Tokens", 405, kc.numberOf("tokens"));
-        assertEquals("Sentences", 75, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 48, kc.numberOf("paragraphs"));
-
-        // Create a query
-        QueryBuilder kq = new QueryBuilder("tokens");
-        SpanQuery query = kq.seg("opennlp/p:NN").with("tt/p:NN").toQuery();
-
-        Result kr = kc.search(query);
-        assertEquals(kr.getTotalResults(), 70);
-
-        kc.extend(kf.and("textClass", "uninteresting"));
-        assertEquals("Documents", 1, kc.numberOf("documents"));
-
-        kc.extend(kf.and("textClass", "wissenschaft"));
-
-        assertEquals("Documents", 3, kc.numberOf("documents"));
-        assertEquals("Tokens", 1669, kc.numberOf("tokens"));
-        assertEquals("Sentences", 188, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 130, kc.numberOf("paragraphs"));
-        // System.err.println(kr.toJSON());
-    };
-
-
-    @Test
-    @Ignore
-    public void filterExampleAtomic () throws Exception {
-
-        // That's exactly the same test class, but with multiple atomic indices
-
-        // 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();
-        };
-
-        CollectionBuilderLegacy kf = new CollectionBuilderLegacy();
-
-        // Create Virtual collections:
-        KrillCollectionLegacy kc = new KrillCollectionLegacy(ki);
-
-        assertEquals("Documents", 7, kc.numberOf("documents"));
-
-        // If this is set - everything is fine automatically ...
-        kc.filter(kf.and("corpusID", "WPD"));
-        assertEquals("Documents", 7, kc.numberOf("documents"));
-
-
-        // The virtual collection consists of all documents that have the textClass "reisen" and "freizeit"
-
-        kc.filter(kf.and("textClass", "reisen").and("textClass",
-                "freizeit-unterhaltung"));
-
-        assertEquals("Documents", 5, kc.numberOf("documents"));
-        assertEquals("Tokens", 1678, kc.numberOf("tokens"));
-        assertEquals("Sentences", 194, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 139, kc.numberOf("paragraphs"));
-
-        // Subset this to all documents that have also the text
-        kc.filter(kf.and("textClass", "kultur"));
-
-        assertEquals("Documents", 1, kc.numberOf("documents"));
-        assertEquals("Tokens", 405, kc.numberOf("tokens"));
-        assertEquals("Sentences", 75, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 48, kc.numberOf("paragraphs"));
-
-        // This is already filtered though ...
-        kc.filter(kf.and("corpusID", "WPD"));
-
-        assertEquals("Documents", 1, kc.numberOf("documents"));
-        assertEquals("Tokens", 405, kc.numberOf("tokens"));
-        assertEquals("Sentences", 75, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 48, kc.numberOf("paragraphs"));
-
-        // Create a query
-        QueryBuilder kq = new QueryBuilder("tokens");
-        SpanQuery query = kq.seg("opennlp/p:NN").with("tt/p:NN").toQuery();
-
-        Result kr = kc.search(query);
-        assertEquals(kr.getTotalResults(), 70);
-
-        kc.extend(kf.and("textClass", "uninteresting"));
-        assertEquals("Documents", 1, kc.numberOf("documents"));
-
-        kc.extend(kf.and("textClass", "wissenschaft"));
-
-        assertEquals("Documents", 3, kc.numberOf("documents"));
-        assertEquals("Tokens", 1669, kc.numberOf("tokens"));
-        assertEquals("Sentences", 188, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 130, kc.numberOf("paragraphs"));
-    };
-
-
-
-    @Test
-    @Ignore
-    public void filterExample2 () 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();
-
-        ki.addDoc(getClass()
-                .getResourceAsStream("/wiki/00012-fakemeta.json.gz"), true);
-
-        ki.commit();
-
-        CollectionBuilderLegacy kf = new CollectionBuilderLegacy();
-
-        // Create Virtual collections:
-        KrillCollectionLegacy kc = new KrillCollectionLegacy(ki);
-        kc.filter(kf.and("textClass", "reisen").and("textClass",
-                "freizeit-unterhaltung"));
-        assertEquals("Documents", 5, kc.numberOf("documents"));
-        assertEquals("Tokens", 1678, kc.numberOf("tokens"));
-        assertEquals("Sentences", 194, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 139, kc.numberOf("paragraphs"));
-
-        // Create a query
-        QueryBuilder kq = new QueryBuilder("tokens");
-        SpanQuery query = kq.seg("opennlp/p:NN").with("tt/p:NN").toQuery();
-
-        Result kr = kc.search(query);
-
-        assertEquals(kr.getTotalResults(), 369);
-
-        kc.filter(kf.and("corpusID", "QQQ"));
-
-        assertEquals("Documents", 0, kc.numberOf("documents"));
-        assertEquals("Tokens", 0, kc.numberOf("tokens"));
-        assertEquals("Sentences", 0, kc.numberOf("sentences"));
-        assertEquals("Paragraphs", 0, kc.numberOf("paragraphs"));
-    };
-
-
-    @Test
-    @Ignore
-    public void uidCollection () throws IOException {
-
-        // Construct index
-        KrillIndex ki = new KrillIndex();
-        // Indexing test files
-        int uid = 1;
-        for (String i : new String[] { "00001", "00002", "00003", "00004",
-                "00005", "00006", "02439" }) {
-            FieldDocument fd = ki.addDoc(uid++,
-                    getClass().getResourceAsStream("/wiki/" + i + ".json.gz"),
-                    true);
-        };
-        ki.commit();
-
-        assertEquals("Documents", 7, ki.numberOf("documents"));
-        assertEquals("Paragraphs", 174, ki.numberOf("paragraphs"));
-        assertEquals("Sentences", 281, ki.numberOf("sentences"));
-        assertEquals("Tokens", 2661, ki.numberOf("tokens"));
-
-        SpanQuery sq = new SpanTermQuery(new Term("tokens", "s:der"));
-        Result kr = ki.search(sq, (short) 10);
-        assertEquals(86, kr.getTotalResults());
-
-        // Create Virtual collections:
-        KrillCollectionLegacy kc = new KrillCollectionLegacy();
-        kc.filterUIDs(new String[] { "2", "3", "4" });
-        kc.setIndex(ki);
-        assertEquals("Documents", 3, kc.numberOf("documents"));
-
-        assertEquals("Paragraphs", 46, kc.numberOf("paragraphs"));
-        assertEquals("Sentences", 103, kc.numberOf("sentences"));
-        assertEquals("Tokens", 1229, kc.numberOf("tokens"));
-
-        kr = kc.search(sq);
-        assertEquals((long) 39, kr.getTotalResults());
-    };
-};
diff --git a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionNew.java b/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionNew.java
deleted file mode 100644
index 52dc59d..0000000
--- a/src/test/java/de/ids_mannheim/korap/collection/TestKrillCollectionNew.java
+++ /dev/null
@@ -1,186 +0,0 @@
-package de.ids_mannheim.korap.collection;
-
-import java.util.*;
-import java.io.*;
-
-import de.ids_mannheim.korap.collection.CollectionBuilder;
-
-import static org.junit.Assert.*;
-import org.junit.Test;
-import org.junit.Ignore;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
-
-@RunWith(JUnit4.class)
-public class TestKrillCollectionNew {
-
-    @Test
-    public void builderTerm () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("author:tree",
-                     kc.term("author", "tree").toString());
-    };
-
-    @Test
-    public void builderRegex () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("QueryWrapperFilter(author:/tre*?/)",
-                     kc.re("author", "tre*?").toString());
-    };
-
-    @Test
-    public void builderDateYear () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("pubDate:[20050000 TO 20059999]",
-                     kc.date("pubDate", "2005").toString());
-    };
-
-    @Test
-    public void builderDateMonth () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("pubDate:[20051000 TO 20051099]",
-                     kc.date("pubDate", "2005-10").toString());
-    };
-
-    @Test
-    public void builderDateDay () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("pubDate:[20051011 TO 20051011]",
-                     kc.date("pubDate", "2005-10-11").toString());
-    };
-
-    @Test
-    public void builderDateBorders () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        // CollectionBuilderNew.CollectionBuilderInterface kbi = ;
-        assertNull(kc.date("pubDate", ""));
-
-        assertEquals("pubDate:[20051580 TO 20051580]",
-                     kc.date("pubDate", "2005-15-80").toString());
-
-        assertNull(kc.date("pubDate", "2005-15-8"));
-        assertNull(kc.date("pubDate", "2005-5-18"));
-        assertNull(kc.date("pubDate", "200-05-18"));
-    };
-
-    @Test
-    public void builderSince () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("pubDate:[20050000 TO 99999999]",
-                     kc.since("pubDate", "2005").toString());
-
-        assertEquals("pubDate:[20051000 TO 99999999]",
-                     kc.since("pubDate", "2005-10").toString());
-
-        assertEquals("pubDate:[20051012 TO 99999999]",
-                     kc.since("pubDate", "2005-10-12").toString());
-    };
-
-
-    @Test
-    public void builderTill () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("pubDate:[0 TO 20059999]",
-                     kc.till("pubDate", "2005").toString());
-
-        assertEquals("pubDate:[0 TO 20051299]",
-                     kc.till("pubDate", "2005-12").toString());
-
-        assertEquals("pubDate:[0 TO 20051204]",
-                     kc.till("pubDate", "2005-12-04").toString());
-    };
-
-
-    @Test
-    public void builderAndSimple () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("author:tree", kc.andGroup().with(kc.term("author", "tree")).toString());
-    };
-
-    @Test
-    public void builderOrSimple () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("author:tree", kc.orGroup().with(kc.term("author", "tree")).toString());
-    };
-
-    @Test
-    public void builderAndCombined () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("AndGroup(author:tree title:name)",
-                     kc.andGroup().with(kc.term("author", "tree"))
-                     .with(kc.term("title", "name")).toString());
-    };
-
-    @Test
-    public void builderAndNestedSimple () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("AndGroup(author:tree title:name)",
-                     kc.andGroup().with(kc.andGroup().with(kc.term("author", "tree")).with(kc.term("title", "name"))).toString());
-    };
-
-
-    @Test
-    public void builderOrCombined () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("OrGroup(author:tree title:name)",
-                     kc.orGroup().with(kc.term("author", "tree"))
-                     .with(kc.term("title", "name")).toString());
-    };
-
-    @Test
-    public void builderOrNestedSimple () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        assertEquals("OrGroup(author:tree title:name)",
-                     kc.orGroup().with(kc.orGroup().with(kc.term("author", "tree"))
-                                .with(kc.term("title", "name"))).toString()
-                     );
-    };
-
-    @Test
-    public void builderGroups () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        String g = kc.orGroup().with(
-                                     kc.orGroup().with(kc.term("author", "tree1")).with(kc.term("title", "name1"))
-        ).with(
-               kc.andGroup().with(kc.term("author", "tree2")).with(kc.term("title", "name2"))
-               ).toString();
-        assertEquals("OrGroup(OrGroup(author:tree1 title:name1) AndGroup(author:tree2 title:name2))", g);
-    };
-
-    @Test
-    public void builderNegationRoot () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        CollectionBuilder.CollectionBuilderInterface kbi = kc.orGroup().with(kc.term("author", "tree1")).with(kc.term("title", "name1"));
-        assertEquals(
-                     "OrGroup(author:tree1 title:name1)",
-                     kbi.toString());
-        assertFalse(kbi.isNegative());
-
-        kbi = kc.andGroup().with(
-                                 kc.orGroup().with(kc.term("author", "tree1")).with(kc.term("title", "name1"))
-              ).not();
-        assertEquals("OrGroup(author:tree1 title:name1)", kbi.toString());
-        assertTrue(kbi.isNegative());
-    };
-
-
-    @Test
-    public void builderNegation () throws IOException {
-        CollectionBuilder kc = new CollectionBuilder();
-        CollectionBuilder.CollectionBuilderInterface kbi =
-            kc.term("author", "tree").not();
-        assertEquals("author:tree", kbi.toString());
-        assertTrue(kbi.isNegative());
-
-        kbi = kc.andGroup().with(kc.term("author", "tree").not());
-        assertEquals("author:tree", kbi.toString());
-        assertTrue(kbi.isNegative());
-
-        kbi = kc.orGroup().with(kc.term("author", "tree").not());
-        assertEquals("author:tree", kbi.toString());
-        assertTrue(kbi.isNegative());
-    };
-
-
-};
diff --git a/src/test/resources/queries/metaquery.jsonld b/src/test/resources/queries/metaquery.jsonld
index b0b4c55..7d785c7 100644
--- a/src/test/resources/queries/metaquery.jsonld
+++ b/src/test/resources/queries/metaquery.jsonld
@@ -1,115 +1,180 @@
 {
-    "@context": "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
-    "meta" : {
-      "startPage" : 2,
-      "count" : 5,
-      "context" : {
-	"left" : [ "token", 3 ],
-	"right" : [ "char", 6 ]
-      }
-    },
-   "query":{
-      "@type":"koral:group",
-      "operation":"operation:or",
-      "operands":[
-         {
+  "@context": "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
+  "meta" : {
+    "startPage" : 2,
+    "count" : 5,
+    "context" : {
+      "left" : [ "token", 3 ],
+      "right" : [ "char", 6 ]
+    }
+  },
+  "query":{
+    "@type":"koral:group",
+    "operation":"operation:or",
+    "operands":[
+      {
+        "@type":"koral:token",
+        "wrap":{
+          "@type":"koral:term",
+          "foundry":"mate",
+	  "layer" : "lemma",
+	  "key" : "Vokal",
+          "match":"match:eq"
+        }
+      },
+      {
+        "@type":"koral:group",
+	"operation": "operation:sequence",
+        "operands":[
+          {
             "@type":"koral:token",
             "wrap":{
-               "@type":"koral:term",
-               "foundry":"mate",
-	      "layer" : "lemma",
-	      "key" : "Vokal",
-               "match":"match:eq"
+              "@type":"koral:term",
+              "foundry":"mate",
+	      "layer" : "base",
+	      "key" : "der",
+              "match":"match:eq"
             }
-         },
-         {
-            "@type":"koral:group",
-	   "operation": "operation:sequence",
-            "operands":[
-               {
-                  "@type":"koral:token",
-                  "wrap":{
-                    "@type":"koral:term",
-                    "foundry":"mate",
-		    "layer" : "base",
-		    "key" : "der",
-                    "match":"match:eq"
-                  }
-               },
-               {
-                  "@type":"koral:token",
-                  "wrap":{
-                     "@type":"koral:term",
-                     "foundry":"mate",
-		      "layer" : "pos",
-		      "key" : "ADJA",
-                     "match":"match:eq"
-                  }
-               }
-            ]
-         }
-      ]
-   },
-    "collections": [
-        {
-            "@type": "koral:meta-filter",
-            "@id": "korap-filter#id-1223232",
-            "@value": {
-                "@type": "koral:term",
-                "@field": "koral:field#textClass",
-                "@value": "wissenschaft"
+          },
+          {
+            "@type":"koral:token",
+            "wrap":{
+              "@type":"koral:term",
+              "foundry":"mate",
+	      "layer" : "pos",
+	      "key" : "ADJA",
+              "match":"match:eq"
             }
-        },
-        {
-            "@type": "koral:meta-filter",
-            "@id": "korap-filter#id-34345454",
-            "@value": {
-                "@type": "koral:group",
-                "relation": "and",
-                "operands": [
-                    {
-                        "@type": "koral:term",
-                        "@field": "koral:field#pubPlace",
-                        "@value": "Erfurt"
-                    },
-                    {
-                        "@type": "koral:term",
-                        "@field": "koral:field#author",
-                        "@value": "Hesse"
-                    }
-                ]
-            }
-        },
-        {
-            "@type": "koral:meta-extend",
-            "@value": {
-                "@type": "koral:group",
-                "relation": "and",
-                "operands": [
-                    {
-                        "@type": "koral:group",
-                        "comment": "other values can be 'since','until' in combination with a simple korap:term",
-                        "relation": "between",
-                        "field": "koral:field#pubDate",
-                        "operands": [
-                            {
-                                "@type": "koral:date",
-                                "comment": "either long value or String representation '2013-04-29'",
-                                "@value": "2011-04-29"
-                            },
-                            {
-                                "@type": "koral:date",
-                                "@value": "2013-12-31"
-                            }
-                        ]
-                    },
-                    {
-                        "@type": "koral:term",
-                        "@field": "koral:field#textClass",
-                        "@value": "freizeit"
-                    }
-                ]
-            }
-        }
+          }
+        ]
+      }
     ]
+  },
+  "collection" : {
+    "@type" : "koral:docGroup",
+    "operation": "operation:or",
+    "operands" : [
+      {
+	"@type" : "koral:docGroup",
+	"operation" : "operation:and",
+	"operands" : [
+	  {
+            "@type": "koral:doc",
+            "key": "textClass",
+            "value": "wissenschaft"
+	  },
+	  {
+	    "@type" : "koral:docGroup",
+	    "operation" : "operation:and",
+	    "operands": [
+              {
+		"@type": "koral:doc",
+		"key": "pubPlace",
+		"value": "Erfurt"
+              },
+              {
+		"@type": "koral:doc",
+		"key": "author",
+		"value": "Hesse"
+              }
+	    ]
+	  }
+	]
+      },
+      {
+	"@type" : "koral:docGroup",
+	"operation" : "operation:and",
+        "operands": [
+          {
+            "@type": "koral:docGroup",
+            "comment": "other values can be 'since','until' in combination with a simple korap:term",
+	    "operation" : "operation:and",
+            "operands": [
+              {
+                "@type": "koral:doc",
+		"type": "type:date",
+                "comment": "either long value or String representation '2013-04-29'",
+		"key" : "pubDate",
+		"match": "match:geq",
+                "value": "2011-04-29"
+              },
+              {
+                "@type": "koral:doc",
+		"type": "type:date",
+		"key" : "pubDate",
+		"match": "match:leq",
+                "value": "2013-12-31"
+              }
+            ]
+          },
+          {
+            "@type": "koral:doc",
+            "key": "textClass",
+            "value": "freizeit"
+          }
+        ]
+      }]
+  },
+  "collections": [
+    {
+      "@type": "koral:meta-filter",
+      "@id": "korap-filter#id-1223232",
+      "@value": {
+        "@type": "koral:term",
+        "@field": "koral:field#textClass",
+        "@value": "wissenschaft"
+      }
+    },
+    {
+      "@type": "koral:meta-filter",
+      "@id": "korap-filter#id-34345454",
+      "@value": {
+        "@type": "koral:group",
+        "relation": "and",
+        "operands": [
+          {
+            "@type": "koral:term",
+            "@field": "koral:field#pubPlace",
+            "@value": "Erfurt"
+          },
+          {
+            "@type": "koral:term",
+            "@field": "koral:field#author",
+            "@value": "Hesse"
+          }
+        ]
+      }
+    },
+    {
+      "@type": "koral:meta-extend",
+      "@value": {
+        "@type": "koral:group",
+        "relation": "and",
+        "operands": [
+          {
+            "@type": "koral:group",
+            "comment": "other values can be 'since','until' in combination with a simple korap:term",
+            "relation": "between",
+            "field": "koral:field#pubDate",
+            "operands": [
+              {
+                "@type": "koral:date",
+                "comment": "either long value or String representation '2013-04-29'",
+                "@value": "2011-04-29"
+              },
+              {
+                "@type": "koral:date",
+                "@value": "2013-12-31"
+              }
+            ]
+          },
+          {
+            "@type": "koral:term",
+            "@field": "koral:field#textClass",
+            "@value": "freizeit"
+          }
+        ]
+      }
+    }
+  ]
 }
diff --git a/src/test/resources/queries/metaquery2.jsonld b/src/test/resources/queries/metaquery2.jsonld
index f7c906a..2586b06 100644
--- a/src/test/resources/queries/metaquery2.jsonld
+++ b/src/test/resources/queries/metaquery2.jsonld
@@ -76,5 +76,23 @@
         ]
       }
     }
-  ]
+  ],
+  "collection" : {
+    "@type" : "koral:docGroup",
+    "operation": "operation:and",
+    "operands" : [
+      {
+	"@type" : "koral:doc",
+	"key" : "author",
+	"value" : "Hesse"
+      },
+      {
+	"@type" : "koral:doc",
+	"key" : "pubDate",
+	"type" : "type:date",
+	"match" : "match:leq",
+	"value" : "2013-12-05"
+      }
+    ]
+  }
 }
diff --git a/src/test/resources/queries/metaquery3.jsonld b/src/test/resources/queries/metaquery3.jsonld
index a9fca44..5fbd487 100644
--- a/src/test/resources/queries/metaquery3.jsonld
+++ b/src/test/resources/queries/metaquery3.jsonld
@@ -1,5 +1,5 @@
 {
-    "@context": "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
+  "@context": "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
   "meta":{
     "startPage" : 2,
     "count" : 5,
@@ -7,47 +7,47 @@
       "left" : [ "token", 3 ],
       "right" : [ "char", 6 ]
     }
-},
-   "query":{
-      "@type":"koral:group",
-      "operation":"operation:or",
-      "operands":[
-         {
+  },
+  "query":{
+    "@type":"koral:group",
+    "operation":"operation:or",
+    "operands":[
+      {
+        "@type":"koral:token",
+        "wrap":{
+          "@type":"koral:term",
+	  "foundry" : "mate",
+          "layer":"l",
+	  "key":"Vokal",
+          "match":"match:eq"
+        }
+      },
+      {
+        "@type":"koral:group",
+	"operation" : "operation:sequence",
+        "operands":[
+          {
             "@type":"koral:token",
             "wrap":{
-               "@type":"koral:term",
-	        "foundry" : "mate",
-               "layer":"l",
-	       "key":"Vokal",
-               "match":"match:eq"
+              "@type":"koral:term",
+	      "foundry" : "mate",
+              "layer":"l",
+	      "key" : "der",
+              "match":"match:eq"
             }
-         },
-         {
-            "@type":"koral:group",
-	   "operation" : "operation:sequence",
-            "operands":[
-               {
-                  "@type":"koral:token",
-                  "wrap":{
-                     "@type":"koral:term",
-		     "foundry" : "mate",
-                     "layer":"l",
-		     "key" : "der",
-                     "match":"match:eq"
-                  }
-               },
-               {
-                  "@type":"koral:token",
-                  "wrap":{
-                     "@type":"koral:term",
-                     "foundry":"mate",
-		      "layer" : "p",
-		      "key" : "ADJA",
-                     "match":"match:eq"
-                  }
-               }
-            ]
-         }
-      ]
-   }
+          },
+          {
+            "@type":"koral:token",
+            "wrap":{
+              "@type":"koral:term",
+              "foundry":"mate",
+	      "layer" : "p",
+	      "key" : "ADJA",
+              "match":"match:eq"
+            }
+          }
+        ]
+      }
+    ]
+  }
 }
diff --git a/src/test/resources/queries/metaquery7.jsonld b/src/test/resources/queries/metaquery7.jsonld
index 0b664ed..0c6ce9e 100644
--- a/src/test/resources/queries/metaquery7.jsonld
+++ b/src/test/resources/queries/metaquery7.jsonld
@@ -1,60 +1,98 @@
 {
-    "@context": "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
-    "meta":{
-	"startPage" : 1,
-	"count" : 5,
-	"context" : {
-	    "left" : [ "token", 3 ],
-	    "right" : [ "char", 6 ]
-	}
+  "@context": "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
+  "meta":{
+    "startPage" : 1,
+    "count" : 5,
+    "context" : {
+      "left" : [ "token", 3 ],
+      "right" : [ "char", 6 ]
+    }
+  },
+  "query":{
+    "@type":"koral:token",
+    "wrap":{
+      "@type":"koral:term",
+      "foundry":"mate",
+      "layer":"l",
+      "key":"lediglich"
+    }
+  },
+  "collections": [
+    {
+      "@type": "koral:meta-filter",
+      "@id": "korap-filter#id-1223232",
+      "@value": {
+	"@type": "koral:group",
+	"relation": "or",
+	"@field": "koral:field#corpusID",
+	"operands": [
+	  {
+	    "@type": "koral:term",
+	    "@value": "c-1"
+	  },
+	  {
+	    "@type": "koral:term",
+	    "@value": "c-2"
+	  }
+	]
+      }
     },
-    "query":{
-	"@type":"koral:token",
-	"wrap":{
-	    "@type":"koral:term",
-	    "foundry":"mate",
-	    "layer":"l",
-	    "key":"lediglich"
-	}
-    },
-    "collections": [
-	{
-	    "@type": "koral:meta-filter",
-	    "@id": "korap-filter#id-1223232",
-	    "@value": {
-		"@type": "koral:group",
-		"relation": "or",
-		"@field": "koral:field#corpusID",
-		"operands": [
-		    {
-			"@type": "koral:term",
-			"@value": "c-1"
-		    },
-		    {
-			"@type": "koral:term",
-			"@value": "c-2"
-		    }
-		]
-	    }
-	},
-	{
-	    "@type": "koral:meta-filter",
-	    "@id": "korap-filter#id-1223232",
-	    "@value": {
-		"@type": "koral:group",
-		"relation": "and",
-		"@field": "koral:field#corpusID",
-		"operands": [
-		    {
-			"@type": "koral:term",
-			"@value": "d-1"
-		    },
-		    {
-			"@type": "koral:term",
-			"@value": "d-2"
-		    }
-		]
-	    }
-	}
+    {
+      "@type": "koral:meta-filter",
+      "@id": "korap-filter#id-1223232",
+      "@value": {
+	"@type": "koral:group",
+	"relation": "and",
+	"@field": "koral:field#corpusID",
+	"operands": [
+	  {
+	    "@type": "koral:term",
+	    "@value": "d-1"
+	  },
+	  {
+	    "@type": "koral:term",
+	    "@value": "d-2"
+	  }
+	]
+      }
+    }
+  ],
+  "collection" : {
+    "@type" : "koral:docGroup",
+    "operation" : "operation:and",
+    "operands" : [
+      {
+	"@type" : "koral:docGroup",
+	"operation" : "operation:or",
+	"operands" : [
+	  {
+	    "@type" : "koral:doc",
+	    "key" : "corpusID",
+	    "value" : "c-1"
+	  },
+	  {
+	    "@type" : "koral:doc",
+	    "key" : "corpusID",
+	    "value" : "c-2"
+	  }
+	]
+      },
+      {
+        "@type" : "koral:docGroup",
+	"operation" : "operation:and",
+	"operands" : [
+	  {
+	    "@type" : "koral:doc",
+	    "key" : "corpusID",
+	    "value" : "d-1"
+	  },
+	  {
+	    "@type" : "koral:doc",
+	    "key" : "corpusID",
+	    "value" : "d-2"
+	  }
+	]
+      }
     ]
+  }
 }
diff --git a/src/test/resources/queries/metaquery9.jsonld b/src/test/resources/queries/metaquery9.jsonld
index 5a812ec..a89b584 100644
--- a/src/test/resources/queries/metaquery9.jsonld
+++ b/src/test/resources/queries/metaquery9.jsonld
@@ -1,12 +1,17 @@
 {
-    "collections":[
-	{
-	    "@type":"koral:meta-filter",
-	    "@value":{
-		"@type":"koral:term",
-		"@field":"koral:field#corpusID",
-		"@value":"WPD"
-	    }
-	}
-    ]
+  "collections":[
+    {
+      "@type":"koral:meta-filter",
+      "@value":{
+	"@type":"koral:term",
+	"@field":"koral:field#corpusID",
+	"@value":"WPD"
+      }
+    }
+  ],
+  "collection" : {
+    "@type" : "koral:doc",
+    "key" : "corpusID",
+    "value" : "WPD"
+  }
 }