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.find;
018:
019: import java.util.Iterator;
020: import java.util.NoSuchElementException;
021:
022: import org.apache.lucene.search.Explanation;
023: import org.compass.core.CompassDetachedHits;
024: import org.compass.core.CompassHit;
025: import org.compass.core.CompassHits;
026: import org.compass.core.CompassSession;
027: import org.compass.core.CompassTransaction;
028: import org.compass.core.Resource;
029: import org.compass.core.engine.SearchEngineException;
030: import org.compass.core.lucene.util.LuceneHelper;
031: import org.compass.core.test.AbstractTestCase;
032:
033: /**
034: * @author kimchy
035: */
036: public class FindTests extends AbstractTestCase {
037:
038: protected String[] getMappings() {
039: return new String[] { "find/find.cpm.xml" };
040: }
041:
042: public void testIterator() {
043: CompassSession session = openSession();
044: CompassTransaction tr = session.beginTransaction();
045:
046: Long id1 = new Long(1);
047: A a1 = new A();
048: a1.setId(id1);
049: a1.setValue("value 1");
050: session.save(a1);
051: Long id2 = new Long(2);
052: A a2 = new A();
053: a2.setId(id2);
054: a2.setValue("value 2");
055: session.save(a2);
056:
057: Iterator<CompassHit> it = session.find("mvalue:\"value 1\"")
058: .detach().iterator();
059: assertEquals(id1, ((A) it.next().getData()).getId());
060: try {
061: it.next();
062: fail();
063: } catch (NoSuchElementException e) {
064:
065: }
066:
067: for (CompassHit hit : session.find("mvalue:\"value 1\"")) {
068: assertEquals(id1, ((A) hit.getData()).getId());
069: }
070:
071: tr.commit();
072: session.close();
073: }
074:
075: public void testDetach() {
076: addDataA(0, 50);
077:
078: CompassSession session = openSession();
079: CompassTransaction tr = session.beginTransaction();
080: CompassHits hits = session.find("alias:a1");
081: assertEquals(50, hits.getLength());
082: CompassDetachedHits detachedHits = hits.detach(40, 10);
083: tr.commit();
084: session.close();
085:
086: assertEquals(10, detachedHits.getLength());
087: assertEquals(50, detachedHits.totalLength());
088: Resource r = detachedHits.resource(0);
089: assertEquals("40", r.getValue("id"));
090: assertEquals(new Long(40), ((A) detachedHits.data(0)).getId());
091:
092: CompassHit hit = detachedHits.hit(0);
093: assertEquals("40", hit.getResource().getValue("id"));
094: assertEquals(new Long(40), ((A) hit.getData()).getId());
095:
096: Resource[] resources = detachedHits.getResources();
097: assertEquals(10, resources.length);
098:
099: Object[] datas = detachedHits.getDatas();
100: assertEquals(10, datas.length);
101: }
102:
103: public void testWithAsterix() {
104: addDataA(0, 10);
105: addDataB(0, 10);
106:
107: CompassSession session = openSession();
108: CompassTransaction tr = session.beginTransaction();
109: addDataA(10, 20);
110: addDataB(10, 20);
111: CompassHits hits = session.find("v*");
112: assertEquals(40, hits.getLength());
113: tr.commit();
114: session.close();
115: }
116:
117: public void testWithPrefix() {
118: CompassSession session = openSession();
119: CompassTransaction tr = session.beginTransaction();
120: A a = new A();
121: a.setId(new Long(1));
122: a.setValue("20020101");
123: session.save(a);
124: CompassHits hits = session.find("mvalue:20020101");
125: assertEquals(1, hits.getLength());
126: tr.commit();
127: session.close();
128: }
129:
130: public void testSubIndexAliasNarrow() {
131: addDataA(0, 10);
132: addDataB(0, 10);
133:
134: CompassSession session = openSession();
135: CompassTransaction tr = session.beginTransaction();
136: CompassHits hits = session.find("alias:a1 OR alias:b1");
137: assertEquals(20, hits.getLength());
138:
139: hits = session.queryBuilder().queryString(
140: "alias:a1 OR alias:b1").toQuery().setAliases(
141: new String[] { "a1" }).hits();
142: assertEquals(10, hits.getLength());
143:
144: hits = session.queryBuilder().queryString(
145: "alias:a1 OR alias:b1").toQuery().setSubIndexes(
146: new String[] { "a1" }).hits();
147: assertEquals(10, hits.getLength());
148:
149: hits = session.queryBuilder().queryString(
150: "alias:a1 OR alias:b1").toQuery().setSubIndexes(
151: new String[] { "a1" })
152: .setAliases(new String[] { "a1" }).hits();
153: assertEquals(10, hits.getLength());
154:
155: tr.commit();
156: session.close();
157: }
158:
159: public void testSubIndexTypeNarrow() {
160: addDataA(0, 10);
161: addDataB(0, 10);
162:
163: CompassSession session = openSession();
164: CompassTransaction tr = session.beginTransaction();
165: CompassHits hits = session.find("alias:a1 OR alias:b1");
166: assertEquals(20, hits.getLength());
167:
168: hits = session.queryBuilder().queryString(
169: "alias:a1 OR alias:b1").toQuery().setTypes(
170: new Class[] { A.class }).hits();
171: assertEquals(10, hits.getLength());
172:
173: hits = session.queryBuilder().queryString(
174: "alias:a1 OR alias:b1").toQuery().setSubIndexes(
175: new String[] { "a1" }).hits();
176: assertEquals(10, hits.getLength());
177:
178: hits = session.queryBuilder().queryString(
179: "alias:a1 OR alias:b1").toQuery().setSubIndexes(
180: new String[] { "a1" })
181: .setTypes(new Class[] { A.class }).hits();
182: assertEquals(10, hits.getLength());
183:
184: tr.commit();
185: session.close();
186: }
187:
188: public void testLuceneExplanation() {
189: addDataA(0, 10);
190:
191: CompassSession session = openSession();
192: CompassTransaction tr = session.beginTransaction();
193: CompassHits hits = session.find("alias:a1 OR alias:b1");
194: assertEquals(10, hits.getLength());
195:
196: Explanation explanation = LuceneHelper
197: .getLuceneSearchEngineHits(hits).explain(0);
198: assertNotNull(explanation);
199: assertEquals("product of:", explanation.getDescription());
200:
201: tr.commit();
202: session.close();
203: }
204:
205: public void testExceptionOutsideOfATransaction() {
206: CompassSession session = openSession();
207: CompassTransaction tr = session.beginTransaction();
208: CompassHits hits = session.find("alias:a1 OR alias:b1");
209: tr.commit();
210: try {
211: session.find("alias:a1 OR alias:b1");
212: fail();
213: } catch (SearchEngineException e) {
214: // all is well
215: }
216: session.close();
217: }
218:
219: private void addDataA(int from, int to) {
220: CompassSession session = openSession();
221: CompassTransaction tr = session.beginTransaction();
222: for (int i = from; i < to; i++) {
223: A a = new A();
224: a.setId(new Long(i));
225: a.setValue("value");
226: session.save(a);
227: }
228: tr.commit();
229: session.close();
230: }
231:
232: private void addDataB(int from, int to) {
233: CompassSession session = openSession();
234: CompassTransaction tr = session.beginTransaction();
235: for (int i = from; i < to; i++) {
236: B b = new B();
237: b.setId(new Long(i));
238: b.setValue("value");
239: session.save(b);
240: }
241: tr.commit();
242: session.close();
243: }
244:
245: }
|