| Nils Diewald | c925b49 | 2013-12-03 23:56:10 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap; |
| 2 | |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 3 | import java.io.*; |
| Nils Diewald | c7d08d9 | 2014-11-05 21:30:05 +0000 | [diff] [blame] | 4 | import java.util.*; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 5 | |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 6 | import org.apache.lucene.search.spans.SpanQuery; |
| Nils Diewald | 92729ce | 2014-10-06 16:00:17 +0000 | [diff] [blame] | 7 | import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper; |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 8 | import de.ids_mannheim.korap.KrillIndex; |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 9 | import de.ids_mannheim.korap.response.Result; |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 10 | import de.ids_mannheim.korap.util.QueryException; |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 11 | import de.ids_mannheim.korap.response.Notifications; |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 12 | import de.ids_mannheim.korap.response.Response; |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 13 | |
| 14 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | import com.fasterxml.jackson.databind.JsonNode; |
| Nils Diewald | c925b49 | 2013-12-03 23:56:10 +0000 | [diff] [blame] | 16 | |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 17 | import org.slf4j.Logger; |
| 18 | import org.slf4j.LoggerFactory; |
| 19 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 20 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 21 | * <p>Krill is a corpus data retrieval index using Lucene for |
| 22 | * Look-Ups.</p> |
| 23 | * |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 24 | * <p> |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 25 | * It is the reference implementation for KoralQuery consumption, |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 26 | * and this class acts as the central point for consuming and |
| 27 | * responding to KoralQuery requests. |
| 28 | * </p> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 29 | * |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 30 | * <p> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 31 | * The processing of the collection section of the request is |
| 32 | * delegated |
| 33 | * to {@link KrillCollection}, the query section to {@link KrillQuery} |
| 34 | * , |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 35 | * and the meta section to {@link KrillMeta}. |
| 36 | * </p> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 37 | * |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 38 | * <blockquote><pre> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 39 | * // Create or receive a KoralQuery JSON string |
| 40 | * String koral = "{\"query\":{...}, \"collection\":{...}, ... }"; |
| 41 | * |
| 42 | * // Create a new krill search object by passing the Query |
| 43 | * Krill krill = new Krill(koral); |
| 44 | * |
| 45 | * // Apply the query to an index and receive a search result |
| 46 | * // This may invoke different actions depending on the request |
| 47 | * Result result = krill.setIndex(new KrillIndex()).apply(); |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 48 | * </pre></blockquote> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 49 | * |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 50 | * @author diewald |
| 51 | * @author margaretha |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 52 | * |
| Nils Diewald | 2d5f810 | 2015-02-26 21:07:54 +0000 | [diff] [blame] | 53 | * @see KrillCollection |
| Nils Diewald | 0339d46 | 2015-02-26 14:53:56 +0000 | [diff] [blame] | 54 | * @see KrillQuery |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 55 | * @see KrillMeta |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 56 | * @see KrillIndex |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 57 | */ |
| Akron | b116644 | 2015-06-27 00:34:19 +0200 | [diff] [blame] | 58 | // TODO: Use a krill.properties configuration file |
| 59 | // TODO: Reuse passed JSON object instead of creating a new response! |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 60 | public class Krill extends Response { |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 61 | private KrillIndex index; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 62 | private SpanQuery spanQuery; |
| Nils Diewald | efb9c9a | 2014-02-20 15:05:18 +0000 | [diff] [blame] | 63 | private JsonNode request; |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 64 | private String spanContext; |
| Nils Diewald | 364eb64 | 2013-12-22 15:03:01 +0000 | [diff] [blame] | 65 | |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 66 | private final ObjectMapper mapper = new ObjectMapper(); |
| 67 | |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 68 | // Logger |
| 69 | private final static Logger log = LoggerFactory.getLogger(Krill.class); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 70 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 71 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Construct a new Krill object. |
| 74 | */ |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 75 | public Krill () {}; |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 76 | |
| 77 | |
| 78 | /** |
| 79 | * Construct a new Krill object, |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 80 | * consuming a KoralQuery json string. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 81 | * |
| 82 | * @param query |
| 83 | * The KoralQuery json string. |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 84 | */ |
| 85 | public Krill (String query) { |
| 86 | this.fromJson(query); |
| 87 | }; |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * Construct a new Krill object, |
| 92 | * consuming a KoralQuery {@link JsonNode} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 93 | * |
| 94 | * @param query |
| 95 | * The KoralQuery {@link JsonNode} object. |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 96 | */ |
| 97 | public Krill (JsonNode query) { |
| 98 | this.fromJson(query); |
| 99 | }; |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Construct a new Krill object, |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 104 | * consuming a {@link SpanQueryWrapper} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 105 | * |
| 106 | * @param query |
| 107 | * The {@link SpanQueryWrapper} object. |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 108 | */ |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 109 | public Krill (SpanQueryWrapper query) { |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 110 | try { |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 111 | this.spanQuery = query.toQuery(); |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 112 | } |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 113 | |
| 114 | // Add the error to the KoralQuery response |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 115 | catch (QueryException q) { |
| 116 | this.addError(q.getErrorCode(), q.getMessage()); |
| 117 | }; |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 118 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 119 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Construct a new Krill object, |
| 123 | * consuming a {@link SpanQuery} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 124 | * |
| 125 | * @param query |
| 126 | * The {@link SpanQuery} object. |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 127 | */ |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 128 | public Krill (SpanQuery query) { |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 129 | this.spanQuery = query; |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 130 | }; |
| Nils Diewald | c925b49 | 2013-12-03 23:56:10 +0000 | [diff] [blame] | 131 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 132 | |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 133 | /** |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 134 | * Parse KoralQuery as a json string. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 135 | * |
| 136 | * @param query |
| 137 | * The KoralQuery json string. |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 138 | * @return The {@link Krill} object for chaining. |
| 139 | * @throws QueryException |
| 140 | */ |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 141 | public Krill fromJson (final String query) { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 142 | // Parse query string |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 143 | try { |
| 144 | this.request = mapper.readTree(query); |
| 145 | this.fromJson(this.request); |
| 146 | } |
| 147 | |
| 148 | // Unable to parse JSON |
| 149 | catch (IOException e) { |
| 150 | this.addError(621, "Unable to parse JSON"); |
| 151 | }; |
| 152 | |
| 153 | return this; |
| 154 | }; |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * Parse KoralQuery as a {@link JsonNode} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 159 | * |
| 160 | * @param query |
| 161 | * The KoralQuery {@link JsonNode} object. |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 162 | * @return The {@link Krill} object for chaining. |
| 163 | * @throws QueryException |
| 164 | */ |
| 165 | public Krill fromJson (JsonNode json) { |
| 166 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 167 | // Parse "query" attribute |
| 168 | if (json.has("query")) { |
| 169 | try { |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 170 | final KrillQuery kq = new KrillQuery("tokens"); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 171 | this.setQuery(kq); |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 172 | |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 173 | final SpanQueryWrapper qw = kq.fromJson(json.get("query")); |
| Akron | 001dab3 | 2015-07-02 12:30:15 +0200 | [diff] [blame] | 174 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 175 | // Throw an error, in case the query matches everywhere |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 176 | if (qw.isEmpty()) |
| 177 | this.addError(780, "This query matches everywhere"); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 178 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 179 | else { |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 180 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 181 | // Serialize a Lucene SpanQuery based on the SpanQueryWrapper |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 182 | this.spanQuery = qw.toQuery(); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 183 | |
| 184 | // Throw a warning in case the root object is optional |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 185 | if (qw.isOptional()) |
| 186 | this.addWarning(781, "Optionality of query is ignored"); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 187 | |
| 188 | // Throw a warning in case the root object is negative |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 189 | if (qw.isNegative()) |
| 190 | this.addWarning(782, "Exclusivity of query is ignored"); |
| 191 | }; |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 192 | } |
| 193 | catch (QueryException q) { |
| 194 | this.addError(q.getErrorCode(), q.getMessage()); |
| 195 | }; |
| 196 | } |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 197 | else |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 198 | this.addError(700, "No query given"); |
| 199 | |
| 200 | // <legacycode> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 201 | if (json.has("warning") && json.get("warning").asText().length() > 0) { |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 202 | this.addWarning(799, json.get("warning").asText()); |
| 203 | }; |
| 204 | // </legacycode> |
| 205 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 206 | // Copy notifications from request |
| 207 | this.copyNotificationsFrom(json); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 208 | |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 209 | // Parse "collection" or "collections" attribute |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 210 | try { |
| 211 | if (json.has("collection")) { |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 212 | final JsonNode collNode = json.get("collection"); |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 213 | |
| Akron | c63697c | 2015-06-17 22:32:02 +0200 | [diff] [blame] | 214 | // TODO: Temporary |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 215 | if (collNode.fieldNames().hasNext()) { |
| Akron | 176c9b1 | 2015-07-29 19:53:40 +0200 | [diff] [blame] | 216 | this.setCollection(new KrillCollection().fromJson(collNode)); |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 217 | }; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 218 | } |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 219 | |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 220 | else if (json.has("collections")) { |
| Akron | 4055017 | 2015-08-04 03:06:12 +0200 | [diff] [blame] | 221 | this.addError(899, |
| 222 | "Collections are not supported anymore in favour of a single collection"); |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 223 | }; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 224 | } |
| 225 | catch (QueryException q) { |
| 226 | this.addError(q.getErrorCode(), q.getMessage()); |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 227 | }; |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 228 | |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 229 | // Parse "meta" attribute |
| Akron | 001dab3 | 2015-07-02 12:30:15 +0200 | [diff] [blame] | 230 | // !this.hasErrors() && |
| 231 | if (json.has("meta")) |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 232 | this.setMeta(new KrillMeta(json.get("meta"))); |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 233 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 234 | return this; |
| 235 | }; |
| 236 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 237 | |
| 238 | /** |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 239 | * Get the associated {@link KrillIndex} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 240 | * |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 241 | * @return The associated {@link KrillIndex} object. |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 242 | */ |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 243 | public KrillIndex getIndex () { |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 244 | return this.index; |
| 245 | }; |
| 246 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 247 | |
| 248 | /** |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 249 | * Set the {@link KrillIndex} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 250 | * |
| 251 | * @param index |
| 252 | * The associated {@link KrillIndex} object. |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 253 | * @return The {@link Krill} object for chaining. |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 254 | */ |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 255 | public Krill setIndex (KrillIndex index) { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 256 | this.index = index; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 257 | return this; |
| 258 | }; |
| 259 | |
| 260 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 261 | /** |
| 262 | * Apply the KoralQuery to an index. |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 263 | * This may invoke different actions depending |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 264 | * on the meta information, like {@link KrillIndex#search} or |
| 265 | * {@link KrillIndex#collect}. |
| 266 | * |
| 267 | * @param index |
| 268 | * The {@link KrillIndex} the search should be applyied |
| 269 | * to. |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 270 | * @return The result as a {@link Result} object. |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 271 | */ |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 272 | public Result apply (KrillIndex index) { |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 273 | return this.setIndex(index).apply(); |
| 274 | }; |
| Nils Diewald | ea28b62 | 2014-10-01 16:01:31 +0000 | [diff] [blame] | 275 | |
| Nils Diewald | c925b49 | 2013-12-03 23:56:10 +0000 | [diff] [blame] | 276 | |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 277 | /** |
| 278 | * Apply the KoralQuery to an index. |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 279 | * This may invoke different actions depending |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 280 | * on the meta information, like {@link KrillIndex#search} or |
| 281 | * {@link KrillIndex#collect}. |
| 282 | * |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 283 | * @return The result as a {@link Result} object. |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 284 | */ |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 285 | public Result apply () { |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 286 | |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 287 | // Create new Result object to return |
| 288 | Result kr = new Result(); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 289 | |
| 290 | // There were errors |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 291 | if (this.hasErrors()) { |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 292 | kr.copyNotificationsFrom(this); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // There was no index |
| 296 | else if (this.index == null) { |
| 297 | kr.addError(601, "Unable to find index"); |
| 298 | } |
| 299 | |
| 300 | // Apply search |
| 301 | else { |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 302 | |
| 303 | // This contains meta and matches |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 304 | kr = this.index.search(this); |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 305 | // this.getCollection().setIndex(this.index); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 306 | kr.copyNotificationsFrom(this); |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 307 | }; |
| Nils Diewald | c6b7875 | 2013-12-05 19:05:12 +0000 | [diff] [blame] | 308 | |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 309 | kr.setQuery(this.getQuery()); |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 310 | kr.setCollection(this.getCollection()); |
| Akron | b116644 | 2015-06-27 00:34:19 +0200 | [diff] [blame] | 311 | kr.setMeta(this.getMeta()); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 312 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 313 | return kr; |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 314 | }; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 315 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 316 | |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 317 | /** |
| 318 | * Get the associated {@link SpanQuery} deserialization |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 319 | * (i.e. the internal correspandence to KoralQuery's query |
| 320 | * object). |
| 321 | * |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 322 | * <strong>Warning</strong>: SpanQueries may be lazy deserialized |
| 323 | * in future versions of Krill, rendering this API obsolete. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 324 | * |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 325 | * @return The deserialized {@link SpanQuery} object. |
| 326 | */ |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 327 | @Deprecated |
| 328 | public SpanQuery getSpanQuery () { |
| 329 | return this.spanQuery; |
| 330 | }; |
| 331 | |
| 332 | |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 333 | /** |
| 334 | * Set the SpanQuery by means of a {@link SpanQueryWrapper} object |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 335 | * (i.e. the internal correspandence to KoralQuery's query |
| 336 | * object). |
| 337 | * |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 338 | * <strong>Warning</strong>: SpanQueries may be lazy deserialized |
| 339 | * in future versions of Krill, rendering this API obsolete. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 340 | * |
| 341 | * @param query |
| 342 | * The {@link SpanQueryWrapper} to unwrap |
| 343 | * the {@link SpanQuery} object. |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 344 | * @return The {@link Krill} object for chaining. |
| 345 | */ |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 346 | @Deprecated |
| 347 | public Krill setSpanQuery (SpanQueryWrapper sqwi) { |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 348 | try { |
| 349 | this.spanQuery = sqwi.toQuery(); |
| 350 | } |
| 351 | catch (QueryException q) { |
| 352 | this.addError(q.getErrorCode(), q.getMessage()); |
| 353 | }; |
| 354 | return this; |
| 355 | }; |
| 356 | |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 357 | |
| 358 | /** |
| 359 | * Set the {@link SpanQuery} object |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 360 | * (i.e. the internal correspandence to KoralQuery's query |
| 361 | * object). |
| 362 | * |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 363 | * <strong>Warning</strong>: SpanQueries may be lazy deserialized |
| 364 | * in future versions of Krill, rendering this API obsolete. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 365 | * |
| 366 | * @param query |
| 367 | * The {@link SpanQuery} object. |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 368 | * @return The {@link Krill} object for chaining. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 369 | */ |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 370 | @Deprecated |
| 371 | public Krill setSpanQuery (SpanQuery sq) { |
| 372 | this.spanQuery = sq; |
| 373 | return this; |
| 374 | }; |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 375 | |
| 376 | |
| 377 | // Requests are out - queries will be mirrored completely |
| 378 | @Deprecated |
| 379 | public JsonNode getRequest () { |
| 380 | return this.request; |
| 381 | }; |
| Nils Diewald | 9f31083 | 2013-12-06 22:38:55 +0000 | [diff] [blame] | 382 | }; |