| 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) { |
| Akron | 850b46e | 2016-06-08 10:08:55 +0200 | [diff] [blame] | 86 | this.fromKoral(query); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 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) { |
| Akron | 850b46e | 2016-06-08 10:08:55 +0200 | [diff] [blame] | 98 | this.fromKoral(query); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 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 | 850b46e | 2016-06-08 10:08:55 +0200 | [diff] [blame] | 141 | public Krill fromKoral (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); |
| Akron | 850b46e | 2016-06-08 10:08:55 +0200 | [diff] [blame] | 145 | this.fromKoral(this.request); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 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 | */ |
| Akron | 850b46e | 2016-06-08 10:08:55 +0200 | [diff] [blame] | 165 | public Krill fromKoral (JsonNode json) { |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 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 | 850b46e | 2016-06-08 10:08:55 +0200 | [diff] [blame] | 173 | final SpanQueryWrapper qw = kq.fromKoral(json.get("query")); |
| Akron | 001dab3 | 2015-07-02 12:30:15 +0200 | [diff] [blame] | 174 | |
| Akron | 352dae8 | 2016-08-05 17:57:51 +0200 | [diff] [blame] | 175 | // Koral messages are moved to the Krill object |
| 176 | this.moveNotificationsFrom(kq); |
| 177 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 178 | // Throw an error, in case the query matches everywhere |
| Akron | dfc9357 | 2016-08-10 19:01:34 +0200 | [diff] [blame] | 179 | if (qw.isEmpty()) { |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 180 | this.addError(780, "This query matches everywhere"); |
| Akron | f9def5e | 2016-10-10 21:26:46 +0200 | [diff] [blame] | 181 | } |
| 182 | else if (qw.isNull()) { |
| 183 | this.addError(783, "This query can't match anywhere"); |
| 184 | } |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 185 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 186 | else { |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 187 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 188 | // Serialize a Lucene SpanQuery based on the SpanQueryWrapper |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 189 | this.spanQuery = qw.toQuery(); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 190 | |
| Akron | a7b936d | 2016-03-04 13:40:54 +0100 | [diff] [blame] | 191 | // TODO: Make these information query rewrites |
| Akron | 0f3607d | 2016-02-23 22:16:20 +0100 | [diff] [blame] | 192 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 193 | // Throw a warning in case the root object is optional |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 194 | if (qw.isOptional()) |
| 195 | this.addWarning(781, "Optionality of query is ignored"); |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 196 | |
| 197 | // Throw a warning in case the root object is negative |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 198 | if (qw.isNegative()) |
| 199 | this.addWarning(782, "Exclusivity of query is ignored"); |
| 200 | }; |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 201 | } |
| 202 | catch (QueryException q) { |
| 203 | this.addError(q.getErrorCode(), q.getMessage()); |
| 204 | }; |
| 205 | } |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 206 | else |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 207 | this.addError(700, "No query given"); |
| 208 | |
| 209 | // <legacycode> |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 210 | if (json.has("warning") && json.get("warning").asText().length() > 0) { |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 211 | this.addWarning(799, json.get("warning").asText()); |
| 212 | }; |
| 213 | // </legacycode> |
| 214 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 215 | // Copy notifications from request |
| 216 | this.copyNotificationsFrom(json); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 217 | |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 218 | // Parse "collection" or "collections" attribute |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 219 | try { |
| 220 | if (json.has("collection")) { |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 221 | final JsonNode collNode = json.get("collection"); |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 222 | |
| Akron | c63697c | 2015-06-17 22:32:02 +0200 | [diff] [blame] | 223 | // TODO: Temporary |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 224 | if (collNode.fieldNames().hasNext()) { |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 225 | this.setCollection( |
| 226 | new KrillCollection().fromKoral(collNode)); |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 227 | }; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 228 | } |
| 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 | else if (json.has("collections")) { |
| Akron | 4055017 | 2015-08-04 03:06:12 +0200 | [diff] [blame] | 231 | this.addError(899, |
| 232 | "Collections are not supported anymore in favour of a single collection"); |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 233 | }; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 234 | } |
| 235 | catch (QueryException q) { |
| 236 | this.addError(q.getErrorCode(), q.getMessage()); |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 237 | }; |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 238 | |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 239 | // Parse "meta" attribute |
| Akron | 001dab3 | 2015-07-02 12:30:15 +0200 | [diff] [blame] | 240 | // !this.hasErrors() && |
| 241 | if (json.has("meta")) |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 242 | this.setMeta(new KrillMeta(json.get("meta"))); |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 243 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 244 | return this; |
| 245 | }; |
| 246 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 247 | |
| 248 | /** |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 249 | * Get the associated {@link KrillIndex} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 250 | * |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 251 | * @return The associated {@link KrillIndex} object. |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 252 | */ |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 253 | public KrillIndex getIndex () { |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 254 | return this.index; |
| 255 | }; |
| 256 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 257 | |
| 258 | /** |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 259 | * Set the {@link KrillIndex} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 260 | * |
| 261 | * @param index |
| 262 | * The associated {@link KrillIndex} object. |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 263 | * @return The {@link Krill} object for chaining. |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 264 | */ |
| Nils Diewald | a14ecd6 | 2015-02-26 21:00:20 +0000 | [diff] [blame] | 265 | public Krill setIndex (KrillIndex index) { |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 266 | this.index = index; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 267 | return this; |
| 268 | }; |
| 269 | |
| 270 | |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 271 | /** |
| 272 | * Apply the KoralQuery to an index. |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 273 | * This may invoke different actions depending |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 274 | * on the meta information, like {@link KrillIndex#search} or |
| 275 | * {@link KrillIndex#collect}. |
| 276 | * |
| 277 | * @param index |
| 278 | * The {@link KrillIndex} the search should be applyied |
| 279 | * to. |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 280 | * @return The result as a {@link Result} object. |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 281 | */ |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 282 | public Result apply (KrillIndex index) { |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 283 | return this.setIndex(index).apply(); |
| 284 | }; |
| Nils Diewald | ea28b62 | 2014-10-01 16:01:31 +0000 | [diff] [blame] | 285 | |
| Nils Diewald | c925b49 | 2013-12-03 23:56:10 +0000 | [diff] [blame] | 286 | |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 287 | /** |
| 288 | * Apply the KoralQuery to an index. |
| Nils Diewald | 21914ff | 2015-02-28 02:09:47 +0000 | [diff] [blame] | 289 | * This may invoke different actions depending |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 290 | * on the meta information, like {@link KrillIndex#search} or |
| 291 | * {@link KrillIndex#collect}. |
| 292 | * |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 293 | * @return The result as a {@link Result} object. |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 294 | */ |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 295 | public Result apply () { |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 296 | |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 297 | // Create new Result object to return |
| 298 | Result kr = new Result(); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 299 | |
| 300 | // There were errors |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 301 | if (this.hasErrors()) { |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 302 | kr.copyNotificationsFrom(this); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // There was no index |
| 306 | else if (this.index == null) { |
| 307 | kr.addError(601, "Unable to find index"); |
| 308 | } |
| 309 | |
| 310 | // Apply search |
| 311 | else { |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 312 | |
| 313 | // This contains meta and matches |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 314 | kr = this.index.search(this); |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 315 | // this.getCollection().setIndex(this.index); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 316 | kr.copyNotificationsFrom(this); |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 317 | }; |
| Nils Diewald | c6b7875 | 2013-12-05 19:05:12 +0000 | [diff] [blame] | 318 | |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 319 | kr.setQuery(this.getQuery()); |
| Akron | bb5d173 | 2015-06-22 01:22:40 +0200 | [diff] [blame] | 320 | kr.setCollection(this.getCollection()); |
| Akron | b116644 | 2015-06-27 00:34:19 +0200 | [diff] [blame] | 321 | kr.setMeta(this.getMeta()); |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 322 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 323 | return kr; |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 324 | }; |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 325 | |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 326 | |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 327 | /** |
| 328 | * Get the associated {@link SpanQuery} deserialization |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 329 | * (i.e. the internal correspandence to KoralQuery's query |
| 330 | * object). |
| 331 | * |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 332 | * <strong>Warning</strong>: SpanQueries may be lazy deserialized |
| 333 | * in future versions of Krill, rendering this API obsolete. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 334 | * |
| Nils Diewald | d37f7e4 | 2015-02-27 21:08:22 +0000 | [diff] [blame] | 335 | * @return The deserialized {@link SpanQuery} object. |
| 336 | */ |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 337 | @Deprecated |
| 338 | public SpanQuery getSpanQuery () { |
| 339 | return this.spanQuery; |
| 340 | }; |
| Nils Diewald | 9f31083 | 2013-12-06 22:38:55 +0000 | [diff] [blame] | 341 | }; |