Fixed query deserialization for sequences with multiple non-anchors
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
index d46c9e2..766d6ee 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
@@ -224,6 +224,7 @@
 	assertEquals("spanExpansion(spanExpansion(tokens:tt/p:NN, !tokens:tt/p:DET{1, 1}, right), !tokens:tt/p:ADJ{1, 1}, right)", sqwi.toQuery().toString());
     };
 
+
     @Test
     public void queryJSONseqNegativeEndSequence2 () throws QueryException {
 	SpanQueryWrapper sqwi = jsonQueryFile("negative-last-sequence-2.jsonld");
@@ -233,6 +234,13 @@
 	assertEquals("spanExpansion(spanExpansion(tokens:tt/p:ADJ, !tokens:tt/p:DET{1, 1}, left), !tokens:tt/p:NN{1, 1}, left)", sqwi.toQuery().toString());
     };
 
+    @Test
+    public void queryJSONseqMultipleDistances () throws QueryException {
+	SpanQueryWrapper sqwi = jsonQueryFile("multiple-distances.jsonld");
+	// er []{,10} kann []{1,10} sagte 
+
+	assertEquals("spanDistance(tokens:s:er, spanDistance(tokens:s:kann, tokens:s:sagte, [(w[2:11], ordered, notExcluded)]), [(w[1:11], ordered, notExcluded)])", sqwi.toQuery().toString());
+    };
 
 
     // get query wrapper based on json file
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java b/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
index 36632e6..8c407ad 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
@@ -747,6 +747,48 @@
 	assertEquals(345, res.at("/matches/0/tokens/2/1").asInt());
     };
 
