001: package org.apache.lucene.index;
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: import java.util.Arrays;
022: import java.util.Collection;
023:
024: import org.apache.lucene.util.LuceneTestCase;
025:
026: import org.apache.lucene.analysis.standard.StandardAnalyzer;
027: import org.apache.lucene.document.Document;
028: import org.apache.lucene.document.Field;
029: import org.apache.lucene.document.MapFieldSelector;
030: import org.apache.lucene.search.BooleanQuery;
031: import org.apache.lucene.search.Hits;
032: import org.apache.lucene.search.IndexSearcher;
033: import org.apache.lucene.search.Query;
034: import org.apache.lucene.search.Searcher;
035: import org.apache.lucene.search.TermQuery;
036: import org.apache.lucene.search.BooleanClause.Occur;
037: import org.apache.lucene.store.Directory;
038: import org.apache.lucene.store.RAMDirectory;
039: import org.apache.lucene.store.MockRAMDirectory;
040:
041: public class TestParallelReader extends LuceneTestCase {
042:
043: private Searcher parallel;
044: private Searcher single;
045:
046: protected void setUp() throws Exception {
047: super .setUp();
048: single = single();
049: parallel = parallel();
050: }
051:
052: public void testQueries() throws Exception {
053: queryTest(new TermQuery(new Term("f1", "v1")));
054: queryTest(new TermQuery(new Term("f1", "v2")));
055: queryTest(new TermQuery(new Term("f2", "v1")));
056: queryTest(new TermQuery(new Term("f2", "v2")));
057: queryTest(new TermQuery(new Term("f3", "v1")));
058: queryTest(new TermQuery(new Term("f3", "v2")));
059: queryTest(new TermQuery(new Term("f4", "v1")));
060: queryTest(new TermQuery(new Term("f4", "v2")));
061:
062: BooleanQuery bq1 = new BooleanQuery();
063: bq1.add(new TermQuery(new Term("f1", "v1")), Occur.MUST);
064: bq1.add(new TermQuery(new Term("f4", "v1")), Occur.MUST);
065: queryTest(bq1);
066: }
067:
068: public void testFieldNames() throws Exception {
069: Directory dir1 = getDir1();
070: Directory dir2 = getDir2();
071: ParallelReader pr = new ParallelReader();
072: pr.add(IndexReader.open(dir1));
073: pr.add(IndexReader.open(dir2));
074: Collection fieldNames = pr
075: .getFieldNames(IndexReader.FieldOption.ALL);
076: assertEquals(4, fieldNames.size());
077: assertTrue(fieldNames.contains("f1"));
078: assertTrue(fieldNames.contains("f2"));
079: assertTrue(fieldNames.contains("f3"));
080: assertTrue(fieldNames.contains("f4"));
081: }
082:
083: public void testDocument() throws IOException {
084: Directory dir1 = getDir1();
085: Directory dir2 = getDir2();
086: ParallelReader pr = new ParallelReader();
087: pr.add(IndexReader.open(dir1));
088: pr.add(IndexReader.open(dir2));
089:
090: Document doc11 = pr.document(0, new MapFieldSelector(
091: new String[] { "f1" }));
092: Document doc24 = pr.document(1, new MapFieldSelector(Arrays
093: .asList(new String[] { "f4" })));
094: Document doc223 = pr.document(1, new MapFieldSelector(
095: new String[] { "f2", "f3" }));
096:
097: assertEquals(1, doc11.getFields().size());
098: assertEquals(1, doc24.getFields().size());
099: assertEquals(2, doc223.getFields().size());
100:
101: assertEquals("v1", doc11.get("f1"));
102: assertEquals("v2", doc24.get("f4"));
103: assertEquals("v2", doc223.get("f2"));
104: assertEquals("v2", doc223.get("f3"));
105: }
106:
107: public void testIncompatibleIndexes() throws IOException {
108: // two documents:
109: Directory dir1 = getDir1();
110:
111: // one document only:
112: Directory dir2 = new MockRAMDirectory();
113: IndexWriter w2 = new IndexWriter(dir2, new StandardAnalyzer(),
114: true);
115: Document d3 = new Document();
116: d3.add(new Field("f3", "v1", Field.Store.YES,
117: Field.Index.TOKENIZED));
118: w2.addDocument(d3);
119: w2.close();
120:
121: ParallelReader pr = new ParallelReader();
122: pr.add(IndexReader.open(dir1));
123: try {
124: pr.add(IndexReader.open(dir2));
125: fail("didn't get exptected exception: indexes don't have same number of documents");
126: } catch (IllegalArgumentException e) {
127: // expected exception
128: }
129: }
130:
131: public void testIsCurrent() throws IOException {
132: Directory dir1 = getDir1();
133: Directory dir2 = getDir1();
134: ParallelReader pr = new ParallelReader();
135: pr.add(IndexReader.open(dir1));
136: pr.add(IndexReader.open(dir2));
137:
138: assertTrue(pr.isCurrent());
139: IndexReader modifier = IndexReader.open(dir1);
140: modifier.setNorm(0, "f1", 100);
141: modifier.close();
142:
143: // one of the two IndexReaders which ParallelReader is using
144: // is not current anymore
145: assertFalse(pr.isCurrent());
146:
147: modifier = IndexReader.open(dir2);
148: modifier.setNorm(0, "f3", 100);
149: modifier.close();
150:
151: // now both are not current anymore
152: assertFalse(pr.isCurrent());
153: }
154:
155: public void testIsOptimized() throws IOException {
156: Directory dir1 = getDir1();
157: Directory dir2 = getDir1();
158:
159: // add another document to ensure that the indexes are not optimized
160: IndexWriter modifier = new IndexWriter(dir1,
161: new StandardAnalyzer());
162: Document d = new Document();
163: d.add(new Field("f1", "v1", Field.Store.YES,
164: Field.Index.TOKENIZED));
165: modifier.addDocument(d);
166: modifier.close();
167:
168: modifier = new IndexWriter(dir2, new StandardAnalyzer());
169: d = new Document();
170: d.add(new Field("f2", "v2", Field.Store.YES,
171: Field.Index.TOKENIZED));
172: modifier.addDocument(d);
173: modifier.close();
174:
175: ParallelReader pr = new ParallelReader();
176: pr.add(IndexReader.open(dir1));
177: pr.add(IndexReader.open(dir2));
178: assertFalse(pr.isOptimized());
179: pr.close();
180:
181: modifier = new IndexWriter(dir1, new StandardAnalyzer());
182: modifier.optimize();
183: modifier.close();
184:
185: pr = new ParallelReader();
186: pr.add(IndexReader.open(dir1));
187: pr.add(IndexReader.open(dir2));
188: // just one of the two indexes are optimized
189: assertFalse(pr.isOptimized());
190: pr.close();
191:
192: modifier = new IndexWriter(dir2, new StandardAnalyzer());
193: modifier.optimize();
194: modifier.close();
195:
196: pr = new ParallelReader();
197: pr.add(IndexReader.open(dir1));
198: pr.add(IndexReader.open(dir2));
199: // now both indexes are optimized
200: assertTrue(pr.isOptimized());
201: pr.close();
202:
203: }
204:
205: private void queryTest(Query query) throws IOException {
206: Hits parallelHits = parallel.search(query);
207: Hits singleHits = single.search(query);
208: assertEquals(parallelHits.length(), singleHits.length());
209: for (int i = 0; i < parallelHits.length(); i++) {
210: assertEquals(parallelHits.score(i), singleHits.score(i),
211: 0.001f);
212: Document docParallel = parallelHits.doc(i);
213: Document docSingle = singleHits.doc(i);
214: assertEquals(docParallel.get("f1"), docSingle.get("f1"));
215: assertEquals(docParallel.get("f2"), docSingle.get("f2"));
216: assertEquals(docParallel.get("f3"), docSingle.get("f3"));
217: assertEquals(docParallel.get("f4"), docSingle.get("f4"));
218: }
219: }
220:
221: // Fields 1-4 indexed together:
222: private Searcher single() throws IOException {
223: Directory dir = new MockRAMDirectory();
224: IndexWriter w = new IndexWriter(dir, new StandardAnalyzer(),
225: true);
226: Document d1 = new Document();
227: d1.add(new Field("f1", "v1", Field.Store.YES,
228: Field.Index.TOKENIZED));
229: d1.add(new Field("f2", "v1", Field.Store.YES,
230: Field.Index.TOKENIZED));
231: d1.add(new Field("f3", "v1", Field.Store.YES,
232: Field.Index.TOKENIZED));
233: d1.add(new Field("f4", "v1", Field.Store.YES,
234: Field.Index.TOKENIZED));
235: w.addDocument(d1);
236: Document d2 = new Document();
237: d2.add(new Field("f1", "v2", Field.Store.YES,
238: Field.Index.TOKENIZED));
239: d2.add(new Field("f2", "v2", Field.Store.YES,
240: Field.Index.TOKENIZED));
241: d2.add(new Field("f3", "v2", Field.Store.YES,
242: Field.Index.TOKENIZED));
243: d2.add(new Field("f4", "v2", Field.Store.YES,
244: Field.Index.TOKENIZED));
245: w.addDocument(d2);
246: w.close();
247:
248: return new IndexSearcher(dir);
249: }
250:
251: // Fields 1 & 2 in one index, 3 & 4 in other, with ParallelReader:
252: private Searcher parallel() throws IOException {
253: Directory dir1 = getDir1();
254: Directory dir2 = getDir2();
255: ParallelReader pr = new ParallelReader();
256: pr.add(IndexReader.open(dir1));
257: pr.add(IndexReader.open(dir2));
258: return new IndexSearcher(pr);
259: }
260:
261: private Directory getDir1() throws IOException {
262: Directory dir1 = new MockRAMDirectory();
263: IndexWriter w1 = new IndexWriter(dir1, new StandardAnalyzer(),
264: true);
265: Document d1 = new Document();
266: d1.add(new Field("f1", "v1", Field.Store.YES,
267: Field.Index.TOKENIZED));
268: d1.add(new Field("f2", "v1", Field.Store.YES,
269: Field.Index.TOKENIZED));
270: w1.addDocument(d1);
271: Document d2 = new Document();
272: d2.add(new Field("f1", "v2", Field.Store.YES,
273: Field.Index.TOKENIZED));
274: d2.add(new Field("f2", "v2", Field.Store.YES,
275: Field.Index.TOKENIZED));
276: w1.addDocument(d2);
277: w1.close();
278: return dir1;
279: }
280:
281: private Directory getDir2() throws IOException {
282: Directory dir2 = new RAMDirectory();
283: IndexWriter w2 = new IndexWriter(dir2, new StandardAnalyzer(),
284: true);
285: Document d3 = new Document();
286: d3.add(new Field("f3", "v1", Field.Store.YES,
287: Field.Index.TOKENIZED));
288: d3.add(new Field("f4", "v1", Field.Store.YES,
289: Field.Index.TOKENIZED));
290: w2.addDocument(d3);
291: Document d4 = new Document();
292: d4.add(new Field("f3", "v2", Field.Store.YES,
293: Field.Index.TOKENIZED));
294: d4.add(new Field("f4", "v2", Field.Store.YES,
295: Field.Index.TOKENIZED));
296: w2.addDocument(d4);
297: w2.close();
298: return dir2;
299: }
300:
301: }
|