commit 1ca81a8a8861d22d732a8cd9954ff8d8aaa60a46
Author: jpoubova <jpoubova@students.zcu.cz>
Date:   Wed Apr 8 19:08:41 2020 +0200

    Re #7805: Vracení nalezených dokumentů na query
    - na / to vrací všechny dokumenty, které dané slovo obsahují
    - součástí JSONu je také PNG obrázek zakódovaný v base64

diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java
index 9ed00d1..19b0cb5 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java
@@ -2,7 +2,7 @@ package cz.zcu.kiv.aswi.fulltextsearch;
 
 import cz.zcu.kiv.aswi.fulltextsearch.model.DocumentResponse;
 import cz.zcu.kiv.aswi.fulltextsearch.model.Query;
-import cz.zcu.kiv.aswi.fulltextsearch.model.QueryResponse;
+import cz.zcu.kiv.aswi.fulltextsearch.model.QueryDocumentResponse;
 import cz.zcu.kiv.aswi.fulltextsearch.model.ResponseMessage;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
@@ -20,17 +20,13 @@ public class IndexController {
     private SolrService solrService = new SolrService();
 
     @PostMapping("/")
-    public QueryResponse index(@RequestBody Query query) {
-        String response;
-
+    public List<QueryDocumentResponse> index(@RequestBody Query query) {
         try {
-            response = solrService.query(query.getQuery());
+            return solrService.query(query.getQuery());
         } catch (Exception e) {
-            response = "Unknown exception";
             e.printStackTrace();
+            return null;
         }
-
-        return new QueryResponse(response);
     }
 
 
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
index 73990ef..2f3d253 100644
--- a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
@@ -3,6 +3,7 @@ package cz.zcu.kiv.aswi.fulltextsearch;
 import cz.zcu.kiv.aswi.fulltextsearch.document.PcGts;
 import cz.zcu.kiv.aswi.fulltextsearch.document.TextLine;
 import cz.zcu.kiv.aswi.fulltextsearch.document.TextRegion;
+import cz.zcu.kiv.aswi.fulltextsearch.model.QueryDocumentResponse;
 import org.apache.solr.client.solrj.SolrQuery;
 import org.apache.solr.client.solrj.SolrServerException;
 import org.apache.solr.client.solrj.impl.HttpSolrClient;
@@ -15,6 +16,7 @@ import org.apache.solr.common.SolrInputDocument;
 
 import javax.xml.bind.JAXBException;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.*;
 
 public class SolrService {
@@ -77,7 +79,7 @@ public class SolrService {
 
     private void addBasicFieldsToSolr() {
         addFieldToSolr(FIELD_DOC_FILENAME);
-        //addFieldToSolr(FIELD_IMG_FILENAME); // todo
+        addFieldToSolr(FIELD_IMG_FILENAME);
         addFieldToSolr(FIELD_TEXT_REGION);
         addFieldToSolr(FIELD_TEXT_REGION_COORDS);
     }
@@ -101,6 +103,7 @@ public class SolrService {
             fieldAttributes.put("name", name);
             fieldAttributes.put("type", "text_general");
             fieldAttributes.put("stored", true);
+            fieldAttributes.put("multiValued", false);
             SchemaRequest.Update addFieldRequest = new SchemaRequest.AddField(fieldAttributes);
             addFieldRequest.process(solrClient);
 
@@ -116,15 +119,47 @@ public class SolrService {
         solrClient.commit();
     }
 
-    public String query(String query) throws IOException, SolrServerException  {
+    public List<QueryDocumentResponse> query(String query) throws IOException, SolrServerException  {
         SolrQuery solrQuery = new SolrQuery();
         solrQuery.set("q", FIELD_TEXT_REGION + ":" + query);
+        solrQuery.setHighlight(true);
+        solrQuery.addHighlightField(PREFIX_TEXT_LINE +  "*");
         QueryResponse response = solrClient.query(solrQuery);
 
         SolrDocumentList docList = response.getResults();
-        Set<String> ids = getUniqueIds(docList);
+        Map<String, Map<String, List<String>>> highlight = response.getHighlighting();
+
+        List<QueryDocumentResponse> results = new ArrayList<>();
+        for (SolrDocument solrDocument: docList) {
+            QueryDocumentResponse documentResponse = new QueryDocumentResponse();
+
+            // filename, text region
+            documentResponse.setDoc_filename((String) solrDocument.getFieldValue(FIELD_DOC_FILENAME));
+            documentResponse.setText_region((String) solrDocument.getFieldValue(FIELD_TEXT_REGION));
+            documentResponse.setText_region_coords((String) solrDocument.getFieldValue(FIELD_TEXT_REGION_COORDS));
+
+            // text lines
+            Map<String, List<String>> map = highlight.get(solrDocument.getFieldValue("id").toString());
+            Set<String> linesNames = map.keySet();
+            Map<String, String> linesMap = new HashMap<>();
+            for (String lineName: linesNames) {
+                String value = (String) solrDocument.getFieldValue(lineName);
+                String coordsValue = (String) solrDocument.getFieldValue(lineName + SUFFIX_COORDS);
+                linesMap.put(lineName, value);
+                linesMap.put(lineName + SUFFIX_COORDS, coordsValue);
+            }
+            documentResponse.setText_lines(linesMap);
+
+            // image
+            String imgFilename = (String) solrDocument.getFieldValue(FIELD_IMG_FILENAME);
+            InputStream is = getClass().getClassLoader().getResourceAsStream(SolrService.DATA_DIR + imgFilename);
+            String encodedImage = Base64.getEncoder().encodeToString(is.readAllBytes());
+            documentResponse.setImage(encodedImage);
 
-        return "Found in " + ids.size() + " documents with ids=" + String.join(", ", ids);
+            results.add(documentResponse);
+        }
+
+        return results;
     }
 
     private void addDocument(PcGts document) throws IOException, SolrServerException {
@@ -136,6 +171,7 @@ public class SolrService {
             SolrInputDocument solrInputDocument = new SolrInputDocument();
 
             solrInputDocument.addField(FIELD_DOC_FILENAME, document.getFilename());
+            solrInputDocument.addField(FIELD_IMG_FILENAME, document.getFilename().replaceAll(".xml", ".png")); // todo
             solrInputDocument.addField(FIELD_TEXT_REGION, textRegion.getTextEquiv().getUnicode());
             solrInputDocument.addField(FIELD_TEXT_REGION_COORDS, textRegion.getCoords().getPoints());
 
@@ -147,6 +183,8 @@ public class SolrService {
 
                 addFieldToSolr(prefix);
                 solrInputDocument.addField(prefix, textLine.getTextEquiv().getUnicode());
+
+                addFieldToSolr(prefix + SUFFIX_COORDS);
                 solrInputDocument.addField(prefix + SUFFIX_COORDS, textLine.getCoords().getPoints());
             }
 
diff --git a/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/QueryDocumentResponse.java b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/QueryDocumentResponse.java
new file mode 100644
index 0000000..058a9f1
--- /dev/null
+++ b/be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/QueryDocumentResponse.java
@@ -0,0 +1,53 @@
+package cz.zcu.kiv.aswi.fulltextsearch.model;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class QueryDocumentResponse {
+
+    private String doc_filename;
+    private String image; // PNG in Base64
+    private String text_region;
+    private String text_region_coords;
+    private Map<String, String> text_lines = new HashMap<>();
+
+    public String getDoc_filename() {
+        return doc_filename;
+    }
+
+    public void setDoc_filename(String doc_filename) {
+        this.doc_filename = doc_filename;
+    }
+
+    public String getText_region() {
+        return text_region;
+    }
+
+    public void setText_region(String text_region) {
+        this.text_region = text_region;
+    }
+
+    public String getText_region_coords() {
+        return text_region_coords;
+    }
+
+    public void setText_region_coords(String text_region_coords) {
+        this.text_region_coords = text_region_coords;
+    }
+
+    public Map<String, String> getText_lines() {
+        return text_lines;
+    }
+
+    public void setText_lines(Map<String, String> text_lines) {
+        this.text_lines = text_lines;
+    }
+
+    public String getImage() {
+        return image;
+    }
+
+    public void setImage(String image) {
+        this.image = image;
+    }
+}
