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