1
package org.apache.lucene.index;
3
* Copyright 2006 The Apache Software Foundation
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
import org.apache.lucene.store.Directory;
19
import org.apache.lucene.store.MockRAMDirectory;
20
import org.apache.lucene.analysis.standard.StandardAnalyzer;
21
import org.apache.lucene.document.Document;
22
import org.apache.lucene.document.Field;
23
import org.apache.lucene.util.LuceneTestCase;
25
import java.io.IOException;
28
public class TestIndexWriterMerging extends LuceneTestCase
32
* Tests that index merging (specifically addIndexes()) doesn't
33
* change the index order of documents.
35
public void testLucene() throws IOException
40
Directory indexA = new MockRAMDirectory();
41
Directory indexB = new MockRAMDirectory();
43
fillIndex(indexA, 0, num);
44
boolean fail = verifyIndex(indexA, 0);
47
fail("Index a is invalid");
50
fillIndex(indexB, num, num);
51
fail = verifyIndex(indexB, num);
54
fail("Index b is invalid");
57
Directory merged = new MockRAMDirectory();
59
IndexWriter writer = new IndexWriter(merged, new StandardAnalyzer(), true);
60
writer.setMergeFactor(2);
62
writer.addIndexes(new Directory[]{indexA, indexB});
65
fail = verifyIndex(merged, 0);
68
assertFalse("The merged index is invalid", fail);
71
private boolean verifyIndex(Directory directory, int startAt) throws IOException
74
IndexReader reader = IndexReader.open(directory);
76
int max = reader.maxDoc();
77
for (int i = 0; i < max; i++)
79
Document temp = reader.document(i);
80
//System.out.println("doc "+i+"="+temp.getField("count").stringValue());
81
//compare the index doc number to the value that it should be
82
if (!temp.getField("count").stringValue().equals((i + startAt) + ""))
85
System.out.println("Document " + (i + startAt) + " is returning document " + temp.getField("count").stringValue());
92
private void fillIndex(Directory dir, int start, int numDocs) throws IOException
95
IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
96
writer.setMergeFactor(2);
97
writer.setMaxBufferedDocs(2);
99
for (int i = start; i < (start + numDocs); i++)
101
Document temp = new Document();
102
temp.add(new Field("count", (""+i), Field.Store.YES, Field.Index.UN_TOKENIZED));
104
writer.addDocument(temp);