001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021:
022: import org.apache.lucene.index.IndexReader;
023: import org.apache.lucene.index.Term;
024:
025: /**
026: * A basic 'positive' Unit test class for the RangeFilter class.
027: *
028: * <p>
029: * NOTE: at the moment, this class only tests for 'positive' results,
030: * it does not verify the results to ensure there are no 'false positives',
031: * nor does it adequately test 'negative' results. It also does not test
032: * that garbage in results in an Exception.
033: */
034: public class TestRangeFilter extends BaseTestRangeFilter {
035:
036: public TestRangeFilter(String name) {
037: super (name);
038: }
039:
040: public TestRangeFilter() {
041: super ();
042: }
043:
044: public void testRangeFilterId() throws IOException {
045:
046: IndexReader reader = IndexReader.open(index);
047: IndexSearcher search = new IndexSearcher(reader);
048:
049: int medId = ((maxId - minId) / 2);
050:
051: String minIP = pad(minId);
052: String maxIP = pad(maxId);
053: String medIP = pad(medId);
054:
055: int numDocs = reader.numDocs();
056:
057: assertEquals("num of docs", numDocs, 1 + maxId - minId);
058:
059: Hits result;
060: Query q = new TermQuery(new Term("body", "body"));
061:
062: // test id, bounded on both ends
063:
064: result = search.search(q, new RangeFilter("id", minIP, maxIP,
065: T, T));
066: assertEquals("find all", numDocs, result.length());
067:
068: result = search.search(q, new RangeFilter("id", minIP, maxIP,
069: T, F));
070: assertEquals("all but last", numDocs - 1, result.length());
071:
072: result = search.search(q, new RangeFilter("id", minIP, maxIP,
073: F, T));
074: assertEquals("all but first", numDocs - 1, result.length());
075:
076: result = search.search(q, new RangeFilter("id", minIP, maxIP,
077: F, F));
078: assertEquals("all but ends", numDocs - 2, result.length());
079:
080: result = search.search(q, new RangeFilter("id", medIP, maxIP,
081: T, T));
082: assertEquals("med and up", 1 + maxId - medId, result.length());
083:
084: result = search.search(q, new RangeFilter("id", minIP, medIP,
085: T, T));
086: assertEquals("up to med", 1 + medId - minId, result.length());
087:
088: // unbounded id
089:
090: result = search.search(q, new RangeFilter("id", minIP, null, T,
091: F));
092: assertEquals("min and up", numDocs, result.length());
093:
094: result = search.search(q, new RangeFilter("id", null, maxIP, F,
095: T));
096: assertEquals("max and down", numDocs, result.length());
097:
098: result = search.search(q, new RangeFilter("id", minIP, null, F,
099: F));
100: assertEquals("not min, but up", numDocs - 1, result.length());
101:
102: result = search.search(q, new RangeFilter("id", null, maxIP, F,
103: F));
104: assertEquals("not max, but down", numDocs - 1, result.length());
105:
106: result = search.search(q, new RangeFilter("id", medIP, maxIP,
107: T, F));
108: assertEquals("med and up, not max", maxId - medId, result
109: .length());
110:
111: result = search.search(q, new RangeFilter("id", minIP, medIP,
112: F, T));
113: assertEquals("not min, up to med", medId - minId, result
114: .length());
115:
116: // very small sets
117:
118: result = search.search(q, new RangeFilter("id", minIP, minIP,
119: F, F));
120: assertEquals("min,min,F,F", 0, result.length());
121: result = search.search(q, new RangeFilter("id", medIP, medIP,
122: F, F));
123: assertEquals("med,med,F,F", 0, result.length());
124: result = search.search(q, new RangeFilter("id", maxIP, maxIP,
125: F, F));
126: assertEquals("max,max,F,F", 0, result.length());
127:
128: result = search.search(q, new RangeFilter("id", minIP, minIP,
129: T, T));
130: assertEquals("min,min,T,T", 1, result.length());
131: result = search.search(q, new RangeFilter("id", null, minIP, F,
132: T));
133: assertEquals("nul,min,F,T", 1, result.length());
134:
135: result = search.search(q, new RangeFilter("id", maxIP, maxIP,
136: T, T));
137: assertEquals("max,max,T,T", 1, result.length());
138: result = search.search(q, new RangeFilter("id", maxIP, null, T,
139: F));
140: assertEquals("max,nul,T,T", 1, result.length());
141:
142: result = search.search(q, new RangeFilter("id", medIP, medIP,
143: T, T));
144: assertEquals("med,med,T,T", 1, result.length());
145:
146: }
147:
148: public void testRangeFilterRand() throws IOException {
149:
150: IndexReader reader = IndexReader.open(index);
151: IndexSearcher search = new IndexSearcher(reader);
152:
153: String minRP = pad(minR);
154: String maxRP = pad(maxR);
155:
156: int numDocs = reader.numDocs();
157:
158: assertEquals("num of docs", numDocs, 1 + maxId - minId);
159:
160: Hits result;
161: Query q = new TermQuery(new Term("body", "body"));
162:
163: // test extremes, bounded on both ends
164:
165: result = search.search(q, new RangeFilter("rand", minRP, maxRP,
166: T, T));
167: assertEquals("find all", numDocs, result.length());
168:
169: result = search.search(q, new RangeFilter("rand", minRP, maxRP,
170: T, F));
171: assertEquals("all but biggest", numDocs - 1, result.length());
172:
173: result = search.search(q, new RangeFilter("rand", minRP, maxRP,
174: F, T));
175: assertEquals("all but smallest", numDocs - 1, result.length());
176:
177: result = search.search(q, new RangeFilter("rand", minRP, maxRP,
178: F, F));
179: assertEquals("all but extremes", numDocs - 2, result.length());
180:
181: // unbounded
182:
183: result = search.search(q, new RangeFilter("rand", minRP, null,
184: T, F));
185: assertEquals("smallest and up", numDocs, result.length());
186:
187: result = search.search(q, new RangeFilter("rand", null, maxRP,
188: F, T));
189: assertEquals("biggest and down", numDocs, result.length());
190:
191: result = search.search(q, new RangeFilter("rand", minRP, null,
192: F, F));
193: assertEquals("not smallest, but up", numDocs - 1, result
194: .length());
195:
196: result = search.search(q, new RangeFilter("rand", null, maxRP,
197: F, F));
198: assertEquals("not biggest, but down", numDocs - 1, result
199: .length());
200:
201: // very small sets
202:
203: result = search.search(q, new RangeFilter("rand", minRP, minRP,
204: F, F));
205: assertEquals("min,min,F,F", 0, result.length());
206: result = search.search(q, new RangeFilter("rand", maxRP, maxRP,
207: F, F));
208: assertEquals("max,max,F,F", 0, result.length());
209:
210: result = search.search(q, new RangeFilter("rand", minRP, minRP,
211: T, T));
212: assertEquals("min,min,T,T", 1, result.length());
213: result = search.search(q, new RangeFilter("rand", null, minRP,
214: F, T));
215: assertEquals("nul,min,F,T", 1, result.length());
216:
217: result = search.search(q, new RangeFilter("rand", maxRP, maxRP,
218: T, T));
219: assertEquals("max,max,T,T", 1, result.length());
220: result = search.search(q, new RangeFilter("rand", maxRP, null,
221: T, F));
222: assertEquals("max,nul,T,T", 1, result.length());
223:
224: }
225:
226: }
|