1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.Random;
25 import org.apache.lucene.analysis.MockAnalyzer;
26 import org.apache.lucene.analysis.standard.StandardAnalyzer;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.document.MapFieldSelector;
30 import org.apache.lucene.search.BooleanQuery;
31 import org.apache.lucene.search.IndexSearcher;
32 import org.apache.lucene.search.Query;
33 import org.apache.lucene.search.ScoreDoc;
34 import org.apache.lucene.search.TermQuery;
35 import org.apache.lucene.search.BooleanClause.Occur;
36 import org.apache.lucene.store.Directory;
37 import org.apache.lucene.util.LuceneTestCase;
39 public class TestParallelReader extends LuceneTestCase {
41 private IndexSearcher parallel;
42 private IndexSearcher single;
43 private Directory dir, dir1, dir2;
46 public void setUp() throws Exception {
48 single = single(random);
49 parallel = parallel(random);
53 public void tearDown() throws Exception {
54 single.getIndexReader().close();
56 parallel.getIndexReader().close();
64 public void testQueries() throws Exception {
65 queryTest(new TermQuery(new Term("f1", "v1")));
66 queryTest(new TermQuery(new Term("f1", "v2")));
67 queryTest(new TermQuery(new Term("f2", "v1")));
68 queryTest(new TermQuery(new Term("f2", "v2")));
69 queryTest(new TermQuery(new Term("f3", "v1")));
70 queryTest(new TermQuery(new Term("f3", "v2")));
71 queryTest(new TermQuery(new Term("f4", "v1")));
72 queryTest(new TermQuery(new Term("f4", "v2")));
74 BooleanQuery bq1 = new BooleanQuery();
75 bq1.add(new TermQuery(new Term("f1", "v1")), Occur.MUST);
76 bq1.add(new TermQuery(new Term("f4", "v1")), Occur.MUST);
80 public void testFieldNames() throws Exception {
81 Directory dir1 = getDir1(random);
82 Directory dir2 = getDir2(random);
83 ParallelReader pr = new ParallelReader();
84 pr.add(IndexReader.open(dir1, false));
85 pr.add(IndexReader.open(dir2, false));
86 Collection<String> fieldNames = pr.getFieldNames(IndexReader.FieldOption.ALL);
87 assertEquals(4, fieldNames.size());
88 assertTrue(fieldNames.contains("f1"));
89 assertTrue(fieldNames.contains("f2"));
90 assertTrue(fieldNames.contains("f3"));
91 assertTrue(fieldNames.contains("f4"));
97 public void testDocument() throws IOException {
98 Directory dir1 = getDir1(random);
99 Directory dir2 = getDir2(random);
100 ParallelReader pr = new ParallelReader();
101 pr.add(IndexReader.open(dir1, false));
102 pr.add(IndexReader.open(dir2, false));
104 Document doc11 = pr.document(0, new MapFieldSelector(new String[] {"f1"}));
105 Document doc24 = pr.document(1, new MapFieldSelector(Arrays.asList(new String[] {"f4"})));
106 Document doc223 = pr.document(1, new MapFieldSelector(new String[] {"f2", "f3"}));
108 assertEquals(1, doc11.getFields().size());
109 assertEquals(1, doc24.getFields().size());
110 assertEquals(2, doc223.getFields().size());
112 assertEquals("v1", doc11.get("f1"));
113 assertEquals("v2", doc24.get("f4"));
114 assertEquals("v2", doc223.get("f2"));
115 assertEquals("v2", doc223.get("f3"));
121 public void testIncompatibleIndexes() throws IOException {
123 Directory dir1 = getDir1(random);
125 // one document only:
126 Directory dir2 = newDirectory();
127 IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
128 Document d3 = new Document();
129 d3.add(newField("f3", "v1", Field.Store.YES, Field.Index.ANALYZED));
133 ParallelReader pr = new ParallelReader();
134 pr.add(IndexReader.open(dir1, false));
135 IndexReader ir = IndexReader.open(dir2, false);
138 fail("didn't get exptected exception: indexes don't have same number of documents");
139 } catch (IllegalArgumentException e) {
140 // expected exception
148 public void testIsCurrent() throws IOException {
149 Directory dir1 = getDir1(random);
150 Directory dir2 = getDir2(random);
151 ParallelReader pr = new ParallelReader();
152 pr.add(IndexReader.open(dir1, false));
153 pr.add(IndexReader.open(dir2, false));
155 assertTrue(pr.isCurrent());
156 IndexReader modifier = IndexReader.open(dir1, false);
157 modifier.setNorm(0, "f1", 100);
160 // one of the two IndexReaders which ParallelReader is using
161 // is not current anymore
162 assertFalse(pr.isCurrent());
164 modifier = IndexReader.open(dir2, false);
165 modifier.setNorm(0, "f3", 100);
168 // now both are not current anymore
169 assertFalse(pr.isCurrent());
175 public void testIsOptimized() throws IOException {
176 Directory dir1 = getDir1(random);
177 Directory dir2 = getDir2(random);
179 // add another document to ensure that the indexes are not optimized
180 IndexWriter modifier = new IndexWriter(
182 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
183 setMergePolicy(newLogMergePolicy(10))
185 Document d = new Document();
186 d.add(newField("f1", "v1", Field.Store.YES, Field.Index.ANALYZED));
187 modifier.addDocument(d);
190 modifier = new IndexWriter(
192 newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
193 setMergePolicy(newLogMergePolicy(10))
196 d.add(newField("f2", "v2", Field.Store.YES, Field.Index.ANALYZED));
197 modifier.addDocument(d);
201 ParallelReader pr = new ParallelReader();
202 pr.add(IndexReader.open(dir1, false));
203 pr.add(IndexReader.open(dir2, false));
204 assertFalse(pr.isOptimized());
207 modifier = new IndexWriter(dir1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
211 pr = new ParallelReader();
212 pr.add(IndexReader.open(dir1, false));
213 pr.add(IndexReader.open(dir2, false));
214 // just one of the two indexes are optimized
215 assertFalse(pr.isOptimized());
219 modifier = new IndexWriter(dir2, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
223 pr = new ParallelReader();
224 pr.add(IndexReader.open(dir1, false));
225 pr.add(IndexReader.open(dir2, false));
226 // now both indexes are optimized
227 assertTrue(pr.isOptimized());
233 public void testAllTermDocs() throws IOException {
234 Directory dir1 = getDir1(random);
235 Directory dir2 = getDir2(random);
236 ParallelReader pr = new ParallelReader();
237 pr.add(IndexReader.open(dir1, false));
238 pr.add(IndexReader.open(dir2, false));
240 TermDocs td = pr.termDocs(null);
241 for(int i=0;i<NUM_DOCS;i++) {
242 assertTrue(td.next());
243 assertEquals(i, td.doc());
244 assertEquals(1, td.freq());
253 private void queryTest(Query query) throws IOException {
254 ScoreDoc[] parallelHits = parallel.search(query, null, 1000).scoreDocs;
255 ScoreDoc[] singleHits = single.search(query, null, 1000).scoreDocs;
256 assertEquals(parallelHits.length, singleHits.length);
257 for(int i = 0; i < parallelHits.length; i++) {
258 assertEquals(parallelHits[i].score, singleHits[i].score, 0.001f);
259 Document docParallel = parallel.doc(parallelHits[i].doc);
260 Document docSingle = single.doc(singleHits[i].doc);
261 assertEquals(docParallel.get("f1"), docSingle.get("f1"));
262 assertEquals(docParallel.get("f2"), docSingle.get("f2"));
263 assertEquals(docParallel.get("f3"), docSingle.get("f3"));
264 assertEquals(docParallel.get("f4"), docSingle.get("f4"));
268 // Fields 1-4 indexed together:
269 private IndexSearcher single(Random random) throws IOException {
270 dir = newDirectory();
271 IndexWriter w = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
272 Document d1 = new Document();
273 d1.add(newField("f1", "v1", Field.Store.YES, Field.Index.ANALYZED));
274 d1.add(newField("f2", "v1", Field.Store.YES, Field.Index.ANALYZED));
275 d1.add(newField("f3", "v1", Field.Store.YES, Field.Index.ANALYZED));
276 d1.add(newField("f4", "v1", Field.Store.YES, Field.Index.ANALYZED));
278 Document d2 = new Document();
279 d2.add(newField("f1", "v2", Field.Store.YES, Field.Index.ANALYZED));
280 d2.add(newField("f2", "v2", Field.Store.YES, Field.Index.ANALYZED));
281 d2.add(newField("f3", "v2", Field.Store.YES, Field.Index.ANALYZED));
282 d2.add(newField("f4", "v2", Field.Store.YES, Field.Index.ANALYZED));
286 return new IndexSearcher(dir, false);
289 // Fields 1 & 2 in one index, 3 & 4 in other, with ParallelReader:
290 private IndexSearcher parallel(Random random) throws IOException {
291 dir1 = getDir1(random);
292 dir2 = getDir2(random);
293 ParallelReader pr = new ParallelReader();
294 pr.add(IndexReader.open(dir1, false));
295 pr.add(IndexReader.open(dir2, false));
296 return newSearcher(pr);
299 private Directory getDir1(Random random) throws IOException {
300 Directory dir1 = newDirectory();
301 IndexWriter w1 = new IndexWriter(dir1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
302 Document d1 = new Document();
303 d1.add(newField("f1", "v1", Field.Store.YES, Field.Index.ANALYZED));
304 d1.add(newField("f2", "v1", Field.Store.YES, Field.Index.ANALYZED));
306 Document d2 = new Document();
307 d2.add(newField("f1", "v2", Field.Store.YES, Field.Index.ANALYZED));
308 d2.add(newField("f2", "v2", Field.Store.YES, Field.Index.ANALYZED));
314 private Directory getDir2(Random random) throws IOException {
315 Directory dir2 = newDirectory();
316 IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
317 Document d3 = new Document();
318 d3.add(newField("f3", "v1", Field.Store.YES, Field.Index.ANALYZED));
319 d3.add(newField("f4", "v1", Field.Store.YES, Field.Index.ANALYZED));
321 Document d4 = new Document();
322 d4.add(newField("f3", "v2", Field.Store.YES, Field.Index.ANALYZED));
323 d4.add(newField("f4", "v2", Field.Store.YES, Field.Index.ANALYZED));