1
|
package eu.dnetlib.cql.lucene;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
|
8
|
import com.google.common.collect.BiMap;
|
9
|
import com.google.common.collect.HashBiMap;
|
10
|
import com.google.common.collect.Lists;
|
11
|
import com.google.common.collect.Maps;
|
12
|
import eu.dnetlib.cql.CqlTranslator;
|
13
|
import eu.dnetlib.cql.CqlTranslatorImpl;
|
14
|
import eu.dnetlib.cql.CqlUtils;
|
15
|
import eu.dnetlib.cql.parse.*;
|
16
|
import org.junit.Before;
|
17
|
import org.junit.Test;
|
18
|
import org.z3950.zing.cql.CQLNode;
|
19
|
import org.z3950.zing.cql.CQLParseException;
|
20
|
import org.z3950.zing.cql.CQLParser;
|
21
|
|
22
|
import static org.junit.Assert.assertEquals;
|
23
|
import static org.junit.Assert.assertNotNull;
|
24
|
|
25
|
/**
|
26
|
* Created by alessia on 18/01/17.
|
27
|
*/
|
28
|
public class CqlTranslatorImplTest {
|
29
|
|
30
|
private CqlTranslator translator;
|
31
|
|
32
|
private Map<String, String> weights;
|
33
|
|
34
|
@Before
|
35
|
public void setUp() {
|
36
|
translator = new CqlTranslatorImpl();
|
37
|
|
38
|
weights = Maps.newHashMap();
|
39
|
weights.put("title", "2");
|
40
|
weights.put("ugo", "1.5");
|
41
|
weights.put("tag", "3");
|
42
|
}
|
43
|
|
44
|
@Test
|
45
|
public void testToSolr_0() throws Exception {
|
46
|
|
47
|
String cqlQuery = "\"a b x\" and r and \"eng/ita\" or f any \"pippo pluto\" and \"con: duepunti\" not (d < 5 and d > 10)";
|
48
|
String luceneQuery = translator.toLucene(cqlQuery);
|
49
|
|
50
|
printQuery(cqlQuery, luceneQuery);
|
51
|
}
|
52
|
|
53
|
@Test
|
54
|
public void testToSolr_00() throws Exception {
|
55
|
|
56
|
String cqlQuery = "resultdupid exact datacite____::b49fd4e3386d82df348a120cfe43516b OR objidentifier exact datacite____::b49fd4e3386d82df348a120cfe43516b";
|
57
|
String luceneQuery = translator.toLucene(cqlQuery);
|
58
|
|
59
|
printQuery(cqlQuery, luceneQuery);
|
60
|
}
|
61
|
|
62
|
@Test
|
63
|
public void testToSolr_1() throws Exception {
|
64
|
|
65
|
String cqlQuery = "publicationdate =/within \"2000-01-01 2010-01-01\"";
|
66
|
String luceneQuery = translator.toLucene(cqlQuery);
|
67
|
|
68
|
printQuery(cqlQuery, luceneQuery);
|
69
|
}
|
70
|
|
71
|
@Test
|
72
|
public void testToSolr_2() throws Exception {
|
73
|
String query = "(_all=faust AND _all=pippo) AND _all<>cinegiornale";
|
74
|
Node node = new AndNode(new AndNode(new TermNode("_all", Relation.EQUAL, "faust"), new TermNode("_all", Relation.EQUAL, "pippo")), new TermNode("_all",
|
75
|
Relation.NOT, "cinegiornale"));
|
76
|
|
77
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
78
|
|
79
|
printQuery(query, parsed.asLucene());
|
80
|
System.out.println("NODE: " + node.toLucene());
|
81
|
System.out.println("NODE: " + node.toString());
|
82
|
assertEquals(node.toLucene(), parsed.asLucene());
|
83
|
}
|
84
|
|
85
|
@Test
|
86
|
public void testToSolr_3() throws Exception {
|
87
|
String query = "_all all faust";
|
88
|
Node node = new TermNode("_all", Relation.ALL, "faust");
|
89
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
90
|
printQuery(query, parsed.asLucene());
|
91
|
assertEquals(node.toLucene(), parsed.asLucene());
|
92
|
|
93
|
query = "__all all \"caracas roma\"";
|
94
|
|
95
|
parsed = translator.getTranslatedQuery(query);
|
96
|
printQuery(query, parsed.asLucene());
|
97
|
|
98
|
query = "__all all caracas roma";
|
99
|
parsed = translator.getTranslatedQuery(query);
|
100
|
printQuery(query, parsed.asLucene());
|
101
|
|
102
|
query = "__all any caracas roma";
|
103
|
parsed = translator.getTranslatedQuery(query);
|
104
|
printQuery(query, parsed.asLucene());
|
105
|
|
106
|
query = "__all any \"caracas roma\"";
|
107
|
parsed = translator.getTranslatedQuery(query);
|
108
|
printQuery(query, parsed.asLucene());
|
109
|
|
110
|
query = "__all exact caracas roma";
|
111
|
parsed = translator.getTranslatedQuery(query);
|
112
|
printQuery(query, parsed.asLucene());
|
113
|
}
|
114
|
|
115
|
@Test
|
116
|
public void testToSolr_4() throws Exception {
|
117
|
|
118
|
String cqlQuery = "publicationdate =/within \"2000-01-01 2010-01-01\" and title = \"ddd\" and y < 2010 or y <= 2010 or y > 2010 or y >= 2010";
|
119
|
String luceneQuery = translator.toLucene(cqlQuery);
|
120
|
|
121
|
printQuery(cqlQuery, luceneQuery);
|
122
|
}
|
123
|
|
124
|
@Test
|
125
|
public void testToSolr_5() throws Exception {
|
126
|
|
127
|
String cqlQuery = "publicationdate within \"2000-01-01 2010-01-01\"";
|
128
|
String luceneQuery = translator.toLucene(cqlQuery);
|
129
|
|
130
|
printQuery(cqlQuery, luceneQuery);
|
131
|
}
|
132
|
|
133
|
@Test
|
134
|
public void testToSolr_6() throws Exception {
|
135
|
|
136
|
String cqlQuery = "cat sortBy title/sort.descending";
|
137
|
final TranslatedQuery tr = translator.getTranslatedQuery(cqlQuery);
|
138
|
printQuery(cqlQuery, tr.asLucene());
|
139
|
System.out.println(tr.getOptions());
|
140
|
|
141
|
}
|
142
|
|
143
|
@Test
|
144
|
public void testToSolr_7() throws Exception {
|
145
|
|
146
|
String cqlQuery = "title exact true sortBy title/sort.ascending";
|
147
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
148
|
|
149
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
150
|
|
151
|
assertNotNull(luceneQuery.getOptions().getSort().getField());
|
152
|
assertNotNull(luceneQuery.getOptions().getSort().getMode());
|
153
|
|
154
|
assertEquals("title", luceneQuery.getOptions().getSort().getField());
|
155
|
assertEquals(SortOperation.Mode.asc, luceneQuery.getOptions().getSort().getMode());
|
156
|
|
157
|
System.out.println("LUCENE: " + luceneQuery.asLucene() + " OPTIONS:" + luceneQuery.getOptions().getSort().getField() + " "
|
158
|
+ luceneQuery.getOptions().getSort().getMode());
|
159
|
|
160
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
161
|
}
|
162
|
|
163
|
@Test
|
164
|
public void testToSolr_8() throws Exception {
|
165
|
|
166
|
String cqlQuery = "__all all \"caracas - roma\"";
|
167
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
168
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
169
|
|
170
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
171
|
}
|
172
|
|
173
|
@Test
|
174
|
public void testToSolr_9() throws Exception {
|
175
|
|
176
|
String cqlQuery = "__all all \"caracas roma\"";
|
177
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
178
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
179
|
|
180
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
181
|
|
182
|
cqlQuery = "__all all caracas roma";
|
183
|
parsed = new CQLParser().parse(cqlQuery);
|
184
|
luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
185
|
|
186
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
187
|
|
188
|
cqlQuery = "__all any caracas roma";
|
189
|
parsed = new CQLParser().parse(cqlQuery);
|
190
|
luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
191
|
|
192
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
193
|
|
194
|
cqlQuery = "__all any \"caracas roma\"";
|
195
|
parsed = new CQLParser().parse(cqlQuery);
|
196
|
luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
197
|
|
198
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
199
|
|
200
|
cqlQuery = "__all exact caracas roma";
|
201
|
parsed = new CQLParser().parse(cqlQuery);
|
202
|
luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
203
|
|
204
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
205
|
}
|
206
|
|
207
|
@Test
|
208
|
public void testToSolr_10() throws Exception {
|
209
|
|
210
|
String cqlQuery = "__all all \"caracas - ro*\"";
|
211
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
212
|
|
213
|
Map<String, List<String>> cqlOptions = Maps.newHashMap();
|
214
|
BiMap<String, String> aliases = HashBiMap.create();
|
215
|
|
216
|
cqlOptions.put("wildcard", Lists.newArrayList("true"));
|
217
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap(), cqlOptions, aliases,
|
218
|
new HashMap<String, String>());
|
219
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
220
|
|
221
|
cqlOptions = Maps.newHashMap();
|
222
|
luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap(), cqlOptions, aliases, new HashMap<String, String>());
|
223
|
|
224
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
225
|
}
|
226
|
|
227
|
@Test
|
228
|
public void testToSolr_11() throws Exception {
|
229
|
|
230
|
String cqlQuery = "__all <> \"kreutz\" and __all <> \"austria\"";
|
231
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
232
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
233
|
|
234
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
235
|
}
|
236
|
|
237
|
@Test
|
238
|
public void testToSolr_12() throws Exception {
|
239
|
|
240
|
String cqlQuery = "__all <> \"kreutz\" and __all <> \"austria\" or __all <> \"italia\" and __dsid exact \"wwwww\"";
|
241
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
242
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
243
|
|
244
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
245
|
}
|
246
|
|
247
|
@Test
|
248
|
public void testToSolr_13() throws Exception {
|
249
|
|
250
|
String cqlQuery = "__all = faust and __all <> cinegiornale";
|
251
|
|
252
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
253
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
254
|
|
255
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
256
|
}
|
257
|
|
258
|
@Test
|
259
|
public void testToSolr_14() throws Exception {
|
260
|
|
261
|
String cqlQuery = "__all <> cinegiornale and __all = faust";
|
262
|
|
263
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
264
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
265
|
|
266
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
267
|
}
|
268
|
|
269
|
@Test
|
270
|
public void testToSolr_15() throws Exception {
|
271
|
|
272
|
String cqlQuery = "(asdasd or asfgqwr) and textual";
|
273
|
|
274
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
275
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(parsed, new IdentityCqlValueTransformerMap());
|
276
|
|
277
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
278
|
}
|
279
|
|
280
|
@Test
|
281
|
public void testToSolr_16() throws Exception {
|
282
|
|
283
|
String cqlQuery = "title = kreutz and subject any \"austria italy\" or tag = pippo and blabla";
|
284
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
285
|
|
286
|
Map<String, List<String>> options = Maps.newHashMap();
|
287
|
BiMap<String, String> aliases = HashBiMap.create();
|
288
|
IdentityCqlValueTransformerMap map = new IdentityCqlValueTransformerMap();
|
289
|
|
290
|
CQLNode expand = CqlUtils.expand(parsed, weights.keySet());
|
291
|
|
292
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(expand, map, options, aliases, weights);
|
293
|
|
294
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
295
|
}
|
296
|
|
297
|
@Test
|
298
|
public void testToSolr_17() throws Exception {
|
299
|
|
300
|
String cqlQuery = "a = 1 and b = 2 or c = 3 and blabla";
|
301
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
302
|
|
303
|
Map<String, List<String>> options = Maps.newHashMap();
|
304
|
BiMap<String, String> aliases = HashBiMap.create();
|
305
|
IdentityCqlValueTransformerMap map = new IdentityCqlValueTransformerMap();
|
306
|
|
307
|
Map<String, String> w = Maps.newHashMap();
|
308
|
w.put("a", "2");
|
309
|
w.put("c", "1.5");
|
310
|
|
311
|
CQLNode expand = CqlUtils.expand(parsed, w.keySet());
|
312
|
|
313
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(expand, map, options, aliases, w);
|
314
|
|
315
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
316
|
}
|
317
|
|
318
|
@Test
|
319
|
public void testToSolr_18() throws Exception {
|
320
|
|
321
|
String cqlQuery = "a exact 1";
|
322
|
CQLNode parsed = new CQLParser().parse(cqlQuery);
|
323
|
|
324
|
Map<String, List<String>> options = Maps.newHashMap();
|
325
|
BiMap<String, String> aliases = HashBiMap.create();
|
326
|
aliases.put("a", "b");
|
327
|
|
328
|
IdentityCqlValueTransformerMap map = new IdentityCqlValueTransformerMap();
|
329
|
|
330
|
Map<String, String> w = Maps.newHashMap();
|
331
|
|
332
|
CQLNode expand = CqlUtils.expand(parsed, w.keySet());
|
333
|
|
334
|
TranslatedQuery luceneQuery = translator.getTranslatedQuery(expand, map, options, aliases, w);
|
335
|
|
336
|
printQuery(cqlQuery, luceneQuery.asLucene());
|
337
|
}
|
338
|
|
339
|
@Test
|
340
|
public void testToSolr_19() throws Exception {
|
341
|
String query = "_all<>faust";
|
342
|
Node node = new TermNode("_all", Relation.NOT, "faust");
|
343
|
|
344
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
345
|
|
346
|
printQuery(query, parsed.asLucene());
|
347
|
assertEquals(node.toLucene(), parsed.asLucene());
|
348
|
|
349
|
query = "_all <> faust nozze";
|
350
|
// node = new TermNode("_all", Relation.NOT, "faust nozze");
|
351
|
parsed = translator.getTranslatedQuery(query);
|
352
|
printQuery(query, parsed.asLucene());
|
353
|
|
354
|
}
|
355
|
|
356
|
@Test
|
357
|
public void testToSolr_20() throws Exception {
|
358
|
|
359
|
String query = "(title = ESTUDIO and title = abierto) not (title = mediante)";
|
360
|
|
361
|
Node node = new NotNode(new AndNode(new TermNode("title", Relation.EQUAL, "ESTUDIO"), new TermNode("title", Relation.EQUAL, "abierto")), new TermNode(
|
362
|
"title", Relation.EQUAL, "mediante"));
|
363
|
|
364
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
365
|
|
366
|
printQuery(query, parsed.asLucene());
|
367
|
assertEquals(node.toLucene(), parsed.asLucene());
|
368
|
}
|
369
|
|
370
|
@Test
|
371
|
public void testToSolr_21() throws Exception {
|
372
|
|
373
|
String query = "(title = ESTUDIO and title = abierto) not (title = mediante or title = verde)";
|
374
|
|
375
|
Node node = new NotNode(new AndNode(new TermNode("title", Relation.EQUAL, "ESTUDIO"), new TermNode("title", Relation.EQUAL, "abierto")), new OrNode(
|
376
|
new TermNode("title", Relation.EQUAL, "mediante"), new TermNode("title", Relation.EQUAL, "verde")));
|
377
|
|
378
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
379
|
|
380
|
printQuery(query, parsed.asLucene());
|
381
|
assertEquals(node.toLucene(), parsed.asLucene());
|
382
|
}
|
383
|
|
384
|
@Test
|
385
|
public void testToSolr_22() throws Exception {
|
386
|
String query = " and itemtype = *";
|
387
|
// Node node = new TermNode("itemtype", Relation.EQUAL, "*");
|
388
|
|
389
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
390
|
|
391
|
printQuery(query, parsed.asLucene());
|
392
|
|
393
|
query = " AND itemtype = *";
|
394
|
parsed = translator.getTranslatedQuery(query);
|
395
|
printQuery(query, parsed.asLucene());
|
396
|
|
397
|
Map<String, List<String>> cqlOptions = Maps.newHashMap();
|
398
|
BiMap<String, String> aliases = HashBiMap.create();
|
399
|
|
400
|
cqlOptions.put("wildcard", Lists.newArrayList("true"));
|
401
|
|
402
|
query = "AND itemtype = vid* borat";
|
403
|
parsed = translator.getTranslatedQuery(query, cqlOptions);
|
404
|
printQuery(query, parsed.asLucene());
|
405
|
|
406
|
// + - && || ! ( ) { } [ ] ^ " ~ * ? : \
|
407
|
query = " AND f = * vid* bo?ra bo+ra bo-ra bo&ra bo!ra bo]ra bo^ra bo*ra";// bo|ra bo(ra bo)ra bo{ra bo}ra
|
408
|
// bo\"ra bo~ra bo:ra bo\\ra";
|
409
|
|
410
|
CQLNode cqlNode = new CQLParser().parse(query);
|
411
|
parsed = translator.getTranslatedQuery(cqlNode, new IdentityCqlValueTransformerMap(), cqlOptions, aliases, new HashMap<String, String>());
|
412
|
printQuery(query, parsed.asLucene());
|
413
|
|
414
|
parsed = translator.getTranslatedQuery(query, cqlOptions);
|
415
|
printQuery(query, parsed.asLucene());
|
416
|
|
417
|
parsed = translator.getTranslatedQuery(query);
|
418
|
printQuery(query, parsed.asLucene());
|
419
|
}
|
420
|
|
421
|
@Test
|
422
|
public void testToSolr_23() throws Exception {
|
423
|
|
424
|
String query = "_all = cat sortBy title/sort.descending";
|
425
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
426
|
|
427
|
Node node = new TermNode("_all", Relation.EQUAL, "cat");
|
428
|
|
429
|
printQuery(query, parsed.asLucene());
|
430
|
assertEquals(node.toLucene(), parsed.asLucene());
|
431
|
|
432
|
assertNotNull(parsed.getOptions().getSort().getField());
|
433
|
assertNotNull(parsed.getOptions().getSort().getMode());
|
434
|
|
435
|
assertEquals("title", parsed.getOptions().getSort().getField());
|
436
|
assertEquals(SortOperation.Mode.desc, parsed.getOptions().getSort().getMode());
|
437
|
}
|
438
|
|
439
|
@Test
|
440
|
public void testToSolr_24() throws Exception {
|
441
|
|
442
|
String query = "invalid exact true sortBy title/sort.ascending";
|
443
|
|
444
|
// Node node = new TermNode("invalid", Relation.EXACT, "true");
|
445
|
|
446
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
447
|
|
448
|
printQuery(query, parsed.asLucene());
|
449
|
|
450
|
assertNotNull(parsed.getOptions().getSort().getField());
|
451
|
assertNotNull(parsed.getOptions().getSort().getMode());
|
452
|
|
453
|
assertEquals("title", parsed.getOptions().getSort().getField());
|
454
|
assertEquals(SortOperation.Mode.asc, parsed.getOptions().getSort().getMode());
|
455
|
}
|
456
|
|
457
|
@Test
|
458
|
public void testToSolr_25() throws Exception {
|
459
|
|
460
|
// String query = "deleted all true and __dsid all xxxxxxxxxxx";
|
461
|
|
462
|
// ciao (+a +b +c)
|
463
|
String query = "ciao or (_all all \"a b c\")";
|
464
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
465
|
printQuery(query, parsed.asLucene());
|
466
|
|
467
|
query = "(__all = ciao) or (__all all \"a b c\")";
|
468
|
parsed = translator.getTranslatedQuery(query);
|
469
|
printQuery(query, parsed.asLucene());
|
470
|
|
471
|
query = "a and b";
|
472
|
parsed = translator.getTranslatedQuery(query);
|
473
|
printQuery(query, parsed.asLucene());
|
474
|
query = "field=a and field=b";
|
475
|
parsed = translator.getTranslatedQuery(query);
|
476
|
printQuery(query, parsed.asLucene());
|
477
|
|
478
|
query = "a or b";
|
479
|
parsed = translator.getTranslatedQuery(query);
|
480
|
printQuery(query, parsed.asLucene());
|
481
|
|
482
|
query = "field=a or field=b";
|
483
|
parsed = translator.getTranslatedQuery(query);
|
484
|
printQuery(query, parsed.asLucene());
|
485
|
|
486
|
query = "field all a or field all b";
|
487
|
parsed = translator.getTranslatedQuery(query);
|
488
|
printQuery(query, parsed.asLucene());
|
489
|
}
|
490
|
|
491
|
@Test
|
492
|
public void testToSolr_26() throws Exception {
|
493
|
|
494
|
String query = "publicationdate =/within \"2000-01-01 2010-01-01\"";
|
495
|
Node node = new TermNode("publicationdate", Relation.WITHIN, "2000-01-01 2010-01-01");
|
496
|
|
497
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
498
|
|
499
|
printQuery(query, parsed.asLucene());
|
500
|
assertEquals(node.toLucene(), parsed.asLucene());
|
501
|
}
|
502
|
|
503
|
@Test
|
504
|
public void testToSolr_27() throws Exception {
|
505
|
|
506
|
String query = "((((publicationdate =/within \"2000-01-01 2010-01-01\" and title = \"ddd\") and y < 2010) or y <= 2010) or y > 2010) or y >= 2010";
|
507
|
Node node = new OrNode(new OrNode(new OrNode(new AndNode(new AndNode(new TermNode("publicationdate", Relation.WITHIN, "2000-01-01 2010-01-01"),
|
508
|
new TermNode("title", Relation.EQUAL, "ddd")), new TermNode("y", Relation.LT, "2010")), new TermNode("y", Relation.LTE, "2010")), new TermNode(
|
509
|
"y", Relation.GT, "2010")), new TermNode("y", Relation.GTE, "2010"));
|
510
|
|
511
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
512
|
|
513
|
printQuery(query, parsed.asLucene());
|
514
|
assertEquals(node.toLucene(), parsed.asLucene());
|
515
|
}
|
516
|
|
517
|
@Test
|
518
|
public void testToSolr_28() throws Exception {
|
519
|
|
520
|
String query = "publicationdate =/within \"2000-01-01 2010-01-01\" and (title = \"ddd\" and (y < 2010 or (y <= 2010 or (y > 2010 or y >= 2010))))";
|
521
|
|
522
|
Node node = new AndNode(new TermNode("publicationdate", Relation.WITHIN, "2000-01-01 2010-01-01"), new AndNode(new TermNode("title", Relation.EQUAL,
|
523
|
"ddd"), new OrNode(new TermNode("y", Relation.LT, "2010"), new OrNode(new TermNode("y", Relation.LTE, "2010"), new OrNode(new TermNode("y",
|
524
|
Relation.GT, "2010"), new TermNode("y", Relation.GTE, "2010"))))));
|
525
|
|
526
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
527
|
|
528
|
printQuery(query, parsed.asLucene());
|
529
|
assertEquals(node.toLucene(), parsed.asLucene());
|
530
|
}
|
531
|
|
532
|
@Test
|
533
|
public void testToSolr_29() throws Exception {
|
534
|
|
535
|
String query = "dateaccepted =/within \"1900-01-01 2000-01-01\" and dateaccepted >= 2010-01-01";
|
536
|
|
537
|
Node node = new AndNode(new TermNode("dateaccepted", Relation.WITHIN, "1900-01-01 2000-01-01"),
|
538
|
new TermNode("dateaccepted", Relation.GTE, "2010-01-01"));
|
539
|
|
540
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
541
|
|
542
|
printQuery(query, parsed.asLucene());
|
543
|
assertEquals(node.toLucene(), parsed.asLucene());
|
544
|
|
545
|
query = "dateaccepted =/within \"1900-01-01 2000-01-01\" and dateaccepted > 2010-01-01";
|
546
|
|
547
|
node = new AndNode(new TermNode("dateaccepted", Relation.WITHIN, "1900-01-01 2000-01-01"), new TermNode("dateaccepted", Relation.GT, "2010-01-01"));
|
548
|
|
549
|
parsed = translator.getTranslatedQuery(query);
|
550
|
|
551
|
printQuery(query, parsed.asLucene());
|
552
|
assertEquals(node.toLucene(), parsed.asLucene());
|
553
|
}
|
554
|
|
555
|
@Test
|
556
|
public void testToSolr_30() throws Exception {
|
557
|
|
558
|
String query = "a = 1 and b = 2 and c = 3";
|
559
|
Node node = new AndNode(new AndNode(new TermNode("a", Relation.EQUAL, "1"), new TermNode("b", Relation.EQUAL, "2")), new TermNode("c", Relation.EQUAL,
|
560
|
"3"));
|
561
|
|
562
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
563
|
|
564
|
printQuery(query, parsed.asLucene());
|
565
|
assertEquals(node.toLucene(), parsed.asLucene());
|
566
|
}
|
567
|
|
568
|
@Test
|
569
|
public void testToSolr_31() throws Exception {
|
570
|
|
571
|
String query = "a = \"pippo pluto\" and b = 2 and c = 3";
|
572
|
Node node = new AndNode(new AndNode(new TermNode("a", Relation.EQUAL, "pippo pluto"), new TermNode("b", Relation.EQUAL, "2")), new TermNode("c",
|
573
|
Relation.EQUAL, "3"));
|
574
|
|
575
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
576
|
|
577
|
printQuery(query, parsed.asLucene());
|
578
|
assertEquals(node.toLucene(), parsed.asLucene());
|
579
|
}
|
580
|
|
581
|
@Test
|
582
|
public void testToSolr_32() throws Exception {
|
583
|
|
584
|
String query = "__all all \"caracas - ro*\"";
|
585
|
|
586
|
TranslatedQuery parsed = translator.getTranslatedQuery(query);
|
587
|
printQuery(query, parsed.asLucene());
|
588
|
|
589
|
Map<String, List<String>> cqlOptions = Maps.newHashMap();
|
590
|
cqlOptions.put("wildcard", Lists.newArrayList("true"));
|
591
|
|
592
|
parsed = translator.getTranslatedQuery(query, cqlOptions);
|
593
|
printQuery(query, parsed.asLucene());
|
594
|
|
595
|
}
|
596
|
|
597
|
@Test
|
598
|
public void testToSolr_33() throws Exception {
|
599
|
String query1 = "query=(deletedbyinference = false) AND (((oaftype exact project) and (test)) and (fundinglevel0_id exact corda_______::FP7))";
|
600
|
printQuery(query1, translator.getTranslatedQuery(query1).asLucene());
|
601
|
|
602
|
String query2 = "query=(((deletedbyinference = false) AND (oaftype exact project)) and (test)) and (fundinglevel0_id exact corda_______::FP7)";
|
603
|
printQuery(query2, translator.getTranslatedQuery(query2).asLucene());
|
604
|
|
605
|
String query3 = "(deletedbyinference = false) AND (((oaftype exact project) and (test)) and (fundinglevel0_id exact corda_______::FP7))";
|
606
|
printQuery(query3, translator.getTranslatedQuery(query1).asLucene());
|
607
|
|
608
|
String query4 = "(((deletedbyinference = false) AND (oaftype exact project)) and (test)) and (fundinglevel0_id exact corda_______::FP7)";
|
609
|
printQuery(query4, translator.getTranslatedQuery(query2).asLucene());
|
610
|
}
|
611
|
|
612
|
@Test
|
613
|
public void testWildcard() throws CQLParseException, IOException {
|
614
|
|
615
|
Map<String, List<String>> options = new HashMap<String, List<String>>();
|
616
|
|
617
|
String cqlQuery = "__all = \"test?\"";
|
618
|
String lucene = translator.toLucene(cqlQuery, options);
|
619
|
printQuery(cqlQuery, lucene);
|
620
|
|
621
|
options.put("wildcard", Lists.newArrayList("true"));
|
622
|
|
623
|
cqlQuery = "__all = \"test?\"";
|
624
|
lucene = translator.toLucene(cqlQuery, options);
|
625
|
printQuery(cqlQuery, lucene);
|
626
|
}
|
627
|
|
628
|
@Test
|
629
|
public void testWildcard_2() throws CQLParseException, IOException {
|
630
|
|
631
|
Map<String, List<String>> options = new HashMap<String, List<String>>();
|
632
|
|
633
|
String cqlQuery = "thumbnail=localhost*";
|
634
|
String lucene = translator.toLucene(cqlQuery, options);
|
635
|
printQuery(cqlQuery, lucene);
|
636
|
|
637
|
options.put("wildcard", Lists.newArrayList("true"));
|
638
|
|
639
|
lucene = translator.toLucene(cqlQuery, options);
|
640
|
printQuery(cqlQuery, lucene);
|
641
|
}
|
642
|
|
643
|
@Test
|
644
|
public void testDateQuery() throws CQLParseException, IOException {
|
645
|
String cqlQuery = "(resultdateofacceptance <= \"2012-03-15\")";
|
646
|
Map<String, List<String>> options = new HashMap<String, List<String>>();
|
647
|
String lucene = translator.toLucene(cqlQuery, options);
|
648
|
printQuery(cqlQuery, lucene);
|
649
|
}
|
650
|
|
651
|
@Test
|
652
|
public void testFullISODateQuery() throws CQLParseException, IOException {
|
653
|
String cqlQuery = "(resultdateofacceptance <= 2012-03-15T00:00:00Z)";
|
654
|
Map<String, List<String>> options = new HashMap<String, List<String>>();
|
655
|
String lucene = translator.toLucene(cqlQuery, options);
|
656
|
printQuery(cqlQuery, lucene);
|
657
|
}
|
658
|
|
659
|
@Test
|
660
|
public void testNonDateQuery() throws CQLParseException, IOException {
|
661
|
String cqlQuery = "(resultdateofacceptance <= 2012.03.15T00:00:00Z)";
|
662
|
Map<String, List<String>> options = new HashMap<String, List<String>>();
|
663
|
String lucene = translator.toLucene(cqlQuery, options);
|
664
|
printQuery(cqlQuery, lucene);
|
665
|
}
|
666
|
|
667
|
@Test
|
668
|
public void testNonDateQuery2() throws CQLParseException, IOException {
|
669
|
String cqlQuery = "(resultdateofacceptance <= ciao)";
|
670
|
Map<String, List<String>> options = new HashMap<String, List<String>>();
|
671
|
String lucene = translator.toLucene(cqlQuery, options);
|
672
|
printQuery(cqlQuery, lucene);
|
673
|
}
|
674
|
|
675
|
@Test
|
676
|
public void testDateWrong() throws Exception {
|
677
|
|
678
|
String cqlQuery = "publicationdate =/within \"2000-01-01 2010.99.01\"";
|
679
|
String luceneQuery = translator.toLucene(cqlQuery);
|
680
|
|
681
|
printQuery(cqlQuery, luceneQuery);
|
682
|
}
|
683
|
|
684
|
@Test
|
685
|
public void testToDate() throws Exception {
|
686
|
|
687
|
String cqlQuery = "publicationdate =/within \"* 2000-01-01\"";
|
688
|
String luceneQuery = translator.toLucene(cqlQuery);
|
689
|
|
690
|
printQuery(cqlQuery, luceneQuery);
|
691
|
}
|
692
|
|
693
|
private void printQuery(final String cql, final String lucene) throws CQLParseException, IOException {
|
694
|
System.out.println("CQL: " + cql);
|
695
|
// System.out.println("PARSED: " + new CQLParser().parse(cql).toCQL());
|
696
|
System.out.println("LUCENE: " + lucene + "\n");
|
697
|
|
698
|
}
|
699
|
}
|