001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.test.querybuilder;
018:
019: import java.util.Calendar;
020:
021: import org.compass.core.CompassException;
022: import org.compass.core.CompassHits;
023: import org.compass.core.CompassQueryBuilder;
024: import org.compass.core.CompassSession;
025: import org.compass.core.CompassTransaction;
026: import org.compass.core.config.CompassEnvironment;
027: import org.compass.core.config.CompassSettings;
028: import org.compass.core.test.AbstractTestCase;
029:
030: /**
031: * @author kimchy
032: */
033: public class QueryBuilderTests extends AbstractTestCase {
034:
035: protected String[] getMappings() {
036: return new String[] { "querybuilder/querybuilder.cpm.xml" };
037: }
038:
039: protected void addSettings(CompassSettings settings) {
040: settings
041: .setGroupSettings(
042: CompassEnvironment.Converter.PREFIX,
043: CompassEnvironment.Converter.DefaultTypes.Simple.DATE,
044: new String[] { CompassEnvironment.Converter.Format.FORMAT },
045: new String[] { "yyyy-MM-dd-HH" });
046: }
047:
048: private void setUpData(CompassSession session) {
049: Calendar calendar = Calendar.getInstance();
050: A a = new A();
051: a.setId(new Long(1));
052: a.setValue1("0001");
053: a.setValue2("test1");
054: calendar.set(2000, 1, 1);
055: a.setDate(calendar.getTime());
056: session.save("a", a);
057: a.setId(new Long(2));
058: a.setValue1("0002");
059: a.setValue2("test2");
060: calendar.set(2000, 1, 2);
061: a.setDate(calendar.getTime());
062: session.save("a", a);
063: a.setId(new Long(3));
064: a.setValue1("0003");
065: a.setValue2("test3");
066: calendar.set(2000, 1, 3);
067: a.setDate(calendar.getTime());
068: session.save("a", a);
069: a.setId(new Long(4));
070: a.setValue1("0004");
071: a.setValue2("the quick brown fox jumped over the lazy dog");
072: calendar.set(2000, 1, 4);
073: a.setDate(calendar.getTime());
074: session.save("a", a);
075: }
076:
077: public void testCustomFormatForDate() {
078: CompassSession session = openSession();
079: CompassTransaction tr = session.beginTransaction();
080:
081: Calendar calendar = Calendar.getInstance();
082: calendar.set(2000, 1, 1);
083: A a = new A();
084: a.setId(new Long(1));
085: a.setValue1("0001");
086: a.setValue2("test1");
087: a.setDate(calendar.getTime());
088: session.save("a1", a);
089:
090: CompassQueryBuilder queryBuilder = session.queryBuilder();
091: CompassHits hits = queryBuilder.term("a1.date",
092: calendar.getTime()).hits();
093: assertEquals(1, hits.length());
094: hits = queryBuilder.term("a1.date.date", calendar.getTime())
095: .hits();
096: assertEquals(1, hits.length());
097: calendar.set(2001, 1, 1);
098: hits = queryBuilder.term("a1.date", calendar.getTime()).hits();
099: assertEquals(0, hits.length());
100:
101: tr.commit();
102: session.close();
103: }
104:
105: public void testMultiPropertyQueryString() {
106: CompassSession session = openSession();
107: CompassTransaction tr = session.beginTransaction();
108:
109: setUpData(session);
110:
111: CompassQueryBuilder queryBuilder = session.queryBuilder();
112: CompassHits hits = queryBuilder.multiPropertyQueryString(
113: "test1 OR 0002").add("mvalue1").add("mvalue2")
114: .toQuery().hits();
115: assertEquals(2, hits.length());
116:
117: tr.commit();
118: session.close();
119: }
120:
121: public void testEqAlias() {
122: CompassSession session = openSession();
123: CompassTransaction tr = session.beginTransaction();
124:
125: setUpData(session);
126:
127: CompassQueryBuilder queryBuilder = session.queryBuilder();
128: CompassHits hits = queryBuilder.alias("a").hits();
129: assertEquals(4, hits.length());
130:
131: try {
132: queryBuilder.alias("b").hits();
133: fail();
134: } catch (CompassException e) {
135: // this is ok
136: }
137:
138: tr.commit();
139: session.close();
140: }
141:
142: public void testEq() {
143: CompassSession session = openSession();
144: CompassTransaction tr = session.beginTransaction();
145:
146: setUpData(session);
147:
148: CompassQueryBuilder queryBuilder = session.queryBuilder();
149: CompassHits hits = queryBuilder.term("mvalue1", "0001").hits();
150: assertEquals(1, hits.length());
151:
152: hits = queryBuilder.term("mvalue2", "brown").hits();
153: assertEquals(1, hits.length());
154:
155: hits = queryBuilder.term("mvalue2", "test").hits();
156: assertEquals(0, hits.length());
157:
158: Calendar calendar = Calendar.getInstance();
159: calendar.set(2000, 1, 1);
160: hits = queryBuilder.term("a.date", calendar.getTime()).hits();
161: assertEquals(1, hits.length());
162: hits = queryBuilder.term("a.date.date", calendar.getTime())
163: .hits();
164: assertEquals(1, hits.length());
165: calendar.set(2001, 1, 1);
166: hits = queryBuilder.term("a.date", calendar.getTime()).hits();
167: assertEquals(0, hits.length());
168:
169: tr.commit();
170: session.close();
171: }
172:
173: public void testBetween() {
174: CompassSession session = openSession();
175: CompassTransaction tr = session.beginTransaction();
176:
177: setUpData(session);
178:
179: CompassQueryBuilder queryBuilder = session.queryBuilder();
180: CompassHits hits = queryBuilder.between("mvalue1", "0001",
181: "0003", true).hits();
182: assertEquals(3, hits.length());
183:
184: queryBuilder = session.queryBuilder();
185: hits = queryBuilder.between("mvalue1", "0001", "0003", false)
186: .hits();
187: assertEquals(1, hits.length());
188:
189: tr.commit();
190: session.close();
191: }
192:
193: public void testGt() {
194: CompassSession session = openSession();
195: CompassTransaction tr = session.beginTransaction();
196:
197: setUpData(session);
198:
199: CompassQueryBuilder queryBuilder = session.queryBuilder();
200: CompassHits hits = queryBuilder.gt("mvalue1", "0002").hits();
201: assertEquals(2, hits.length());
202:
203: tr.commit();
204: session.close();
205: }
206:
207: public void testGe() {
208: CompassSession session = openSession();
209: CompassTransaction tr = session.beginTransaction();
210:
211: setUpData(session);
212:
213: CompassQueryBuilder queryBuilder = session.queryBuilder();
214: CompassHits hits = queryBuilder.ge("mvalue1", "0002").hits();
215: assertEquals(3, hits.length());
216:
217: tr.commit();
218: session.close();
219: }
220:
221: public void testLt() {
222: CompassSession session = openSession();
223: CompassTransaction tr = session.beginTransaction();
224:
225: setUpData(session);
226:
227: CompassQueryBuilder queryBuilder = session.queryBuilder();
228: CompassHits hits = queryBuilder.lt("mvalue1", "0002").hits();
229: assertEquals(1, hits.length());
230:
231: tr.commit();
232: session.close();
233: }
234:
235: public void testLe() {
236: CompassSession session = openSession();
237: CompassTransaction tr = session.beginTransaction();
238:
239: setUpData(session);
240:
241: CompassQueryBuilder queryBuilder = session.queryBuilder();
242: CompassHits hits = queryBuilder.le("mvalue1", "0002").hits();
243: assertEquals(2, hits.length());
244:
245: tr.commit();
246: session.close();
247: }
248:
249: public void testPrefix() {
250: CompassSession session = openSession();
251: CompassTransaction tr = session.beginTransaction();
252:
253: setUpData(session);
254:
255: CompassQueryBuilder queryBuilder = session.queryBuilder();
256: CompassHits hits = queryBuilder.prefix("mvalue1", "000").hits();
257: assertEquals(4, hits.length());
258:
259: hits = queryBuilder.prefix("a.value1", "000").hits();
260: assertEquals(4, hits.length());
261:
262: // test escaping
263: hits = queryBuilder.prefix("'a.value1'", "000").hits();
264: assertEquals(0, hits.length());
265:
266: queryBuilder = session.queryBuilder();
267: hits = queryBuilder.prefix("mvalue1", "0002").hits();
268: assertEquals(1, hits.length());
269:
270: queryBuilder = session.queryBuilder();
271: hits = queryBuilder.prefix("mvalue1", "001").hits();
272: assertEquals(0, hits.length());
273:
274: tr.commit();
275: session.close();
276: }
277:
278: public void testWildcard() {
279: CompassSession session = openSession();
280: CompassTransaction tr = session.beginTransaction();
281:
282: setUpData(session);
283:
284: CompassQueryBuilder queryBuilder = session.queryBuilder();
285: CompassHits hits = queryBuilder.wildcard("mvalue1", "000*")
286: .hits();
287: assertEquals(4, hits.length());
288:
289: hits = queryBuilder.wildcard("a.value1", "000*").hits();
290: assertEquals(4, hits.length());
291:
292: hits = queryBuilder.wildcard("a.value1.mvalue1", "000*").hits();
293: assertEquals(4, hits.length());
294:
295: tr.commit();
296: session.close();
297: }
298:
299: public void testPhrase() {
300: CompassSession session = openSession();
301: CompassTransaction tr = session.beginTransaction();
302:
303: setUpData(session);
304:
305: CompassQueryBuilder queryBuilder = session.queryBuilder();
306: CompassHits hits = queryBuilder.multiPhrase("mvalue2").setSlop(
307: 0).add("quick").add("fox").toQuery().hits();
308: assertEquals(0, hits.length());
309:
310: queryBuilder = session.queryBuilder();
311: hits = queryBuilder.multiPhrase("mvalue2").setSlop(1).add(
312: "quick").add("fox").toQuery().hits();
313: assertEquals(1, hits.length());
314:
315: tr.commit();
316: session.close();
317: }
318:
319: public void testFuzzy() {
320: CompassSession session = openSession();
321: CompassTransaction tr = session.beginTransaction();
322:
323: A a = new A();
324: a.setId(new Long(1));
325: a.setValue1("wuzzy");
326: session.save(a);
327:
328: CompassQueryBuilder queryBuilder = session.queryBuilder();
329: CompassHits hits = queryBuilder.fuzzy("mvalue1", "wuzza")
330: .hits();
331: assertEquals(1, hits.length());
332:
333: hits = queryBuilder.fuzzy("a.value1.mvalue1", "wuzza").hits();
334: assertEquals(1, hits.length());
335:
336: queryBuilder = session.queryBuilder();
337: hits = queryBuilder.fuzzy("mvalue1", "wuzza", 0.9999f).hits();
338: assertEquals(0, hits.length());
339:
340: tr.commit();
341: session.close();
342: }
343:
344: public void testBool() {
345: CompassSession session = openSession();
346: CompassTransaction tr = session.beginTransaction();
347:
348: setUpData(session);
349:
350: CompassQueryBuilder queryBuilder = session.queryBuilder();
351: CompassHits hits = queryBuilder.bool().addMust(
352: queryBuilder.term("mvalue1", "0001")).toQuery().hits();
353: assertEquals(1, hits.length());
354:
355: queryBuilder = session.queryBuilder();
356: hits = queryBuilder.bool().addMust(
357: queryBuilder.term("mvalue1", "0001")).addMust(
358: queryBuilder.term("mvalue2", "test1")).toQuery().hits();
359: assertEquals(1, hits.length());
360:
361: queryBuilder = session.queryBuilder();
362: hits = queryBuilder.bool().addMust(
363: queryBuilder.term("mvalue1", "0001")).addMust(
364: queryBuilder.term("mvalue2", "test2")).toQuery().hits();
365: assertEquals(0, hits.length());
366:
367: queryBuilder = session.queryBuilder();
368: hits = queryBuilder.bool().addMust(
369: queryBuilder.term("mvalue1", "0001")).addMustNot(
370: queryBuilder.term("mvalue2", "test1")).toQuery().hits();
371: assertEquals(0, hits.length());
372:
373: queryBuilder = session.queryBuilder();
374: hits = queryBuilder.bool().addMust(
375: queryBuilder.term("mvalue1", "0001")).addShould(
376: queryBuilder.term("mvalue2", "test2XXX")).toQuery()
377: .hits();
378: assertEquals(1, hits.length());
379:
380: tr.commit();
381: session.close();
382: }
383:
384: public void testSpanEq() {
385: CompassSession session = openSession();
386: CompassTransaction tr = session.beginTransaction();
387:
388: setUpData(session);
389:
390: CompassQueryBuilder queryBuilder = session.queryBuilder();
391: CompassHits hits = queryBuilder.spanEq("mvalue1", "0001")
392: .hits();
393: assertEquals(1, hits.length());
394:
395: hits = queryBuilder.spanEq("mvalue2", "test").hits();
396: assertEquals(0, hits.length());
397:
398: Calendar calendar = Calendar.getInstance();
399: calendar.set(2000, 1, 1);
400: hits = queryBuilder.spanEq("a.date", calendar.getTime()).hits();
401: assertEquals(1, hits.length());
402: hits = queryBuilder.spanEq("a.date.date", calendar.getTime())
403: .hits();
404: assertEquals(1, hits.length());
405: calendar.set(2001, 1, 1);
406: hits = queryBuilder.spanEq("a.date", calendar.getTime()).hits();
407: assertEquals(0, hits.length());
408:
409: tr.commit();
410: session.close();
411: }
412:
413: public void testSpanFirst() {
414: CompassSession session = openSession();
415: CompassTransaction tr = session.beginTransaction();
416:
417: setUpData(session);
418:
419: CompassQueryBuilder queryBuilder = session.queryBuilder();
420: CompassHits hits = queryBuilder
421: .spanFirst("mvalue2", "brown", 1).hits();
422: assertEquals(0, hits.length());
423:
424: queryBuilder = session.queryBuilder();
425: hits = queryBuilder.spanFirst("mvalue2", "brown", 2).hits();
426: assertEquals(1, hits.length());
427:
428: tr.commit();
429: session.close();
430: }
431:
432: public void testSpanNear() {
433: CompassSession session = openSession();
434: CompassTransaction tr = session.beginTransaction();
435:
436: setUpData(session);
437:
438: CompassQueryBuilder queryBuilder = session.queryBuilder();
439: CompassHits hits = queryBuilder.spanNear("mvalue2")
440: .add("quick").add("brown").add("dog").setSlop(0)
441: .setInOrder(true).toQuery().hits();
442: assertEquals(0, hits.length());
443:
444: hits = queryBuilder.spanNear("mvalue2").add("quick").add(
445: "brown").add("dog").setSlop(3).setInOrder(true)
446: .toQuery().hits();
447: assertEquals(0, hits.length());
448:
449: hits = queryBuilder.spanNear("mvalue2").add("quick").add(
450: "brown").add("dog").setSlop(4).setInOrder(true)
451: .toQuery().hits();
452: assertEquals(1, hits.length());
453:
454: hits = queryBuilder.spanNear("mvalue2").add("lazy").add("fox")
455: .setSlop(3).setInOrder(false).toQuery().hits();
456: assertEquals(1, hits.length());
457:
458: tr.commit();
459: session.close();
460: }
461:
462: public void testSpanNot() {
463: CompassSession session = openSession();
464: CompassTransaction tr = session.beginTransaction();
465:
466: setUpData(session);
467:
468: CompassQueryBuilder queryBuilder = session.queryBuilder();
469: CompassHits hits = queryBuilder.spanNot(
470: queryBuilder.spanNear("mvalue2").add("quick")
471: .add("fox").setSlop(1).setInOrder(true)
472: .toQuery(),
473: queryBuilder.spanEq("mvalue2", "dog")).hits();
474: assertEquals(1, hits.length());
475:
476: hits = queryBuilder.spanNot(
477: queryBuilder.spanNear("mvalue2").add("quick")
478: .add("fox").setSlop(1).setInOrder(true)
479: .toQuery(),
480: queryBuilder.spanEq("mvalue2", "brown")).hits();
481: assertEquals(0, hits.length());
482:
483: tr.commit();
484: session.close();
485: }
486:
487: public void testSpanOr() {
488: CompassSession session = openSession();
489: CompassTransaction tr = session.beginTransaction();
490:
491: setUpData(session);
492:
493: CompassQueryBuilder queryBuilder = session.queryBuilder();
494: CompassHits hits = queryBuilder.spanOr().add(
495: queryBuilder.spanEq("mvalue2", "quick")).add(
496: queryBuilder.spanEq("mvalue2", "notthere")).toQuery()
497: .hits();
498: assertEquals(1, hits.length());
499:
500: tr.commit();
501: session.close();
502: }
503: }
|