Merge branch 'master' of ssh://korap.ids-mannheim.de:29418/private/Kustvakt-core
diff --git a/src/main/java/de/ids_mannheim/korap/web/SearchLucene.java b/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
similarity index 83%
rename from src/main/java/de/ids_mannheim/korap/web/SearchLucene.java
rename to src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
index be19540..d04193e 100644
--- a/src/main/java/de/ids_mannheim/korap/web/SearchLucene.java
+++ b/src/main/java/de/ids_mannheim/korap/web/SearchKrill.java
@@ -18,15 +18,14 @@
 import java.util.List;
 
 /**
- * The SearchLucene class allows for searching in the Lucene backend
- * by JSON-LD serialized queries.
- * It supports span queries, virtual collections, and paging.
+ * The SearchKrill class allows for searching in the
+ * Lucene based Krill backend by applying KoralQuery.
  *
  * @author Nils Diewald
  */
-public class SearchLucene {
+public class SearchKrill {
     private final static Logger qlog = KorAPLogger.initiate("queryLogger");
-    private final static Logger log = KorAPLogger.initiate(SearchLucene.class);
+    private final static Logger log = KorAPLogger.initiate(SearchKrill.class);
     // Temporary
     String indexDir = "/data/prep_corpus/index/";
     String i = "/Users/hanl/Projects/prep_corpus";
@@ -38,7 +37,7 @@
      * Constructor
      */
     // todo: use korap.config to get index location
-    public SearchLucene(String path) {
+    public SearchKrill(String path) {
         try {
             File f = new File(path);
             log.info("Loading index from " + path);
@@ -47,7 +46,7 @@
                 System.exit(-1);
             }
             this.index = new KrillIndex(new MMapDirectory(new File(path)));
-        }catch (IOException e) {
+        } catch (IOException e) {
             KorAPLogger.ERROR_LOGGER
                     .error("Unable to load index: {}", e.getMessage());
         }
@@ -61,31 +60,28 @@
     public String search(String json) {
         qlog.trace(json);
         if (this.index != null)
-            return new Krill(json).setIndex(this.index).toJsonString();
+            return new Krill(json).apply(this.index).toJsonString();
 
         Result kr = new Result();
-        //        kr.setError("Index not found");
+        kr.addError(601, "Unable to find index");
         return kr.toJsonString();
-    }
-
-    ;
+    };
 
     /**
      * Search in the Lucene index and return matches as token lists.
      *
      * @param json JSON-LD string with search and potential meta filters.
      */
+    @Deprecated
     public String searchTokenList(String json) {
         qlog.trace(json);
         if (this.index != null)
-            return new Krill(json).setIndex(this.index).toJsonString();
+            return new Krill(json).apply(this.index).toTokenListJsonString();
 
         Result kr = new Result();
-        //        kr.setError("Index not found");
+        kr.addError(601, "Unable to find index");
         return kr.toJsonString();
-    }
-
-    ;
+    };
 
     /**
      * Get info on a match - by means of a richly annotated html snippet.
@@ -97,17 +93,17 @@
         if (this.index != null) {
             try {
                 return this.index.getMatch(id).toJsonString();
-            }catch (QueryException qe) {
+            } catch (QueryException qe) {
                 Match km = new Match();
-                km.setError(qe.getMessage());
+                km.addError(qe.getErrorCode(), qe.getMessage());
                 return km.toJsonString();
             }
-        }
+        };
 
         Match km = new Match();
-        km.setError("Index not found");
+        km.addError(601, "Unable to find index");
         return km.toJsonString();
-    }
+    };
 
     public String getMatch(String id, List<String> foundries,
             List<String> layers, boolean includeSpans,
@@ -119,18 +115,18 @@
                         .getMatchInfo(id, "tokens", true, foundries, layers,
                                 includeSpans, includeHighlights,
                                 sentenceExpansion).toJsonString();
-            }catch (QueryException qe) {
+            } catch (QueryException qe) {
                 Match km = new Match();
-                km.setError(qe.getMessage());
+                km.addError(qe.getErrorCode(), qe.getMessage());
                 return km.toJsonString();
             }
-        }
+        };
 
         Match km = new Match();
-        km.setError("Index not found");
+        km.addError(601, "Unable to find index");
         return km.toJsonString();
+    };
 
-    }
 
     /**
      * Get info on a match - by means of a richly annotated html snippet.
@@ -141,7 +137,7 @@
      * @param includeSpans      Should spans be included (or only token infos)?
      * @param includeHighlights Should highlight markup be included?
      */
-    public String getMatch(String id, String foundry, String layer,
+    public String getMatch (String id, String foundry, String layer,
             boolean includeSpans, boolean includeHighlights,
             boolean sentenceExpansion) {
 
@@ -163,24 +159,25 @@
                 return this.index.getMatchInfo(id, "tokens", foundry, layer,
                         includeSpans, includeHighlights, sentenceExpansion)
                         .toJsonString();
-            }catch (QueryException qe) {
+            } catch (QueryException qe) {
                 Match km = new Match();
-                km.setError(qe.getMessage());
+                km.addError(qe.getErrorCode(), qe.getMessage());
                 return km.toJsonString();
             }
-        }
+        };
 
         Match km = new Match();
