blob: 6b5516e4988e453f5e485602638122a48abb71ad [file] [log] [blame]
Nils Diewaldf399a672013-11-18 17:55:22 +00001package de.ids_mannheim.korap;
2
Nils Diewaldd723d812014-09-23 18:50:52 +00003import com.fasterxml.jackson.annotation.*;
4import com.fasterxml.jackson.annotation.JsonInclude.Include;
Michael Hanl7edaa552014-05-23 18:48:50 +00005import com.fasterxml.jackson.databind.JsonNode;
6import com.fasterxml.jackson.databind.ObjectMapper;
7import com.fasterxml.jackson.databind.SerializationFeature;
8import com.fasterxml.jackson.databind.node.ObjectNode;
Nils Diewald277e9ce2014-11-06 03:42:11 +00009import com.fasterxml.jackson.databind.node.ArrayNode;
Nils Diewaldd723d812014-09-23 18:50:52 +000010
Nils Diewald3caa00d2013-12-13 02:24:04 +000011import de.ids_mannheim.korap.index.PositionsToOffset;
Nils Diewald1e5d5942014-05-20 13:29:53 +000012import de.ids_mannheim.korap.index.SearchContext;
Nils Diewaldc471b182014-11-19 22:51:15 +000013import de.ids_mannheim.korap.response.KorapResponse;
Nils Diewaldd723d812014-09-23 18:50:52 +000014
Nils Diewaldf399a672013-11-18 17:55:22 +000015import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
Michael Hanl7edaa552014-05-23 18:48:50 +000018import java.util.ArrayList;
19import java.util.List;
Nils Diewaldf399a672013-11-18 17:55:22 +000020
Nils Diewald010c10f2013-12-17 01:58:31 +000021/*
22TODO: Reuse the KorapSearch code for data serialization!
23*/
Nils Diewaldc471b182014-11-19 22:51:15 +000024
Nils Diewaldd723d812014-09-23 18:50:52 +000025@JsonInclude(Include.NON_NULL)
26@JsonIgnoreProperties(ignoreUnknown = true)
27public class KorapResult extends KorapResponse {
Nils Diewaldf399a672013-11-18 17:55:22 +000028 ObjectMapper mapper = new ObjectMapper();
29
Nils Diewaldd723d812014-09-23 18:50:52 +000030 @JsonIgnore
Nils Diewaldf399a672013-11-18 17:55:22 +000031 public static final short ITEMS_PER_PAGE = 25;
Nils Diewaldd723d812014-09-23 18:50:52 +000032
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000033 private int startIndex = 0;
34 private long totalTexts, totalResults;
Nils Diewaldc471b182014-11-19 22:51:15 +000035
Nils Diewaldf399a672013-11-18 17:55:22 +000036 private String query;
37
38 private List<KorapMatch> matches;
39
Nils Diewaldf399a672013-11-18 17:55:22 +000040
Nils Diewald1e5d5942014-05-20 13:29:53 +000041 private SearchContext context;
42
Nils Diewalde1ecd5e2014-11-27 02:17:24 +000043 private short itemsPerPage = ITEMS_PER_PAGE,
44 itemsPerResource = 0;
Nils Diewaldf399a672013-11-18 17:55:22 +000045
Nils Diewaldefb9c9a2014-02-20 15:05:18 +000046 private JsonNode request;
47
Nils Diewald32912a62014-11-11 01:57:45 +000048
Nils Diewaldf399a672013-11-18 17:55:22 +000049 // Logger
Nils Diewald22efd2d2013-11-29 22:54:24 +000050 // This is KorapMatch instead of KorapResult!
Nils Diewaldf399a672013-11-18 17:55:22 +000051 private final static Logger log = LoggerFactory.getLogger(KorapMatch.class);
52
Nils Diewaldc6b78752013-12-05 19:05:12 +000053 // Empty result
Nils Diewaldd723d812014-09-23 18:50:52 +000054 public KorapResult() {
55 mapper.enable(SerializationFeature.INDENT_OUTPUT);
56 };
Nils Diewaldc6b78752013-12-05 19:05:12 +000057
Michael Hanl7edaa552014-05-23 18:48:50 +000058 public KorapResult(String query,
59 int startIndex,
60 short itemsPerPage,
61 SearchContext context) {
Nils Diewaldf399a672013-11-18 17:55:22 +000062
Michael Hanl7edaa552014-05-23 18:48:50 +000063 mapper.enable(SerializationFeature.INDENT_OUTPUT);
64 // mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
Nils Diewaldd723d812014-09-23 18:50:52 +000065 // mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
Nils Diewaldf399a672013-11-18 17:55:22 +000066
Michael Hanl7edaa552014-05-23 18:48:50 +000067 this.matches = new ArrayList<>(itemsPerPage);
68 this.query = query;
69 this.startIndex = startIndex;
Nils Diewaldc471b182014-11-19 22:51:15 +000070 this.itemsPerPage = (itemsPerPage > 50 || itemsPerPage < 1) ?
71 ITEMS_PER_PAGE : itemsPerPage;
Michael Hanl7edaa552014-05-23 18:48:50 +000072 this.context = context;
Nils Diewaldc471b182014-11-19 22:51:15 +000073 };
Nils Diewaldf399a672013-11-18 17:55:22 +000074
Nils Diewaldf399a672013-11-18 17:55:22 +000075
Nils Diewaldc471b182014-11-19 22:51:15 +000076 public void add (KorapMatch km) {
Michael Hanl7edaa552014-05-23 18:48:50 +000077 this.matches.add(km);
Nils Diewaldc471b182014-11-19 22:51:15 +000078 };
Nils Diewald833fe7e2013-12-14 16:06:33 +000079
Nils Diewald277e9ce2014-11-06 03:42:11 +000080 public KorapMatch addMatch (PositionsToOffset pto,
81 int localDocID,
82 int startPos,
83 int endPos) {
Michael Hanl7edaa552014-05-23 18:48:50 +000084 KorapMatch km = new KorapMatch(pto, localDocID, startPos, endPos);
Nils Diewald1e5d5942014-05-20 13:29:53 +000085
Michael Hanl7edaa552014-05-23 18:48:50 +000086 // Temporary - should use the same interface like results
87 // in the future:
88 km.setContext(this.context);
Michael Hanl7edaa552014-05-23 18:48:50 +000089 this.add(km);
90 return km;
Nils Diewald277e9ce2014-11-06 03:42:11 +000091 };
Nils Diewald12f00d42013-12-12 18:47:59 +000092
Michael Hanl7edaa552014-05-23 18:48:50 +000093 public short getItemsPerPage() {
94 return this.itemsPerPage;
Nils Diewaldc471b182014-11-19 22:51:15 +000095 };
Nils Diewaldf399a672013-11-18 17:55:22 +000096
Michael Hanl7edaa552014-05-23 18:48:50 +000097 public void setRequest(JsonNode request) {
98 this.request = request;
Nils Diewaldea28b622014-10-01 16:01:31 +000099 };
Nils Diewaldf399a672013-11-18 17:55:22 +0000100
Michael Hanl7edaa552014-05-23 18:48:50 +0000101 public JsonNode getRequest() {
102 return this.request;
Nils Diewaldd723d812014-09-23 18:50:52 +0000103 };
Nils Diewaldf399a672013-11-18 17:55:22 +0000104
Nils Diewaldc471b182014-11-19 22:51:15 +0000105 // 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 Diewalde1ecd5e2014-11-27 02:17:24 +0000112 public KorapResult incrTotalTexts (int i) {
113 this.totalTexts += i;
114 return this;
115 };
116
Nils Diewaldc471b182014-11-19 22:51:15 +0000117 public long getTotalTexts() {
118 return this.totalTexts;
119 };
120
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000121
122 public KorapResult setTotalResults (long i) {
Nils Diewaldc471b182014-11-19 22:51:15 +0000123 this.totalResults = i;
124 return this;
125 };
126
127 public KorapResult incrTotalResults (int i) {
128 this.totalResults += i;
129 return this;
130 };
131
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000132 public long getTotalResults() {
Nils Diewaldc471b182014-11-19 22:51:15 +0000133 return this.totalResults;
134 };
135
Nils Diewald67f54042014-09-27 14:53:38 +0000136 @JsonIgnore
Nils Diewald7cf8c6d2014-05-28 18:37:38 +0000137 public void setItemsPerResource (short value) {
138 this.itemsPerResource = value;
139 };
140
Nils Diewald67f54042014-09-27 14:53:38 +0000141 @JsonIgnore
Nils Diewaldd723d812014-09-23 18:50:52 +0000142 public void setItemsPerResource (int value) {
143 this.itemsPerResource = (short) value;
144 };
145
Nils Diewald67f54042014-09-27 14:53:38 +0000146 @JsonIgnore
Nils Diewald7cf8c6d2014-05-28 18:37:38 +0000147 public short getItemsPerResource () {
148 return this.itemsPerResource;
149 };
150
Nils Diewald277e9ce2014-11-06 03:42:11 +0000151 public String getQuery () {
Michael Hanl7edaa552014-05-23 18:48:50 +0000152 return this.query;
Nils Diewald277e9ce2014-11-06 03:42:11 +0000153 };
Michael Hanl7edaa552014-05-23 18:48:50 +0000154
Nils Diewaldd723d812014-09-23 18:50:52 +0000155 @JsonIgnore
Nils Diewald277e9ce2014-11-06 03:42:11 +0000156 public KorapMatch getMatch (int index) {
Michael Hanl7edaa552014-05-23 18:48:50 +0000157 return this.matches.get(index);
Nils Diewald277e9ce2014-11-06 03:42:11 +0000158 };
Michael Hanl7edaa552014-05-23 18:48:50 +0000159
Nils Diewald277e9ce2014-11-06 03:42:11 +0000160 @JsonIgnore
Michael Hanl7edaa552014-05-23 18:48:50 +0000161 public List<KorapMatch> getMatches() {
162 return this.matches;
Nils Diewald277e9ce2014-11-06 03:42:11 +0000163 };
Nils Diewaldf399a672013-11-18 17:55:22 +0000164
Nils Diewald277e9ce2014-11-06 03:42:11 +0000165 public int getStartIndex () {
Michael Hanl7edaa552014-05-23 18:48:50 +0000166 return startIndex;
Nils Diewald277e9ce2014-11-06 03:42:11 +0000167 };
Michael Hanl7edaa552014-05-23 18:48:50 +0000168
Nils Diewaldd723d812014-09-23 18:50:52 +0000169 @JsonIgnore
Michael Hanl7edaa552014-05-23 18:48:50 +0000170 public KorapResult setContext(SearchContext context) {
171 this.context = context;
172 return this;
173 }
174
Nils Diewald1e5d5942014-05-20 13:29:53 +0000175
176 @JsonIgnore
Michael Hanl7edaa552014-05-23 18:48:50 +0000177 public SearchContext getContext() {
178 return this.context;
179 }
180
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000181
182 public JsonNode toJsonNode () {
183 ObjectNode json = (ObjectNode) mapper.valueToTree(super.toJsonNode());
Nils Diewaldf399a672013-11-18 17:55:22 +0000184
Nils Diewald54187632014-06-11 14:39:29 +0000185 if (this.context != null)
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000186 json.put("context", this.getContext().toJsonNode());
Nils Diewaldf399a672013-11-18 17:55:22 +0000187
Nils Diewald7cf8c6d2014-05-28 18:37:38 +0000188 if (this.itemsPerResource > 0)
Nils Diewaldc471b182014-11-19 22:51:15 +0000189 json.put("itemsPerResource",
190 this.itemsPerResource);
Nils Diewald7cf8c6d2014-05-28 18:37:38 +0000191
Nils Diewaldc471b182014-11-19 22:51:15 +0000192 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 Diewald277e9ce2014-11-06 03:42:11 +0000209 // Add matches
Nils Diewaldc471b182014-11-19 22:51:15 +0000210 if (this.matches != null)
211 json.putPOJO("matches", this.getMatches());
Nils Diewald277e9ce2014-11-06 03:42:11 +0000212
Nils Diewaldc471b182014-11-19 22:51:15 +0000213 return json;
Nils Diewaldd723d812014-09-23 18:50:52 +0000214 };
Nils Diewald277e9ce2014-11-06 03:42:11 +0000215
216
217 // For Collocation Analysis API
Nils Diewalde1ecd5e2014-11-27 02:17:24 +0000218 public String toTokenListJsonString () {
Nils Diewald277e9ce2014-11-06 03:42:11 +0000219 ObjectNode json = (ObjectNode) mapper.valueToTree(this);
220
Nils Diewald277e9ce2014-11-06 03:42:11 +0000221 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 Diewaldf399a672013-11-18 17:55:22 +0000238};