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