| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap; |
| 2 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 3 | import com.fasterxml.jackson.annotation.*; |
| 4 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 5 | import com.fasterxml.jackson.databind.JsonNode; |
| 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 8 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 9 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 10 | |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 11 | import de.ids_mannheim.korap.index.PositionsToOffset; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 12 | import de.ids_mannheim.korap.index.SearchContext; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 13 | import de.ids_mannheim.korap.response.KorapResponse; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 14 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 15 | import org.slf4j.Logger; |
| 16 | import org.slf4j.LoggerFactory; |
| 17 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 20 | |
| Nils Diewald | 010c10f | 2013-12-17 01:58:31 +0000 | [diff] [blame] | 21 | /* |
| 22 | TODO: Reuse the KorapSearch code for data serialization! |
| 23 | */ |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 24 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 25 | @JsonInclude(Include.NON_NULL) |
| 26 | @JsonIgnoreProperties(ignoreUnknown = true) |
| 27 | public class KorapResult extends KorapResponse { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 28 | ObjectMapper mapper = new ObjectMapper(); |
| 29 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 30 | @JsonIgnore |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 31 | public static final short ITEMS_PER_PAGE = 25; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 32 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 33 | private int startIndex = 0; |
| 34 | private long totalTexts, totalResults; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 35 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 36 | private String query; |
| 37 | |
| 38 | private List<KorapMatch> matches; |
| 39 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 40 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 41 | private SearchContext context; |
| 42 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 43 | private short itemsPerPage = ITEMS_PER_PAGE, |
| 44 | itemsPerResource = 0; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 45 | |
| Nils Diewald | efb9c9a | 2014-02-20 15:05:18 +0000 | [diff] [blame] | 46 | private JsonNode request; |
| 47 | |
| Nils Diewald | 32912a6 | 2014-11-11 01:57:45 +0000 | [diff] [blame] | 48 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 49 | // Logger |
| Nils Diewald | 22efd2d | 2013-11-29 22:54:24 +0000 | [diff] [blame] | 50 | // This is KorapMatch instead of KorapResult! |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 51 | private final static Logger log = LoggerFactory.getLogger(KorapMatch.class); |
| 52 | |
| Nils Diewald | c6b7875 | 2013-12-05 19:05:12 +0000 | [diff] [blame] | 53 | // Empty result |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 54 | public KorapResult() { |
| 55 | mapper.enable(SerializationFeature.INDENT_OUTPUT); |
| 56 | }; |
| Nils Diewald | c6b7875 | 2013-12-05 19:05:12 +0000 | [diff] [blame] | 57 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 58 | public KorapResult(String query, |
| 59 | int startIndex, |
| 60 | short itemsPerPage, |
| 61 | SearchContext context) { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 62 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 63 | mapper.enable(SerializationFeature.INDENT_OUTPUT); |
| 64 | // mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 65 | // mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 66 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 67 | this.matches = new ArrayList<>(itemsPerPage); |
| 68 | this.query = query; |
| 69 | this.startIndex = startIndex; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 70 | this.itemsPerPage = (itemsPerPage > 50 || itemsPerPage < 1) ? |
| 71 | ITEMS_PER_PAGE : itemsPerPage; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 72 | this.context = context; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 73 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 74 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 75 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 76 | public void add (KorapMatch km) { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 77 | this.matches.add(km); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 78 | }; |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 79 | |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 80 | public KorapMatch addMatch (PositionsToOffset pto, |
| 81 | int localDocID, |
| 82 | int startPos, |
| 83 | int endPos) { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 84 | KorapMatch km = new KorapMatch(pto, localDocID, startPos, endPos); |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 85 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 86 | // Temporary - should use the same interface like results |
| 87 | // in the future: |
| 88 | km.setContext(this.context); |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 89 | this.add(km); |
| 90 | return km; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 91 | }; |
| Nils Diewald | 12f00d4 | 2013-12-12 18:47:59 +0000 | [diff] [blame] | 92 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 93 | public short getItemsPerPage() { |
| 94 | return this.itemsPerPage; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 95 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 96 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 97 | public void setRequest(JsonNode request) { |
| 98 | this.request = request; |
| Nils Diewald | ea28b62 | 2014-10-01 16:01:31 +0000 | [diff] [blame] | 99 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 100 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 101 | public JsonNode getRequest() { |
| 102 | return this.request; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 103 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 104 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 105 | // Make this working in a KorapResult class |
| 106 | // that is independent from search and collection |
| 107 | public KorapResult setTotalTexts (long i) { |
| 108 | this.totalTexts = i; |
| 109 | return this; |
| 110 | }; |
| 111 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 112 | public KorapResult incrTotalTexts (int i) { |
| 113 | this.totalTexts += i; |
| 114 | return this; |
| 115 | }; |
| 116 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 117 | public long getTotalTexts() { |
| 118 | return this.totalTexts; |
| 119 | }; |
| 120 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 121 | |
| 122 | public KorapResult setTotalResults (long i) { |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 123 | this.totalResults = i; |
| 124 | return this; |
| 125 | }; |
| 126 | |
| 127 | public KorapResult incrTotalResults (int i) { |
| 128 | this.totalResults += i; |
| 129 | return this; |
| 130 | }; |
| 131 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 132 | public long getTotalResults() { |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 133 | return this.totalResults; |
| 134 | }; |
| 135 | |
| Nils Diewald | 67f5404 | 2014-09-27 14:53:38 +0000 | [diff] [blame] | 136 | @JsonIgnore |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 137 | public void setItemsPerResource (short value) { |
| 138 | this.itemsPerResource = value; |
| 139 | }; |
| 140 | |
| Nils Diewald | 67f5404 | 2014-09-27 14:53:38 +0000 | [diff] [blame] | 141 | @JsonIgnore |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 142 | public void setItemsPerResource (int value) { |
| 143 | this.itemsPerResource = (short) value; |
| 144 | }; |
| 145 | |
| Nils Diewald | 67f5404 | 2014-09-27 14:53:38 +0000 | [diff] [blame] | 146 | @JsonIgnore |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 147 | public short getItemsPerResource () { |
| 148 | return this.itemsPerResource; |
| 149 | }; |
| 150 | |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 151 | public String getQuery () { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 152 | return this.query; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 153 | }; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 154 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 155 | @JsonIgnore |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 156 | public KorapMatch getMatch (int index) { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 157 | return this.matches.get(index); |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 158 | }; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 159 | |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 160 | @JsonIgnore |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 161 | public List<KorapMatch> getMatches() { |
| 162 | return this.matches; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 163 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 164 | |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 165 | public int getStartIndex () { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 166 | return startIndex; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 167 | }; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 168 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 169 | @JsonIgnore |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 170 | public KorapResult setContext(SearchContext context) { |
| 171 | this.context = context; |
| 172 | return this; |
| 173 | } |
| 174 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 175 | |
| 176 | @JsonIgnore |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 177 | public SearchContext getContext() { |
| 178 | return this.context; |
| 179 | } |
| 180 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 181 | |
| 182 | public JsonNode toJsonNode () { |
| 183 | ObjectNode json = (ObjectNode) mapper.valueToTree(super.toJsonNode()); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 184 | |
| Nils Diewald | 5418763 | 2014-06-11 14:39:29 +0000 | [diff] [blame] | 185 | if (this.context != null) |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 186 | json.put("context", this.getContext().toJsonNode()); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 187 | |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 188 | if (this.itemsPerResource > 0) |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 189 | json.put("itemsPerResource", |
| 190 | this.itemsPerResource); |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 191 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 192 | json.put("itemsPerPage", |
| 193 | this.itemsPerPage); |
| 194 | |
| 195 | // TODO: If test |
| 196 | if (this.request != null) |
| 197 | json.put("request", this.request); |
| 198 | |
| 199 | // TODO: If test |
| 200 | if (this.request != null) |
| 201 | json.put("request", this.request); |
| 202 | if (this.query != null) |
| 203 | json.put("query", this.query); |
| 204 | |
| 205 | json.put("startIndex", this.startIndex); |
| 206 | |
| 207 | json.put("totalResults", this.getTotalResults()); |
| 208 | |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 209 | // Add matches |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 210 | if (this.matches != null) |
| 211 | json.putPOJO("matches", this.getMatches()); |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 212 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 213 | return json; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 214 | }; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 215 | |
| 216 | |
| 217 | // For Collocation Analysis API |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 218 | public String toTokenListJsonString () { |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 219 | ObjectNode json = (ObjectNode) mapper.valueToTree(this); |
| 220 | |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 221 | ArrayNode array = json.putArray("matches"); |
| 222 | |
| 223 | // Add matches as token lists |
| 224 | for (KorapMatch km : this.getMatches()) { |
| 225 | array.add(km.toTokenList()); |
| 226 | }; |
| 227 | |
| 228 | try { |
| 229 | return mapper.writeValueAsString(json); |
| 230 | } |
| 231 | catch (Exception e) { |
| 232 | log.warn(e.getLocalizedMessage()); |
| 233 | }; |
| 234 | |
| 235 | return "{}"; |
| 236 | }; |
| 237 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 238 | }; |