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.boost;
018:
019: import org.compass.core.CompassHits;
020: import org.compass.core.CompassSession;
021: import org.compass.core.CompassTransaction;
022: import org.compass.core.test.AbstractTestCase;
023:
024: /**
025: * @author kimchy
026: */
027: public class BoostTests extends AbstractTestCase {
028:
029: protected String[] getMappings() {
030: return new String[] { "boost/boost.cpm.xml" };
031: }
032:
033: public void testNoBoostOrderAndScore() throws Exception {
034: CompassSession session = openSession();
035: CompassTransaction tr = session.beginTransaction();
036:
037: // first save under a1, where no boosting is done
038: // check the order for the search so we can then use the boost
039: // test to check that the order was reversed
040: A a = new A();
041: a.id = 1;
042: a.value1 = "match";
043: a.value2 = "nomatch";
044: session.save("a1", a);
045:
046: a = new A();
047: a.id = 2;
048: a.value1 = "nomatch";
049: a.value2 = "match";
050: session.save("a1", a);
051:
052: for (int i = 0; i < 10; i++) {
053: CompassHits hits = session
054: .find("value1:match OR value2:match");
055: assertEquals(2, hits.length());
056: assertTrue(hits.score(0) == hits.score(1));
057: assertEquals(1, ((A) hits.data(0)).id);
058: assertEquals(2, ((A) hits.data(1)).id);
059: }
060:
061: // check the order when we use the all proeprty
062: CompassHits hits = session.find("match");
063: assertEquals(2, hits.length());
064: assertTrue(hits.score(0) == hits.score(1));
065: assertEquals(1, ((A) hits.data(0)).id);
066: assertEquals(2, ((A) hits.data(1)).id);
067:
068: tr.commit();
069: session.close();
070: }
071:
072: public void testWithBoostOrderAndScore() throws Exception {
073: CompassSession session = openSession();
074: CompassTransaction tr = session.beginTransaction();
075:
076: // since we checked before the correct order, now we save
077: // under a2, that boosts value2, which means that a match
078: // on it will result in the hits having higher score
079: A a = new A();
080: a.id = 1;
081: a.value1 = "match";
082: a.value2 = "nomatch";
083: session.save("a2", a);
084:
085: a = new A();
086: a.id = 2;
087: a.value1 = "nomatch";
088: a.value2 = "match";
089: session.save("a2", a);
090:
091: for (int i = 0; i < 10; i++) {
092: CompassHits hits = session
093: .find("value1:match OR value2:match");
094: assertEquals(2, hits.length());
095: assertTrue(hits.score(0) > hits.score(1));
096: assertEquals(2, ((A) hits.data(0)).id);
097: assertEquals(1, ((A) hits.data(1)).id);
098: }
099:
100: // check the order when we use the all proeprty
101: // note, we now support order in the all property as well
102: CompassHits hits = session.find("match");
103: assertEquals(2, hits.length());
104: assertEquals(2, ((A) hits.data(0)).id);
105: assertEquals(1, ((A) hits.data(1)).id);
106:
107: tr.commit();
108: session.close();
109: }
110:
111: public void testClassLevelBoost() throws Exception {
112: CompassSession session = openSession();
113: CompassTransaction tr = session.beginTransaction();
114:
115: // we save exact same A values, one under a3, and one
116: // under a4, where a4 has a higher boost level
117: A a = new A();
118: a.id = 1;
119: a.value1 = "match";
120: a.value2 = "nomatch";
121: session.save("a3", a);
122:
123: a = new A();
124: a.id = 1;
125: a.value1 = "match";
126: a.value2 = "nomatch";
127: session.save("a4", a);
128:
129: for (int i = 0; i < 10; i++) {
130: CompassHits hits = session.find("value1:match");
131: assertEquals(2, hits.length());
132: assertTrue(hits.score(0) > hits.score(1));
133: assertEquals("a4", hits.resource(0).getAlias());
134: assertEquals("a3", hits.resource(1).getAlias());
135: }
136:
137: tr.commit();
138: session.close();
139: }
140:
141: public void testComponentBoostDoesNotPropogateToParent() {
142: CompassSession session = openSession();
143: CompassTransaction tr = session.beginTransaction();
144:
145: Parent parent = new Parent();
146: parent.id = 1;
147: parent.value = "match";
148: session.save("parent11", parent);
149:
150: parent = new Parent();
151: parent.id = 2;
152: parent.value = "match";
153: parent.child = new Child();
154: parent.child.value = "nomatch";
155: session.save("parent12", parent);
156:
157: // if the component boost level propogated from child to parent 12
158: // than it will score higher when searching for match
159: CompassHits hits = session.find("value:match");
160: assertEquals(2, hits.length());
161: assertTrue(hits.score(0) > hits.score(1));
162: assertEquals("parent11", hits.resource(0).getAlias());
163: assertEquals("parent12", hits.resource(1).getAlias());
164:
165: tr.commit();
166: session.close();
167: }
168: }
|