Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 1ca81a8a

Přidáno uživatelem Jitka Poubová před více než 4 roky(ů)

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

Zobrazit rozdíly:

be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/IndexController.java
2 2

  
3 3
import cz.zcu.kiv.aswi.fulltextsearch.model.DocumentResponse;
4 4
import cz.zcu.kiv.aswi.fulltextsearch.model.Query;
5
import cz.zcu.kiv.aswi.fulltextsearch.model.QueryResponse;
5
import cz.zcu.kiv.aswi.fulltextsearch.model.QueryDocumentResponse;
6 6
import cz.zcu.kiv.aswi.fulltextsearch.model.ResponseMessage;
7 7
import org.springframework.http.HttpStatus;
8 8
import org.springframework.http.ResponseEntity;
......
20 20
    private SolrService solrService = new SolrService();
21 21

  
22 22
    @PostMapping("/")
23
    public QueryResponse index(@RequestBody Query query) {
24
        String response;
25

  
23
    public List<QueryDocumentResponse> index(@RequestBody Query query) {
26 24
        try {
27
            response = solrService.query(query.getQuery());
25
            return solrService.query(query.getQuery());
28 26
        } catch (Exception e) {
29
            response = "Unknown exception";
30 27
            e.printStackTrace();
28
            return null;
31 29
        }
32

  
33
        return new QueryResponse(response);
34 30
    }
35 31

  
36 32

  
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/SolrService.java
3 3
import cz.zcu.kiv.aswi.fulltextsearch.document.PcGts;
4 4
import cz.zcu.kiv.aswi.fulltextsearch.document.TextLine;
5 5
import cz.zcu.kiv.aswi.fulltextsearch.document.TextRegion;
6
import cz.zcu.kiv.aswi.fulltextsearch.model.QueryDocumentResponse;
6 7
import org.apache.solr.client.solrj.SolrQuery;
7 8
import org.apache.solr.client.solrj.SolrServerException;
8 9
import org.apache.solr.client.solrj.impl.HttpSolrClient;
......
15 16

  
16 17
import javax.xml.bind.JAXBException;
17 18
import java.io.IOException;
19
import java.io.InputStream;
18 20
import java.util.*;
19 21

  
20 22
public class SolrService {
......
77 79

  
78 80
    private void addBasicFieldsToSolr() {
79 81
        addFieldToSolr(FIELD_DOC_FILENAME);
80
        //addFieldToSolr(FIELD_IMG_FILENAME); // todo
82
        addFieldToSolr(FIELD_IMG_FILENAME);
81 83
        addFieldToSolr(FIELD_TEXT_REGION);
82 84
        addFieldToSolr(FIELD_TEXT_REGION_COORDS);
83 85
    }
......
101 103
            fieldAttributes.put("name", name);
102 104
            fieldAttributes.put("type", "text_general");
103 105
            fieldAttributes.put("stored", true);
106
            fieldAttributes.put("multiValued", false);
104 107
            SchemaRequest.Update addFieldRequest = new SchemaRequest.AddField(fieldAttributes);
105 108
            addFieldRequest.process(solrClient);
106 109

  
......
116 119
        solrClient.commit();
117 120
    }
118 121

  
119
    public String query(String query) throws IOException, SolrServerException  {
122
    public List<QueryDocumentResponse> query(String query) throws IOException, SolrServerException  {
120 123
        SolrQuery solrQuery = new SolrQuery();
121 124
        solrQuery.set("q", FIELD_TEXT_REGION + ":" + query);
125
        solrQuery.setHighlight(true);
126
        solrQuery.addHighlightField(PREFIX_TEXT_LINE +  "*");
122 127
        QueryResponse response = solrClient.query(solrQuery);
123 128

  
124 129
        SolrDocumentList docList = response.getResults();
125
        Set<String> ids = getUniqueIds(docList);
130
        Map<String, Map<String, List<String>>> highlight = response.getHighlighting();
131

  
132
        List<QueryDocumentResponse> results = new ArrayList<>();
133
        for (SolrDocument solrDocument: docList) {
134
            QueryDocumentResponse documentResponse = new QueryDocumentResponse();
135

  
136
            // filename, text region
137
            documentResponse.setDoc_filename((String) solrDocument.getFieldValue(FIELD_DOC_FILENAME));
138
            documentResponse.setText_region((String) solrDocument.getFieldValue(FIELD_TEXT_REGION));
139
            documentResponse.setText_region_coords((String) solrDocument.getFieldValue(FIELD_TEXT_REGION_COORDS));
140

  
141
            // text lines
142
            Map<String, List<String>> map = highlight.get(solrDocument.getFieldValue("id").toString());
143
            Set<String> linesNames = map.keySet();
144
            Map<String, String> linesMap = new HashMap<>();
145
            for (String lineName: linesNames) {
146
                String value = (String) solrDocument.getFieldValue(lineName);
147
                String coordsValue = (String) solrDocument.getFieldValue(lineName + SUFFIX_COORDS);
148
                linesMap.put(lineName, value);
149
                linesMap.put(lineName + SUFFIX_COORDS, coordsValue);
150
            }
151
            documentResponse.setText_lines(linesMap);
152

  
153
            // image
154
            String imgFilename = (String) solrDocument.getFieldValue(FIELD_IMG_FILENAME);
155
            InputStream is = getClass().getClassLoader().getResourceAsStream(SolrService.DATA_DIR + imgFilename);
156
            String encodedImage = Base64.getEncoder().encodeToString(is.readAllBytes());
157
            documentResponse.setImage(encodedImage);
126 158

  
127
        return "Found in " + ids.size() + " documents with ids=" + String.join(", ", ids);
159
            results.add(documentResponse);
160
        }
161

  
162
        return results;
128 163
    }
129 164

  
130 165
    private void addDocument(PcGts document) throws IOException, SolrServerException {
......
136 171
            SolrInputDocument solrInputDocument = new SolrInputDocument();
137 172

  
138 173
            solrInputDocument.addField(FIELD_DOC_FILENAME, document.getFilename());
174
            solrInputDocument.addField(FIELD_IMG_FILENAME, document.getFilename().replaceAll(".xml", ".png")); // todo
139 175
            solrInputDocument.addField(FIELD_TEXT_REGION, textRegion.getTextEquiv().getUnicode());
140 176
            solrInputDocument.addField(FIELD_TEXT_REGION_COORDS, textRegion.getCoords().getPoints());
141 177

  
......
147 183

  
148 184
                addFieldToSolr(prefix);
149 185
                solrInputDocument.addField(prefix, textLine.getTextEquiv().getUnicode());
186

  
187
                addFieldToSolr(prefix + SUFFIX_COORDS);
150 188
                solrInputDocument.addField(prefix + SUFFIX_COORDS, textLine.getCoords().getPoints());
151 189
            }
152 190

  
be/fulltextsearch/src/main/java/cz/zcu/kiv/aswi/fulltextsearch/model/QueryDocumentResponse.java
1
package cz.zcu.kiv.aswi.fulltextsearch.model;
2

  
3
import java.util.HashMap;
4
import java.util.Map;
5

  
6
public class QueryDocumentResponse {
7

  
8
    private String doc_filename;
9
    private String image; // PNG in Base64
10
    private String text_region;
11
    private String text_region_coords;
12
    private Map<String, String> text_lines = new HashMap<>();
13

  
14
    public String getDoc_filename() {
15
        return doc_filename;
16
    }
17

  
18
    public void setDoc_filename(String doc_filename) {
19
        this.doc_filename = doc_filename;
20
    }
21

  
22
    public String getText_region() {
23
        return text_region;
24
    }
25

  
26
    public void setText_region(String text_region) {
27
        this.text_region = text_region;
28
    }
29

  
30
    public String getText_region_coords() {
31
        return text_region_coords;
32
    }
33

  
34
    public void setText_region_coords(String text_region_coords) {
35
        this.text_region_coords = text_region_coords;
36
    }
37

  
38
    public Map<String, String> getText_lines() {
39
        return text_lines;
40
    }
41

  
42
    public void setText_lines(Map<String, String> text_lines) {
43
        this.text_lines = text_lines;
44
    }
45

  
46
    public String getImage() {
47
        return image;
48
    }
49

  
50
    public void setImage(String image) {
51
        this.image = image;
52
    }
53
}

Také k dispozici: Unified diff