~ubuntu-branches/ubuntu/trusty/pylucene/trusty

« back to all changes in this revision

Viewing changes to lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/index/TestParallelReader.java

  • Committer: Package Import Robot
  • Author(s): Dmitry Nezhevenko
  • Date: 2012-04-23 16:43:55 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120423164355-grqtepnwtecdjfk2
Tags: 3.5.0-1
* New maintainer (closes: 670179)
* New upstream release
* Switch to dpkg-source 3.0 (quilt) format
* Switch to machine-readable debian/copyright
* Bump debian/compat to 8, drop debian/pycompat
* Switch from cdbs to dh
* Add watch file
* Build for all supported versions of python2 (closes: 581198, 632240)
* Rename binary package to python-lucene (closes: 581197)
* Add -dbg package

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.apache.lucene.index;
 
2
 
 
3
/**
 
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
 
10
 *
 
11
 *     http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
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.
 
18
 */
 
19
 
 
20
import java.io.IOException;
 
21
import java.util.Arrays;
 
22
import java.util.Collection;
 
23
import java.util.Random;
 
24
 
 
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;
 
38
 
 
39
public class TestParallelReader extends LuceneTestCase {
 
40
 
 
41
  private IndexSearcher parallel;
 
42
  private IndexSearcher single;
 
43
  private Directory dir, dir1, dir2;
 
44
  
 
45
  @Override
 
46
  public void setUp() throws Exception {
 
47
    super.setUp();
 
48
    single = single(random);
 
49
    parallel = parallel(random);
 
50
  }
 
51
  
 
52
  @Override
 
53
  public void tearDown() throws Exception {
 
54
    single.getIndexReader().close();
 
55
    single.close();
 
56
    parallel.getIndexReader().close();
 
57
    parallel.close();
 
58
    dir.close();
 
59
    dir1.close();
 
60
    dir2.close();
 
61
    super.tearDown();
 
62
  }
 
63
 
 
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")));
 
73
 
 
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);
 
77
    queryTest(bq1);
 
78
  }
 
79
 
 
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"));
 
92
    pr.close();
 
93
    dir1.close();
 
94
    dir2.close();
 
95
  }
 
96
  
 
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));
 
103
 
 
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"}));
 
107
    
 
108
    assertEquals(1, doc11.getFields().size());
 
109
    assertEquals(1, doc24.getFields().size());
 
110
    assertEquals(2, doc223.getFields().size());
 
111
    
 
112
    assertEquals("v1", doc11.get("f1"));
 
113
    assertEquals("v2", doc24.get("f4"));
 
114
    assertEquals("v2", doc223.get("f2"));
 
115
    assertEquals("v2", doc223.get("f3"));
 
116
    pr.close();
 
117
    dir1.close();
 
118
    dir2.close();
 
119
  }
 
120
  
 
121
  public void testIncompatibleIndexes() throws IOException {
 
122
    // two documents:
 
123
    Directory dir1 = getDir1(random);
 
124
 
 
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));
 
130
    w2.addDocument(d3);
 
131
    w2.close();
 
132
    
 
133
    ParallelReader pr = new ParallelReader();
 
134
    pr.add(IndexReader.open(dir1, false));
 
135
    IndexReader ir = IndexReader.open(dir2, false);
 
136
    try {
 
137
      pr.add(ir);
 
138
      fail("didn't get exptected exception: indexes don't have same number of documents");
 
139
    } catch (IllegalArgumentException e) {
 
140
      // expected exception
 
141
    }
 
142
    pr.close();
 
143
    ir.close();
 
144
    dir1.close();
 
145
    dir2.close();
 
146
  }
 
147
  
 
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));
 
154
    
 
155
    assertTrue(pr.isCurrent());
 
156
    IndexReader modifier = IndexReader.open(dir1, false);
 
157
    modifier.setNorm(0, "f1", 100);
 
158
    modifier.close();
 
159
    
 
160
    // one of the two IndexReaders which ParallelReader is using
 
161
    // is not current anymore
 
162
    assertFalse(pr.isCurrent());
 
163
    
 
164
    modifier = IndexReader.open(dir2, false);
 
165
    modifier.setNorm(0, "f3", 100);
 
166
    modifier.close();
 
167
    
 
168
    // now both are not current anymore
 
169
    assertFalse(pr.isCurrent());
 
170
    pr.close();
 
171
    dir1.close();
 
172
    dir2.close();
 
173
  }
 
174
 
 
175
  public void testIsOptimized() throws IOException {
 
176
    Directory dir1 = getDir1(random);
 
177
    Directory dir2 = getDir2(random);
 
178
    
 
179
    // add another document to ensure that the indexes are not optimized
 
180
    IndexWriter modifier = new IndexWriter(
 
181
        dir1,
 
182
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
 
183
            setMergePolicy(newLogMergePolicy(10))
 
184
    );
 
185
    Document d = new Document();
 
186
    d.add(newField("f1", "v1", Field.Store.YES, Field.Index.ANALYZED));
 
187
    modifier.addDocument(d);
 
188
    modifier.close();
 
189
 
 
190
    modifier = new IndexWriter(
 
191
        dir2,
 
192
        newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).
 
193
            setMergePolicy(newLogMergePolicy(10))
 
194
    );
 
195
    d = new Document();
 
196
    d.add(newField("f2", "v2", Field.Store.YES, Field.Index.ANALYZED));
 
197
    modifier.addDocument(d);
 
198
    modifier.close();
 
199
 
 
200
    
 
201
    ParallelReader pr = new ParallelReader();
 
202
    pr.add(IndexReader.open(dir1, false));
 
203
    pr.add(IndexReader.open(dir2, false));
 
204
    assertFalse(pr.isOptimized());
 
205
    pr.close();
 
206
    
 
207
    modifier = new IndexWriter(dir1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
 
208
    modifier.optimize();
 
209
    modifier.close();
 
210
    
 
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());
 
216
    pr.close();
 
217
 
 
218
    
 
219
    modifier = new IndexWriter(dir2, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
 
220
    modifier.optimize();
 
221
    modifier.close();
 
222
    
 
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());
 
228
    pr.close();
 
229
    dir1.close();
 
230
    dir2.close();
 
231
  }
 
232
 
 
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));
 
239
    int NUM_DOCS = 2;
 
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());
 
245
    }
 
246
    td.close();
 
247
    pr.close();
 
248
    dir1.close();
 
249
    dir2.close();
 
250
  }
 
251
    
 
252
  
 
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"));
 
265
    }
 
266
  }
 
267
 
 
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));
 
277
    w.addDocument(d1);
 
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));
 
283
    w.addDocument(d2);
 
284
    w.close();
 
285
 
 
286
    return new IndexSearcher(dir, false);
 
287
  }
 
288
 
 
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);
 
297
  }
 
298
 
 
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));
 
305
    w1.addDocument(d1);
 
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));
 
309
    w1.addDocument(d2);
 
310
    w1.close();
 
311
    return dir1;
 
312
  }
 
313
 
 
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));
 
320
    w2.addDocument(d3);
 
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));
 
324
    w2.addDocument(d4);
 
325
    w2.close();
 
326
    return dir2;
 
327
  }
 
328
 
 
329
}