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