+    @Test
+    public void searchJSONmultitermRewriteBug () throws IOException {
+	// Construct index
+	KorapIndex ki = new KorapIndex();
+	// Indexing test files
+	ki.addDocFile(
+            1,getClass().getResource("/bzk/D59-00089.json.gz").getFile(), true
+	);
+	ki.addDocFile(
+            2,getClass().getResource("/bzk/D59-00089.json.gz").getFile(), true
+	);
+
+	ki.commit();
+
+	String json = getString(
+	    getClass().getResource("/queries/bugs/multiterm_rewrite.jsonld").getFile()
+        );
+	
+	KorapSearch ks = new KorapSearch(json);
+	KorapResult kr = ks.run(ki);
+	assertEquals(kr.getQuery(),"");
+
+
+	/*
+
+	assertEquals(
+            kr.getQuery(),
+	    "{4: spanNext({1: spanNext({2: tokens:s:ins}, {3: tokens:s:Leben})}, tokens:s:gerufen)}"
+        );
+	assertEquals(
+	    kr.getMatch(0).getSnippetBrackets(),
+	    "... sozialistischen Initiative\" eine neue politische Gruppierung " +
+	    "[{4:{1:{2:ins} {3:Leben}} gerufen}] hatten. " +
+	    "Pressemeldungen zufolge haben sich in ..."
+        );
+
+	assertEquals(2, kr.getTotalResults());
+	assertEquals(0, kr.getStartIndex());
+	*/
+    };
+
+
 
     @Test
     public void searchJSONCollection () throws IOException {
diff --git a/src/test/resources/queries/bsp-class.jsonld b/src/test/resources/queries/bsp-class.jsonld
index 075f927..b4fd65e 100644
--- a/src/test/resources/queries/bsp-class.jsonld
+++ b/src/test/resources/queries/bsp-class.jsonld
@@ -3,7 +3,7 @@
   "query":{
     "@type":"korap:group",
     "operation" : "operation:class",
-   "class":0,
+   "class":1,
     "operands":[
       {
 	"@type":"korap:group",
diff --git a/src/test/resources/queries/bugs/cosmas_classrefcheck.jsonld b/src/test/resources/queries/bugs/cosmas_classrefcheck.jsonld
new file mode 100644
index 0000000..bd0c08b
--- /dev/null
+++ b/src/test/resources/queries/bugs/cosmas_classrefcheck.jsonld
@@ -0,0 +1,65 @@
+{
+  "@context": "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
+  "warnings":[
+    "This is a warning coming from the serialization"
+  ],
+  "query": {
+    "@type" : "korap:reference",
+    "classRef" : [
+      130
+    ],
+    "operands" : [
+      {
+        "@type" : "korap:group",
+        "class" : 131,
+        "classIn" : [
+          129,
+          130
+        ],
+        "classOut" : 131,
+        "classRefCheck" : "classRefCheck:includes",
+        "operands" : [
+          {
+            "@type" : "korap:group",
+            "frame" : "frame:contains",
+            "frames" : [],
+            "operands" : [
+              {
+                "@type" : "korap:group",
+                "class" : 129,
+                "classOut" : 129,
+                "operands" : [
+                  {
+                    "@type" : "korap:span",
+                    "key" : "s"
+                  }
+                ],
+                "operation" : "operation:class"
+              },
+              {
+                "@type" : "korap:group",
+                "class" : 130,
+                "classOut" : 130,
+                "operands" : [
+                  {
+                    "@type" : "korap:token",
+                    "wrap" : {
+                      "@type" : "korap:term",
+                      "key" : "wegen",
+                      "layer" : "orth",
+                      "match" : "match:eq"
+                    }
+                  }
+                ],
+                "operation" : "operation:class"
+              }
+            ],
+            "operation" : "operation:position"
+          }
+        ],
+        "operation" : "operation:class"
+      }
+    ],
+    "operation" : "operation:focus"
+  }
+}
diff --git a/src/test/resources/queries/bugs/multiterm_rewrite.jsonld b/src/test/resources/queries/bugs/multiterm_rewrite.jsonld
new file mode 100644
index 0000000..c07f88c
--- /dev/null
+++ b/src/test/resources/queries/bugs/multiterm_rewrite.jsonld
@@ -0,0 +1,64 @@
+{
+  "@context":"http://ids-mannheim.de/ns/KorAP/json-ld/v0.2/context.jsonld",
+  "errors":[],
+  "warnings":[],
+  "announcements":[
+    "Deprecated 2014-07-24: 'min' and 'max' to be supported until 3 months from deprecation date."
+  ],
+  "collection":{},
+  "meta":{
+    "startPage":1,
+    "context":"paragraph"
+  },
+  "query":{
+    "@type":"korap:group",
+    "operation":"operation:sequence",
+    "operands":[
+      {
+	"@type":"korap:group",
+	"operation":"operation:repetition",
+	"operands":[
+	  {
+	    "@type":"korap:token",
+	    "wrap":{
+	      "@type":"korap:term",
+	      "foundry":"tt",
+	      "layer":"p",
+	      "type":"type:regex",
+	      "key":"A.*",
+	      "match":"match:eq"
+	    }
+	  }
+	],
+	"boundary":{
+	  "@type":"korap:boundary",
+	  "min":0,
+	  "max":3
+	},
+	"min":0,
+	"max":3
+      },
+      {
+	"@type":"korap:token",
+	"wrap":{
+	  "@type":"korap:term",
+	  "foundry":"tt",
+	  "layer":"p",
+	  "type":"type:regex",
+	  "key":"N.*",
+	  "match":"match:eq"
+	}
+      }
+    ]
+  },
+  "collections":[
+    {
+      "@type":"korap:meta-filter",
+      "@value":{
+	"@type":"korap:term",
+	"@field":"korap:field#corpusID",
+	"@value":"WPD"
+      }
+    }
+  ]
+}
diff --git a/src/test/resources/queries/sequence/multiple-distances.jsonld b/src/test/resources/queries/sequence/multiple-distances.jsonld
new file mode 100644
index 0000000..4187ded
--- /dev/null
+++ b/src/test/resources/queries/sequence/multiple-distances.jsonld
@@ -0,0 +1,70 @@
+{
+  "@context": "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
+  "query": {
+    "@type" : "korap:group",
+    "distances" : [
+      {
+        "@type" : "korap:distance",
+        "boundary" : {
+          "@type" : "korap:boundary",
+          "max" : 11,
+          "min" : 1
+        },
+        "key" : "w",
+        "max" : 11,
+        "min" : 1
+      }
+    ],
+    "inOrder" : true,
+    "operands" : [
+      {
+        "@type" : "korap:token",
+        "wrap" : {
+          "@type" : "korap:term",
+          "key" : "er",
+          "layer" : "orth",
+          "match" : "match:eq"
+        }
+      },
+      {
+        "@type" : "korap:group",
+        "distances" : [
+          {
+            "@type" : "korap:distance",
+            "boundary" : {
+              "@type" : "korap:boundary",
+              "max" : 11,
+              "min" : 2
+            },
+            "key" : "w",
+            "max" : 11,
+            "min" : 2
+          }
+        ],
+        "inOrder" : true,
+        "operands" : [
+          {
+            "@type" : "korap:token",
+            "wrap" : {
+              "@type" : "korap:term",
+              "key" : "kann",
+              "layer" : "orth",
+              "match" : "match:eq"
+            }
+          },
+          {
+            "@type" : "korap:token",
+            "wrap" : {
+              "@type" : "korap:term",
+              "key" : "sagte",
+              "layer" : "orth",
+              "match" : "match:eq"
+            }
+          }
+        ],
+        "operation" : "operation:sequence"
+      }
+    ],
+    "operation" : "operation:sequence"
+  }
+}