-        km.setError("Index not found");
+        km.addError(601, "Unable to find index");
         return km.toJsonString();
-    }
+    };
 
     /**
      * Get statistics on (virtual) collections.
      *
      * @param json JSON-LD string with potential meta filters.
      */
-    public String getStatisticsLegacy(JsonNode json) throws QueryException {
+    @Deprecated
+    public String getStatisticsLegacy (JsonNode json) throws QueryException {
         qlog.trace(JsonUtils.toJSON(json));
         System.out.println("THE NODE BEFORE GETTING STATISTICS " + json);
 
@@ -196,9 +193,9 @@
         kc.setIndex(this.index);
 
         long docs = 0,
-                tokens = 0,
-                sentences = 0,
-                paragraphs = 0;
+            tokens = 0,
+            sentences = 0,
+            paragraphs = 0;
 
         // Get numbers from index (currently slow)
         try {
@@ -206,7 +203,7 @@
             tokens = kc.numberOf("tokens");
             sentences = kc.numberOf("sentences");
             paragraphs = kc.numberOf("paragraphs");
-        }catch (IOException e) {
+        } catch (IOException e) {
             e.printStackTrace();
         }
 
@@ -228,7 +225,8 @@
      *
      * @param json JSON-LD string with potential meta filters.
      */
-    public String getStatistics(String json) {
+    @Deprecated
+    public String getStatistics (String json) {
         qlog.trace(json);
 
         if (this.index == null) {
@@ -252,7 +250,7 @@
             tokens = kc.numberOf("tokens");
             sentences = kc.numberOf("sentences");
             paragraphs = kc.numberOf("paragraphs");
-        }catch (IOException e) {
+        } catch (IOException e) {
             e.printStackTrace();
         }
 
@@ -274,7 +272,8 @@
      *
      * @param json JSON-LD string with potential meta filters.
      */
-    public String getTermRelation(String json, String field) {
+    @Deprecated
+    public String getTermRelation (String json, String field) {
         qlog.trace(json);
 
         if (this.index == null) {
@@ -289,14 +288,14 @@
         long v = 0L;
         try {
             v = kc.numberOf("documents");
-        }catch (IOException e) {
+        } catch (IOException e) {
             e.printStackTrace();
         }
 
         try {
             // Get term relations as a json string
             return kc.getTermRelationJSON(field);
-        }catch (IOException e) {
+        } catch (IOException e) {
             KorAPLogger.ERROR_LOGGER
                     .error("Unable to retrieve term relations: {}",
                             e.getMessage());
@@ -304,7 +303,7 @@
         }
     }
 
-    public String getMatchId(String type, String docid, String tofrom) {
+    public String getMatchId (String type, String docid, String tofrom) {
         return new StringBuilder().append("contains-").append(type).append("!")
                 .append(type).append("_").append(docid).append("-")
                 .append(tofrom).toString();
diff --git a/src/main/java/de/ids_mannheim/korap/web/service/LightService.java b/src/main/java/de/ids_mannheim/korap/web/service/LightService.java
index 35e1c6b..99b4372 100644
--- a/src/main/java/de/ids_mannheim/korap/web/service/LightService.java
+++ b/src/main/java/de/ids_mannheim/korap/web/service/LightService.java
@@ -12,7 +12,7 @@
 import de.ids_mannheim.korap.utils.KorAPLogger;
 import de.ids_mannheim.korap.utils.StringUtils;
 import de.ids_mannheim.korap.web.ClientsHandler;
-import de.ids_mannheim.korap.web.SearchLucene;
+import de.ids_mannheim.korap.web.SearchKrill;
 import de.ids_mannheim.korap.web.TRACE;
 import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler;
 import org.slf4j.Logger;
@@ -34,12 +34,12 @@
 
     private static Logger jlog = KorAPLogger.initiate(LightService.class);
 
-    private SearchLucene searchLucene;
+    private SearchKrill searchKrill;
     private ClientsHandler graphDBhandler;
     private RewriteProcessor processor;
 
     public LightService() {
-        this.searchLucene = new SearchLucene(
+        this.searchKrill = new SearchKrill(
                 BeanConfiguration.getConfiguration().getIndexDir());
         UriBuilder builder = UriBuilder.fromUri("http://10.0.10.13").port(9997);
         this.graphDBhandler = new ClientsHandler(builder.build());
@@ -101,8 +101,8 @@
         // todo: should be possible to add the meta part to the query serialization
         jlog.info("Serialized search: {}", jsonld);
 
-        // fixme: to use the systemarchitecture pointcut thingis, searchlucene must be injected via
-        String result = searchLucene.search(jsonld);
+        // fixme: to use the systemarchitecture pointcut thingis, searchkrill must be injected via
+        String result = searchKrill.search(jsonld);
         KorAPLogger.QUERY_LOGGER.trace("The result set: {}", result);
         return Response.ok(result).build();
     }
@@ -128,9 +128,13 @@
         //        meta.addEntry("itemsPerResource", 1);
         serializer.addMeta(meta);
 
+        if (cq != null)
+            serializer.setCollection(cq);
+
         String query = processor.process(serializer.toJSON());
         jlog.info("the serialized query {}", query);
 
+        // This may not work with the the KoralQuery
         if (eng.equals(KustvaktConfiguration.BACKENDS.NEO4J)) {
             MultivaluedMap map = new MultivaluedMapImpl();
             map.add("q", query);
@@ -145,7 +149,7 @@
                 throw KustvaktResponseHandler.throwit(e);
             }
         }else
-            result = searchLucene.search(query);
+            result = searchKrill.search(query);
         KorAPLogger.QUERY_LOGGER.trace("The result set: {}", result);
         return Response.ok(result).build();
     }
@@ -212,7 +216,7 @@
                         String.valueOf(meta.getSpanContext().getRight_size()));
                 result = this.graphDBhandler.getResponse(map, "distKwic");
             }else
-                result = searchLucene.search(query);
+                result = searchKrill.search(query);
 
         }catch (Exception e) {
             KorAPLogger.ERROR_LOGGER
@@ -231,7 +235,7 @@
         CollectionQueryBuilder builder = new CollectionQueryBuilder();
         builder.addResource(json);
 
-        String stats = searchLucene.getStatistics(builder.toCollections());
+        String stats = searchKrill.getStatistics(builder.toCollections());
         if (stats.contains("-1"))
             throw KustvaktResponseHandler.throwit(StatusCodes.EMPTY_RESULTS);
 
@@ -247,7 +251,7 @@
             @QueryParam("layer") Set<String> layers,
             @QueryParam("spans") Boolean spans) {
         spans = spans != null ? spans : false;
-        String matchid = searchLucene.getMatchId(id, docid, rest);
+        String matchid = searchKrill.getMatchId(id, docid, rest);
 
         if (layers == null || layers.isEmpty())
             layers = new HashSet<>();
@@ -269,18 +273,18 @@
                     f_list.add(sep[0]);
                     l_list.add(sep[1]);
                 }
-                results = searchLucene
+                results = searchKrill
                         .getMatch(matchid, new ArrayList<>(f_list),
                                 new ArrayList<>(l_list), spans, false, true);
             }
         }
         try {
             if (!match_only)
-                results = searchLucene
+                results = searchKrill
                         .getMatch(matchid, new ArrayList<>(foundries),
                                 new ArrayList<>(layers), spans, false, true);
             else
-                results = searchLucene.getMatch(matchid);
+                results = searchKrill.getMatch(matchid);
         }catch (Exception e) {
             KorAPLogger.ERROR_LOGGER.error("Exception encountered!", e);
             throw KustvaktResponseHandler