blob: 1dd488b1cb11ed87c776f776fd968ad021011456 [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 }
64
65 @Test
66 public void testThreeConjuncts() throws QueryException {
67 query = "textClass=Sport & year=2014 & corpusID=WPD";
68 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000069 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000070 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +000071 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel49da7ca2014-10-07 12:26:14 +000072 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}," +
Joachim Bingela3f51f72014-07-22 14:45:31 +000073 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
74 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000075 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000076 cqt = new CollectionQueryTree();
77 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000078 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000079 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
80 }
81
82
83 @Test
84 public void testTwoDisjuncts() throws QueryException {
85 query = "textClass=Sport | year=2014";
86 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000087 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000088 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingel49da7ca2014-10-07 12:26:14 +000089 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000090 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000091 cqt = new CollectionQueryTree();
92 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000093 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000094 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
95 }
96
97 @Test
98 public void testThreeDisjuncts() throws QueryException {
99 query = "textClass=Sport | year=2014 | corpusID=WPD";
100 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000101 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000102 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000103 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000104 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}," +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000105 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
106 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000107 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000108 cqt = new CollectionQueryTree();
109 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000110 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000111 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
112 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000113
114
Joachim Bingela3f51f72014-07-22 14:45:31 +0000115 @Test
116 public void testMixed() throws QueryException {
117 query = "(textClass=Sport | textClass=ausland) & corpusID=WPD";
118 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000119
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000120 "{@type=korap:docGroup, operation=operation:and, operands=[" +
121 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000122 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
123 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
124 "]}," +
125 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000126 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000127 cqt = new CollectionQueryTree();
128 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000129 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000130 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
131
132 query = "(textClass=Sport & textClass=ausland) & corpusID=WPD";
133 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000134
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000135 "{@type=korap:docGroup, operation=operation:and, operands=[" +
136 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000137 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
138 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
139 "]}," +
140 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000141 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000142 cqt = new CollectionQueryTree();
143 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000144 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000145 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
146
147 query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White)";
148 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000149
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000150 "{@type=korap:docGroup, operation=operation:or, operands=[" +
151 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000152 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
153 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
154 "]}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000155 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000156 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," +
157 "{@type=korap:doc, key=author, value=White, match=match:eq}" +
158 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000159 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000160 cqt = new CollectionQueryTree();
161 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000162 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000163 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel43a444a2014-08-07 15:06:02 +0000164
165 query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White & year=2010)";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000166 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000167 "{@type=korap:docGroup, operation=operation:or, operands=[" +
168 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000169 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
170 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
171 "]}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000172 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000173 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000174 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000175 "{@type=korap:doc, key=author, value=White, match=match:eq}," +
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000176 "{@type=korap:doc, key=year, type=type:date, value=2010, match=match:eq}" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000177 "]}" +
178 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000179 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000180 cqt = new CollectionQueryTree();
181 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000182 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000183 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
184 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000185
Joachim Bingela3f51f72014-07-22 14:45:31 +0000186 @Test
187 public void testDate() throws QueryException {
188 // search for pubDate between 1990 and 2010!
189 query = "1990<pubDate<2010";
190 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000191 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000192 "{@type=korap:doc, key=pubDate, type=type:date, value=1990, match=match:gt}," +
193 "{@type=korap:doc, key=pubDate, type=type:date, value=2010, match=match:lt}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000194 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000195 cqt = new CollectionQueryTree();
196 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000197 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000198 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
199
200 query = "pubDate>=1990";
201 expected =
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000202 "{@type=korap:doc, key=pubDate, type=type:date, value=1990, match=match:geq}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000203 cqt = new CollectionQueryTree();
204 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000205 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000206 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel624854b2014-07-23 13:53:28 +0000207
208 query = "pubDate>=1990-05";
209 expected =
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000210 "{@type=korap:doc, key=pubDate, type=type:date, value=1990-05, match=match:geq}";
Joachim Bingel624854b2014-07-23 13:53:28 +0000211 cqt = new CollectionQueryTree();
212 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000213 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel624854b2014-07-23 13:53:28 +0000214 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
215
216 query = "pubDate>=1990-05-01";
217 expected =
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000218 "{@type=korap:doc, key=pubDate, type=type:date, value=1990-05-01, match=match:geq}";
Joachim Bingel624854b2014-07-23 13:53:28 +0000219 cqt = new CollectionQueryTree();
220 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000221 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel624854b2014-07-23 13:53:28 +0000222 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingela3f51f72014-07-22 14:45:31 +0000223 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000224
Joachim Bingela3f51f72014-07-22 14:45:31 +0000225 @Test
226 public void testRegex() throws QueryException {
227 query = "author=/Go.*he/";
228 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000229 "{@type=korap:doc, key=author, value=Go.*he, type=type:regex, match=match:eq}";
230 cqt = new CollectionQueryTree();
231 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000232 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000233 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
234 }
235
236 @Test
237 public void testContentFilter() throws QueryException {
238 query = "[base=Schwalbe]";
239 expected =
240 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}";
241 cqt = new CollectionQueryTree();
242 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000243 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000244 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
245
246 query = "[cnx/base=Schwalbe]";
247 expected =
248 "{@type=korap:token, wrap={@type=korap:term, foundry=cnx, layer=lemma, key=Schwalbe, match=match:eq}}";
249 cqt = new CollectionQueryTree();
250 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000251 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000252 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
253
254 query = "[base!=Schwalbe]";
255 expected =
256 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:ne}}";
257 cqt = new CollectionQueryTree();
258 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000259 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000260 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
261
262 query = "[base=Schwalbe] & [orth=Foul]";
263 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000264 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000265 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}," +
266 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Foul, match=match:eq}}" +
267 "]}";
268 cqt = new CollectionQueryTree();
269 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000270 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000271 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
272 }
273
274 @Test
275 public void testContentMetaMixed() throws QueryException {
276 query = "textClass=Sport & [base=Schwalbe]";
277 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000278 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000279 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
280 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}" +
281 "]}";
282 cqt = new CollectionQueryTree();
283 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000284 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000285 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
286
287 query = "[base=Schwalbe] & textClass=Sport";
288 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000289 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000290 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}," +
291 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}" +
292 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000293 cqt = new CollectionQueryTree();
294 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000295 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000296 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
297 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000298
299}
300