| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.response; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 2 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 3 | import com.fasterxml.jackson.annotation.*; |
| 4 | import com.fasterxml.jackson.annotation.JsonInclude.Include; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 5 | import com.fasterxml.jackson.databind.JsonNode; |
| 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 8 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 9 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 10 | |
| Nils Diewald | 3caa00d | 2013-12-13 02:24:04 +0000 | [diff] [blame] | 11 | import de.ids_mannheim.korap.index.PositionsToOffset; |
| Nils Diewald | f5ab4b2 | 2015-02-25 20:55:16 +0000 | [diff] [blame] | 12 | |
| 13 | // Remove: |
| Nils Diewald | 65449ff | 2015-02-27 17:57:29 +0000 | [diff] [blame] | 14 | import de.ids_mannheim.korap.response.SearchContext; |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 15 | |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 16 | import de.ids_mannheim.korap.response.Response; |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 17 | import de.ids_mannheim.korap.response.Match; |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 18 | import de.ids_mannheim.korap.Krill; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 19 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 20 | import org.slf4j.Logger; |
| 21 | import org.slf4j.LoggerFactory; |
| 22 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 25 | |
| Nils Diewald | 010c10f | 2013-12-17 01:58:31 +0000 | [diff] [blame] | 26 | /* |
| Nils Diewald | bbd39a5 | 2015-02-23 19:56:57 +0000 | [diff] [blame] | 27 | TODO: Reuse the Krill code for data serialization! |
| Nils Diewald | 010c10f | 2013-12-17 01:58:31 +0000 | [diff] [blame] | 28 | */ |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Response class for search results. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 31 | * |
| Nils Diewald | cec40f9 | 2015-02-19 22:20:02 +0000 | [diff] [blame] | 32 | * TODO: Synopsis and let it base on KoralQuery |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 33 | * |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 34 | * @author diewald |
| Nils Diewald | 0881e24 | 2015-02-27 17:31:01 +0000 | [diff] [blame] | 35 | * @see Response |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 36 | */ |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 37 | @JsonInclude(Include.NON_NULL) |
| 38 | @JsonIgnoreProperties(ignoreUnknown = true) |
| Akron | 98b7854 | 2015-08-06 21:43:08 +0200 | [diff] [blame] | 39 | public final class Result extends Krill { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 40 | ObjectMapper mapper = new ObjectMapper(); |
| 41 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 42 | @JsonIgnore |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 43 | public static final short ITEMS_PER_PAGE = 25; |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 44 | public static final short ITEMS_PER_PAGE_MAX = 100; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 45 | |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 46 | private int startIndex = 0; |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 47 | private String serialQuery; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 48 | |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 49 | private List<Match> matches; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 50 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 51 | private SearchContext context; |
| 52 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 53 | private short itemsPerPage = ITEMS_PER_PAGE, itemsPerResource = 0; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 54 | |
| Nils Diewald | efb9c9a | 2014-02-20 15:05:18 +0000 | [diff] [blame] | 55 | private JsonNode request; |
| 56 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 57 | // Logger |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 58 | // This is Match instead of Result! |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 59 | private final static Logger log = LoggerFactory.getLogger(Match.class); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 60 | |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 63 | * Construct a new Result object. |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 64 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 65 | public Result () { |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 66 | mapper.enable(SerializationFeature.INDENT_OUTPUT); |
| 67 | }; |
| Nils Diewald | c6b7875 | 2013-12-05 19:05:12 +0000 | [diff] [blame] | 68 | |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 71 | * Construct a new Result object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 72 | * |
| 73 | * @param serialQuery |
| 74 | * Query representation as a string. |
| 75 | * @param startIndex |
| 76 | * Offset position in match array. |
| 77 | * @param itemsPerPage |
| 78 | * Number of matches per page. |
| 79 | * @param context |
| 80 | * Requested {@link SearchContext} |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 81 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 82 | public Result (String query, int startIndex, short itemsPerPage, |
| 83 | SearchContext context) { |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 84 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 85 | mapper.enable(SerializationFeature.INDENT_OUTPUT); |
| 86 | // mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 87 | // mapper.disable(SerializationFeature.WRITE_NULL_MAP_VALUES); |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 88 | |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 89 | this.matches = new ArrayList<>(itemsPerPage); |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 90 | this.serialQuery = query; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 91 | this.startIndex = startIndex; |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 92 | this.itemsPerPage = (itemsPerPage > ITEMS_PER_PAGE_MAX |
| 93 | || itemsPerPage < 1) ? ITEMS_PER_PAGE : itemsPerPage; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 94 | this.context = context; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 95 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 96 | |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 97 | |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Add a new match to the result set. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 100 | * |
| 101 | * @param match |
| 102 | * A {@link Match} to add. |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 103 | */ |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 104 | public void add (Match km) { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 105 | this.matches.add(km); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 106 | }; |
| Nils Diewald | 833fe7e | 2013-12-14 16:06:33 +0000 | [diff] [blame] | 107 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 108 | |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 109 | /** |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 110 | * Get the number of items (documents) shown per page. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 111 | * |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 112 | * @return Number of items shown per page. |
| 113 | */ |
| 114 | public short getItemsPerPage () { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 115 | return this.itemsPerPage; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 116 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 117 | |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 118 | |
| 119 | /** |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 120 | * Set the number of items (documents) shown per page. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 121 | * |
| 122 | * @param count |
| 123 | * Number of items shown per page. |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 124 | * @return {@link Result} object for chaining. |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 125 | */ |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 126 | public Result setItemsPerPage (short count) { |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 127 | this.itemsPerPage = count; |
| 128 | return this; |
| Nils Diewald | ea28b62 | 2014-10-01 16:01:31 +0000 | [diff] [blame] | 129 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 130 | |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 131 | |
| 132 | /** |
| 133 | * Get serialized query as a {@link JsonNode}. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 134 | * |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 135 | * @return {@link JsonNode} representation of the query object. |
| 136 | */ |
| 137 | public JsonNode getRequest () { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 138 | return this.request; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 139 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 140 | |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Set serialized query as a {@link JsonNode}. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 144 | * |
| 145 | * @param request |
| 146 | * {@link JsonNode} representation of the query object. |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 147 | * @return {@link Result} object for chaining. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 148 | */ |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 149 | public Result setRequest (JsonNode request) { |
| Nils Diewald | afab8f3 | 2015-01-26 19:11:32 +0000 | [diff] [blame] | 150 | this.request = request; |
| 151 | return this; |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 154 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 155 | /** |
| 156 | * Get the number of items shown per resource (document). |
| 157 | * Defaults to <tt>0</tt>, which is infinite. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 158 | * |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 159 | * @return The number of items shown per resource. |
| 160 | */ |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 161 | public short getItemsPerResource () { |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 162 | return this.itemsPerResource; |
| Nils Diewald | 7cf8c6d | 2014-05-28 18:37:38 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 165 | |
| 166 | /** |
| 167 | * Set the number of items (matches) shown per resource (text). |
| 168 | * Defaults to <tt>0</tt>, which is infinite. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 169 | * |
| 170 | * @param value |
| 171 | * The number of items shown per resource. |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 172 | * @return {@link Result} object for chaining. |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 173 | */ |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 174 | public Result setItemsPerResource (short value) { |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 175 | this.itemsPerResource = value; |
| 176 | return this; |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 181 | * Set the number of items (matches) shown per resource |
| 182 | * (document). |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 183 | * Defaults to <tt>0</tt>, which is infinite. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 184 | * |
| 185 | * @param value |
| 186 | * The number of items shown per resource. |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 187 | * @return {@link Result} object for chaining. |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 188 | */ |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 189 | public Result setItemsPerResource (int value) { |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 190 | this.itemsPerResource = (short) value; |
| 191 | return this; |
| 192 | }; |
| 193 | |
| 194 | |
| 195 | /** |
| 196 | * Get the string representation of the search query. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 197 | * |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 198 | * @return The string representation of the search query. |
| 199 | */ |
| Nils Diewald | 3aa9e69 | 2015-02-20 22:20:11 +0000 | [diff] [blame] | 200 | public String getSerialQuery () { |
| 201 | return this.serialQuery; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 202 | }; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 203 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 204 | |
| 205 | /** |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 206 | * Get a certain {@link Match} by index. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 207 | * |
| 208 | * @param index |
| 209 | * The numerical index of the match, |
| 210 | * starts with <tt>0</tt>. |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 211 | * @return The {@link Match} object. |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 212 | */ |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 213 | @JsonIgnore |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 214 | public Match getMatch (int index) { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 215 | return this.matches.get(index); |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 216 | }; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 217 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 218 | |
| 219 | /** |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 220 | * Get the list of {@link Match} matches. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 221 | * |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 222 | * @return The list of {@link Match} objects. |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 223 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 224 | public List<Match> getMatches () { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 225 | return this.matches; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 226 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 227 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * Get the number of the first match in the result set |
| 231 | * (<i>aka</i> the offset). Starts with <tt>0</tt>. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 232 | * |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 233 | * @return The index number of the first match in the result set. |
| 234 | */ |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 235 | public int getStartIndex () { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 236 | return startIndex; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 237 | }; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 238 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * Get the context parameters of the search by means of a |
| 242 | * {@link SearchContext} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 243 | * |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 244 | * @return The {@link SearchContext} object. |
| 245 | */ |
| 246 | public SearchContext getContext () { |
| 247 | return this.context; |
| 248 | }; |
| 249 | |
| 250 | |
| 251 | /** |
| 252 | * Set the context parameters of the search by means of a |
| 253 | * {@link SearchContext} object. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 254 | * |
| 255 | * @param context |
| 256 | * The {@link SearchContext} object providing |
| 257 | * search context parameters. |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 258 | * @return {@link Result} object for chaining. |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 259 | */ |
| Nils Diewald | 884dbcf | 2015-02-27 17:02:28 +0000 | [diff] [blame] | 260 | public Result setContext (SearchContext context) { |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 261 | this.context = context; |
| 262 | return this; |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 263 | }; |
| Michael Hanl | 7edaa55 | 2014-05-23 18:48:50 +0000 | [diff] [blame] | 264 | |
| Nils Diewald | 1e5d594 | 2014-05-20 13:29:53 +0000 | [diff] [blame] | 265 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 266 | /** |
| 267 | * Serialize the result set as a {@link JsonNode}. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 268 | * |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 269 | * @return {@link JsonNode} representation of the search results. |
| 270 | */ |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 271 | public JsonNode toJsonNode () { |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 272 | ObjectNode json = (ObjectNode) mapper.valueToTree(super.toJsonNode()); |
| Akron | d7d7b1f | 2016-06-25 00:31:16 +0200 | [diff] [blame] | 273 | |
| Akron | d504f21 | 2015-06-20 00:27:54 +0200 | [diff] [blame] | 274 | this._addMeta(json); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 275 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 276 | // Add matches |
| 277 | if (this.matches != null) |
| 278 | json.putPOJO("matches", this.getMatches()); |
| Nils Diewald | c471b18 | 2014-11-19 22:51:15 +0000 | [diff] [blame] | 279 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 280 | return json; |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 281 | }; |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 282 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 283 | |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 284 | /** |
| 285 | * Stringifies the matches to give a brief overview on |
| 286 | * the result. Mainly used for testing. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 287 | * |
| 288 | * @return The stringified matches |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 289 | */ |
| 290 | public String getOverview () { |
| 291 | StringBuilder sb = new StringBuilder(); |
| 292 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 293 | sb.append("Search for: ").append(this.serialQuery).append("\n"); |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 294 | |
| 295 | int i = 1; |
| 296 | |
| 297 | // Add matches as bracket strings |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 298 | for (Match km : this.getMatches()) |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 299 | sb.append(i++).append(": ").append(km.getSnippetBrackets()) |
| 300 | .append(" (Doc ").append(km.getLocalDocID()).append(")\n"); |
| Nils Diewald | cd22686 | 2015-02-11 22:27:45 +0000 | [diff] [blame] | 301 | |
| 302 | return sb.toString(); |
| 303 | }; |
| 304 | |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 305 | |
| 306 | // For Collocation Analysis API |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 307 | @Deprecated |
| Nils Diewald | e1ecd5e | 2014-11-27 02:17:24 +0000 | [diff] [blame] | 308 | public String toTokenListJsonString () { |
| Akron | d504f21 | 2015-06-20 00:27:54 +0200 | [diff] [blame] | 309 | ObjectNode json = (ObjectNode) mapper.valueToTree(super.toJsonNode()); |
| Akron | b116644 | 2015-06-27 00:34:19 +0200 | [diff] [blame] | 310 | |
| 311 | // Meta data for paging |
| 312 | ObjectNode meta = json.has("meta") ? (ObjectNode) json.get("meta") |
| 313 | : (ObjectNode) json.putObject("meta"); |
| 314 | |
| 315 | if (this.context != null) |
| 316 | meta.put("context", this.getContext().toJsonNode()); |
| 317 | |
| 318 | meta.put("itemsPerPage", this.itemsPerPage); |
| 319 | meta.put("startIndex", this.startIndex); |
| 320 | |
| 321 | if (this.itemsPerResource > 0) |
| 322 | meta.put("itemsPerResource", this.itemsPerResource); |
| 323 | |
| 324 | if (this.serialQuery != null) |
| 325 | meta.put("serialQuery", this.serialQuery); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 326 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 327 | ArrayNode array = json.putArray("matches"); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 328 | |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 329 | // Add matches as token lists |
| Nils Diewald | 392bcf3 | 2015-02-26 20:01:17 +0000 | [diff] [blame] | 330 | for (Match km : this.getMatches()) |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 331 | array.add(km.toTokenList()); |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 332 | |
| 333 | try { |
| 334 | return mapper.writeValueAsString(json); |
| 335 | } |
| Nils Diewald | d75e6f6 | 2015-01-28 23:44:56 +0000 | [diff] [blame] | 336 | catch (Exception e) { |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 337 | log.warn(e.getLocalizedMessage()); |
| 338 | }; |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 339 | |
| Nils Diewald | 277e9ce | 2014-11-06 03:42:11 +0000 | [diff] [blame] | 340 | return "{}"; |
| 341 | }; |
| Akron | d504f21 | 2015-06-20 00:27:54 +0200 | [diff] [blame] | 342 | |
| 343 | |
| 344 | private void _addMeta (ObjectNode json) { |
| Akron | b116644 | 2015-06-27 00:34:19 +0200 | [diff] [blame] | 345 | ObjectNode meta = (ObjectNode) this.getMeta().toJsonNode(); |
| Akron | d504f21 | 2015-06-20 00:27:54 +0200 | [diff] [blame] | 346 | |
| Akron | b116644 | 2015-06-27 00:34:19 +0200 | [diff] [blame] | 347 | // Lucene query for debugging purposes |
| Akron | d504f21 | 2015-06-20 00:27:54 +0200 | [diff] [blame] | 348 | if (this.serialQuery != null) |
| 349 | meta.put("serialQuery", this.serialQuery); |
| Akron | d504f21 | 2015-06-20 00:27:54 +0200 | [diff] [blame] | 350 | |
| Akron | b116644 | 2015-06-27 00:34:19 +0200 | [diff] [blame] | 351 | // This may override count |
| 352 | meta.put("itemsPerPage", this.itemsPerPage); |
| Akron | d504f21 | 2015-06-20 00:27:54 +0200 | [diff] [blame] | 353 | |
| Akron | b116644 | 2015-06-27 00:34:19 +0200 | [diff] [blame] | 354 | if (json.has("meta")) { |
| 355 | ((ObjectNode) json.get("meta")).putAll(meta); |
| 356 | } |
| 357 | else { |
| 358 | json.put("meta", meta); |
| 359 | }; |
| Akron | d504f21 | 2015-06-20 00:27:54 +0200 | [diff] [blame] | 360 | }; |
| Nils Diewald | f399a67 | 2013-11-18 17:55:22 +0000 | [diff] [blame] | 361 | }; |