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