pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / index / TestSegmentTermEnum.java
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
22 import org.apache.lucene.util.LuceneTestCase;
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
27 import org.apache.lucene.store.Directory;
28
29
30 public class TestSegmentTermEnum extends LuceneTestCase {
31   
32   Directory dir;
33   
34   @Override
35   public void setUp() throws Exception {
36     super.setUp();
37     dir = newDirectory();
38   }
39   
40   @Override
41   public void tearDown() throws Exception {
42     dir.close();
43     super.tearDown();
44   }
45
46   public void testTermEnum() throws IOException {
47     IndexWriter writer = null;
48
49     writer  = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
50
51     // ADD 100 documents with term : aaa
52     // add 100 documents with terms: aaa bbb
53     // Therefore, term 'aaa' has document frequency of 200 and term 'bbb' 100
54     for (int i = 0; i < 100; i++) {
55       addDoc(writer, "aaa");
56       addDoc(writer, "aaa bbb");
57     }
58
59     writer.close();
60
61     // verify document frequency of terms in an unoptimized index
62     verifyDocFreq();
63
64     // merge segments by optimizing the index
65     writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
66     writer.optimize();
67     writer.close();
68
69     // verify document frequency of terms in an optimized index
70     verifyDocFreq();
71   }
72
73   public void testPrevTermAtEnd() throws IOException
74   {
75     IndexWriter writer  = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
76     addDoc(writer, "aaa bbb");
77     writer.close();
78     SegmentReader reader = SegmentReader.getOnlySegmentReader(dir);
79     SegmentTermEnum termEnum = (SegmentTermEnum) reader.terms();
80     assertTrue(termEnum.next());
81     assertEquals("aaa", termEnum.term().text());
82     assertTrue(termEnum.next());
83     assertEquals("aaa", termEnum.prev().text());
84     assertEquals("bbb", termEnum.term().text());
85     assertFalse(termEnum.next());
86     assertEquals("bbb", termEnum.prev().text());
87     reader.close();
88   }
89
90   private void verifyDocFreq()
91       throws IOException
92   {
93       IndexReader reader = IndexReader.open(dir, true);
94       TermEnum termEnum = null;
95
96     // create enumeration of all terms
97     termEnum = reader.terms();
98     // go to the first term (aaa)
99     termEnum.next();
100     // assert that term is 'aaa'
101     assertEquals("aaa", termEnum.term().text());
102     assertEquals(200, termEnum.docFreq());
103     // go to the second term (bbb)
104     termEnum.next();
105     // assert that term is 'bbb'
106     assertEquals("bbb", termEnum.term().text());
107     assertEquals(100, termEnum.docFreq());
108
109     termEnum.close();
110
111
112     // create enumeration of terms after term 'aaa', including 'aaa'
113     termEnum = reader.terms(new Term("content", "aaa"));
114     // assert that term is 'aaa'
115     assertEquals("aaa", termEnum.term().text());
116     assertEquals(200, termEnum.docFreq());
117     // go to term 'bbb'
118     termEnum.next();
119     // assert that term is 'bbb'
120     assertEquals("bbb", termEnum.term().text());
121     assertEquals(100, termEnum.docFreq());
122     termEnum.close();
123     reader.close();
124   }
125
126   private void addDoc(IndexWriter writer, String value) throws IOException
127   {
128     Document doc = new Document();
129     doc.add(newField("content", value, Field.Store.NO, Field.Index.ANALYZED));
130     writer.addDocument(doc);
131   }
132 }