Added query-param show-snippet to the search web-service (solves #322)

Change-Id: Id0719fc2dd1166ae083418e27e6b15c0aaa4a289
diff --git a/core/Changes b/core/Changes
index 71d464d..1189922 100644
--- a/core/Changes
+++ b/core/Changes
@@ -3,6 +3,7 @@
 - Added data folder containing super_client_info and database 
   for mounting into docker
 - Removed the API authentication scheme from www-authenticate header  
+- Added query-param show-snippet to the search web-service (solves #322)
   
 # version 0.69.4
 
diff --git a/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java b/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
index f1b70ef..526d1e5 100644
--- a/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
+++ b/core/src/main/java/de/ids_mannheim/korap/service/SearchService.java
@@ -136,7 +136,7 @@
             String q, String ql, String v, List<String> cqList, String fields,
             String pipes, Integer pageIndex, Integer pageInteger, String ctx,
             Integer pageLength, Boolean cutoff, boolean accessRewriteDisabled,
-            boolean showTokens)
+            boolean showTokens, boolean showSnippet)
             throws KustvaktException {
 
         if (pageInteger != null && pageInteger < 1) {
@@ -169,7 +169,7 @@
         
         MetaQueryBuilder meta = createMetaQuery(pageIndex, pageInteger, ctx,
                 pageLength, cutoff, corpusAccess, fieldList, accessRewriteDisabled,
-                showTokens);
+                showTokens, showSnippet);
         serializer.setMeta(meta.raw());
         
         // There is an error in query processing
@@ -322,7 +322,7 @@
             Integer pageInteger, String ctx, Integer pageLength,
             Boolean cutoff, CorpusAccess corpusAccess, List<String> fieldList,
             boolean accessRewriteDisabled,
-            boolean showTokens) {
+            boolean showTokens, boolean showSnippet) {
         MetaQueryBuilder meta = new MetaQueryBuilder();
         meta.addEntry("startIndex", pageIndex);
         meta.addEntry("startPage", pageInteger);
@@ -330,7 +330,7 @@
         meta.addEntry("count", pageLength);
         // todo: what happened to cutoff?
         meta.addEntry("cutOff", cutoff);
-        meta.addEntry("snippets", !accessRewriteDisabled);
+        meta.addEntry("snippets", (showSnippet && !accessRewriteDisabled));
         if (!accessRewriteDisabled) {
             meta.addEntry("tokens", showTokens);
         }
diff --git a/core/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java b/core/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
index 7c180e8..4dd51c7 100644
--- a/core/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
+++ b/core/src/main/java/de/ids_mannheim/korap/web/controller/SearchController.java
@@ -227,6 +227,7 @@
             @QueryParam("pipes") String pipes,
             @QueryParam("access-rewrite-disabled") boolean accessRewriteDisabled,
             @QueryParam("show-tokens") boolean showTokens,
+            @DefaultValue("true") @QueryParam("show-snippet") boolean showSnippet,
             @QueryParam("cq") List<String> cq, 
             @QueryParam("engine") String engine) {
 
@@ -239,7 +240,7 @@
             result = searchService.search(engine, context.getUsername(),
                     headers, q, ql, v, cq, fields, pipes, pageIndex,
                     pageInteger, ctx, pageLength, cutoff,
-                    accessRewriteDisabled, showTokens);
+                    accessRewriteDisabled, showTokens, showSnippet);
         }
         catch (KustvaktException e) {
             throw kustvaktResponseHandler.throwit(e);
diff --git a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
index 968861f..37b2b6a 100644
--- a/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
+++ b/full/src/test/java/de/ids_mannheim/korap/web/controller/SearchControllerTest.java
@@ -76,11 +76,41 @@
     }
 
     @Test
+    public void testSearchShowTokens () throws KustvaktException {
+        Response response = target().path(API_VERSION).path("search")
+                .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
+                .queryParam("show-tokens", true).request()
+                .get();
+        assertEquals(Status.OK.getStatusCode(), response.getStatus());
+        String entity = response.readEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+        assertEquals(3, node.at("/matches/0/tokens").size());
+        assertFalse(node.at("/matches/0/snippet").isMissingNode());
+    }
+    
+    @Test
+    public void testSearchDisableSnippet () throws KustvaktException {
+        Response response = target().path(API_VERSION).path("search")
+                .queryParam("q", "[orth=die]").queryParam("ql", "poliqarp")
+                .queryParam("show-snippet", false)
+                .queryParam("show-tokens", true)
+                .request()
+                .get();
+        assertEquals(Status.OK.getStatusCode(), response.getStatus());
+        String entity = response.readEntity(String.class);
+        JsonNode node = JsonUtils.readTree(entity);
+        assertTrue(node.at("/matches/0/snippet").isMissingNode());
+        assertEquals(3, node.at("/matches/0/tokens").size());
+    }
+    
+    @Test
     public void testSearchWithField () throws KustvaktException {
         JsonNode node = requestSearchWithFields("author");
         assertNotEquals(0, node.at("/matches").size());
         assertEquals("[\"author\"]",
                 node.at("/meta/fields").toString());
+        
+        assertTrue(node.at("/matches/0/tokens").isMissingNode());
     }
     
     @Test
@@ -266,6 +296,31 @@
         assertEquals("operation:insertion",
                 node.at("/collection/rewrites/0/operation").asText());
     }
+    
+    @Test
+    public void testSearchWithCorpusQuery () throws KustvaktException {
+        Response response = target().path(API_VERSION).path("search")
+                .queryParam("q", "[orth=der]").queryParam("ql", "poliqarp")
+                .queryParam("cq", "corpusTitle=gingko")
+                .request()
+                .accept(MediaType.APPLICATION_JSON).get();
+        assertEquals(Status.OK.getStatusCode(),
+                response.getStatus());
+        String ent = response.readEntity(String.class);
+        JsonNode node = JsonUtils.readTree(ent);
+
+        assertEquals("koral:docGroup", node.at("/collection/@type").asText());
+        assertEquals("operation:and", node.at("/collection/operation").asText());
+        assertEquals(2, node.at("/collection/operands").size());
+        assertEquals("CC-BY.*",
+                node.at("/collection/operands/0/value").asText());
+        
+        assertEquals("gingko",
+                node.at("/collection/operands/1/value").asText());
+        assertEquals("match:eq",
+                node.at("/collection/operands/1/match").asText());
+        assertTrue(node.at("/collection/operands/1/type").isMissingNode());
+    }
 
     @Test
     public void testSearchQueryWithCollectionQueryAuthorizedWithoutIP ()