blob: ebe9cc7ef41dd12e725146fb6860868d1600114d [file] [log] [blame]
Joachim Bingel43a444a2014-08-07 15:06:02 +00001import static org.junit.Assert.*;
Michael Hanlf1fead42014-05-14 15:13:33 +00002import de.ids_mannheim.korap.query.serialize.CollectionQueryTree;
Michael Hanlf1fead42014-05-14 15:13:33 +00003import de.ids_mannheim.korap.util.QueryException;
Michael Hanlf1fead42014-05-14 15:13:33 +00004import org.junit.Test;
5
6public class CollectionQueryTreeTest {
7
Joachim Bingela3f51f72014-07-22 14:45:31 +00008 CollectionQueryTree cqt;
9 String map;
10 private String query;
11 private String expected;
Michael Hanlf1fead42014-05-14 15:13:33 +000012
Joachim Bingela3f51f72014-07-22 14:45:31 +000013 @Test
14 public void testSimple() throws QueryException {
15 query = "textClass=Sport";
16 // String regex1 = "{@type=korap:filter, filter={@type=korap:doc, attribute=textClass, key=Sport, match=match:eq}}";
Joachim Bingel43a444a2014-08-07 15:06:02 +000017 expected = "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000018 cqt = new CollectionQueryTree();
19 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000020 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000021 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
22
23 query = "textClass!=Sport";
24 // String regex1 = "{@type=korap:filter, filter={@type=korap:doc, attribute=textClass, key=Sport, match=match:eq}}";
Joachim Bingel43a444a2014-08-07 15:06:02 +000025 expected = "{@type=korap:doc, key=textClass, value=Sport, match=match:ne}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000026 cqt = new CollectionQueryTree();
27 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000028 map = cqt.getRequestMap().get("collection").toString();
29 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
30 }
31
32 @Test
33 public void testContains() throws QueryException {
34 query = "title~Mannheim";
35 expected =
36 "{@type=korap:doc, key=title, value=Mannheim, match=match:contains}";
37 cqt = new CollectionQueryTree();
38 cqt.process(query);
39 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000040 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel82e4ca72014-10-27 11:03:38 +000041
42 query = "title~\"IDS Mannheim\"";
43 expected =
44 "{@type=korap:doc, key=title, value=IDS Mannheim, match=match:contains}";
45 cqt = new CollectionQueryTree();
46 cqt.process(query);
47 map = cqt.getRequestMap().get("collection").toString();
48 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingela3f51f72014-07-22 14:45:31 +000049 }
50
51 @Test
52 public void testTwoConjuncts() throws QueryException {
53 query = "textClass=Sport & year=2014";
54 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000055 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000056 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingel49da7ca2014-10-07 12:26:14 +000057 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000058 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000059 cqt = new CollectionQueryTree();
60 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000061 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000062 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
63 }
Michael Hanl2bd00c62014-11-04 16:26:42 +000064
65 //todo year type is not yet serialized!
Joachim Bingela3f51f72014-07-22 14:45:31 +000066 @Test
67 public void testThreeConjuncts() throws QueryException {
68 query = "textClass=Sport & year=2014 & corpusID=WPD";
69 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000070 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000071 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +000072 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel49da7ca2014-10-07 12:26:14 +000073 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}," +
Joachim Bingela3f51f72014-07-22 14:45:31 +000074 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
75 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000076 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000077 cqt = new CollectionQueryTree();
78 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000079 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000080 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
81 }
82
83
84 @Test
85 public void testTwoDisjuncts() throws QueryException {
86 query = "textClass=Sport | year=2014";
87 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000088 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000089 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingel49da7ca2014-10-07 12:26:14 +000090 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000091 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000092 cqt = new CollectionQueryTree();
93 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000094 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000095 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
96 }
97
98 @Test
99 public void testThreeDisjuncts() throws QueryException {
100 query = "textClass=Sport | year=2014 | corpusID=WPD";
101 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000102 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000103 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000104 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000105 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}," +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000106 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
107 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000108 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000109 cqt = new CollectionQueryTree();
110 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000111 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000112 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
113 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000114
115
Joachim Bingela3f51f72014-07-22 14:45:31 +0000116 @Test
117 public void testMixed() throws QueryException {
118 query = "(textClass=Sport | textClass=ausland) & corpusID=WPD";
119 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000120
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000121 "{@type=korap:docGroup, operation=operation:and, operands=[" +
122 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000123 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
124 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
125 "]}," +
126 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000127 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000128 cqt = new CollectionQueryTree();
129 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000130 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000131 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
132
133 query = "(textClass=Sport & textClass=ausland) & corpusID=WPD";
134 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000135
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000136 "{@type=korap:docGroup, operation=operation:and, operands=[" +
137 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000138 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
139 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
140 "]}," +
141 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000142 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000143 cqt = new CollectionQueryTree();
144 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000145 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000146 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
147
148 query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White)";
149 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000150
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000151 "{@type=korap:docGroup, operation=operation:or, operands=[" +
152 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000153 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
154 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
155 "]}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000156 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000157 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," +
158 "{@type=korap:doc, key=author, value=White, match=match:eq}" +
159 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000160 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000161 cqt = new CollectionQueryTree();
162 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000163 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000164 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel43a444a2014-08-07 15:06:02 +0000165
166 query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White & year=2010)";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000167 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000168 "{@type=korap:docGroup, operation=operation:or, operands=[" +
169 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000170 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
171 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
172 "]}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000173 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000174 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000175 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000176 "{@type=korap:doc, key=author, value=White, match=match:eq}," +
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000177 "{@type=korap:doc, key=year, type=type:date, value=2010, match=match:eq}" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000178 "]}" +
179 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000180 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000181 cqt = new CollectionQueryTree();
182 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000183 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000184 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
185 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000186
Joachim Bingela3f51f72014-07-22 14:45:31 +0000187 @Test
188 public void testDate() throws QueryException {
189 // search for pubDate between 1990 and 2010!
190 query = "1990<pubDate<2010";
191 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000192 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000193 "{@type=korap:doc, key=pubDate, type=type:date, value=1990, match=match:gt}," +
194 "{@type=korap:doc, key=pubDate, type=type:date, value=2010, match=match:lt}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000195 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000196 cqt = new CollectionQueryTree();
197 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000198 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000199 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
200
201 query = "pubDate>=1990";
202 expected =
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000203 "{@type=korap:doc, key=pubDate, type=type:date, value=1990, match=match:geq}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000204 cqt = new CollectionQueryTree();
205 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000206 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000207 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel624854b2014-07-23 13:53:28 +0000208
209 query = "pubDate>=1990-05";
210 expected =
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000211 "{@type=korap:doc, key=pubDate, type=type:date, value=1990-05, match=match:geq}";
Joachim Bingel624854b2014-07-23 13:53:28 +0000212 cqt = new CollectionQueryTree();
213 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000214 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel624854b2014-07-23 13:53:28 +0000215 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
216
217 query = "pubDate>=1990-05-01";
218 expected =
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000219 "{@type=korap:doc, key=pubDate, type=type:date, value=1990-05-01, match=match:geq}";
Joachim Bingel624854b2014-07-23 13:53:28 +0000220 cqt = new CollectionQueryTree();
221 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000222 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel624854b2014-07-23 13:53:28 +0000223 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingela3f51f72014-07-22 14:45:31 +0000224 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000225
Joachim Bingela3f51f72014-07-22 14:45:31 +0000226 @Test
227 public void testRegex() throws QueryException {
228 query = "author=/Go.*he/";
229 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000230 "{@type=korap:doc, key=author, value=Go.*he, type=type:regex, match=match:eq}";
231 cqt = new CollectionQueryTree();
232 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000233 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000234 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
235 }
236
237 @Test
238 public void testContentFilter() throws QueryException {
239 query = "[base=Schwalbe]";
240 expected =
241 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}";
242 cqt = new CollectionQueryTree();
243 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000244 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000245 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
246
247 query = "[cnx/base=Schwalbe]";
248 expected =
249 "{@type=korap:token, wrap={@type=korap:term, foundry=cnx, layer=lemma, key=Schwalbe, match=match:eq}}";
250 cqt = new CollectionQueryTree();
251 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000252 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000253 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
254
255 query = "[base!=Schwalbe]";
256 expected =
257 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:ne}}";
258 cqt = new CollectionQueryTree();
259 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000260 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000261 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
262
263 query = "[base=Schwalbe] & [orth=Foul]";
264 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000265 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000266 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}," +
267 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Foul, match=match:eq}}" +
268 "]}";
269 cqt = new CollectionQueryTree();
270 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000271 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000272 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
273 }
274
275 @Test
276 public void testContentMetaMixed() throws QueryException {
277 query = "textClass=Sport & [base=Schwalbe]";
278 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000279 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000280 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
281 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}" +
282 "]}";
283 cqt = new CollectionQueryTree();
284 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000285 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000286 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
287
288 query = "[base=Schwalbe] & textClass=Sport";
289 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000290 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000291 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}," +
292 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}" +
293 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000294 cqt = new CollectionQueryTree();
295 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000296 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000297 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
298 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000299
300}
301