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;
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;
30
public class TestSegmentTermEnum extends LuceneTestCase {
35
public void setUp() throws Exception {
41
public void tearDown() throws Exception {
46
public void testTermEnum() throws IOException {
47
IndexWriter writer = null;
49
writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
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");
61
// verify document frequency of terms in an multi segment index
65
writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
69
// verify document frequency of terms in a single segment index
73
public void testPrevTermAtEnd() throws IOException
75
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
76
addDoc(writer, "aaa bbb");
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());
90
private void verifyDocFreq()
93
IndexReader reader = IndexReader.open(dir, true);
94
TermEnum termEnum = null;
96
// create enumeration of all terms
97
termEnum = reader.terms();
98
// go to the first term (aaa)
100
// assert that term is 'aaa'
101
assertEquals("aaa", termEnum.term().text());
102
assertEquals(200, termEnum.docFreq());
103
// go to the second term (bbb)
105
// assert that term is 'bbb'
106
assertEquals("bbb", termEnum.term().text());
107
assertEquals(100, termEnum.docFreq());
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());
119
// assert that term is 'bbb'
120
assertEquals("bbb", termEnum.term().text());
121
assertEquals(100, termEnum.docFreq());
126
private void addDoc(IndexWriter writer, String value) throws IOException
128
Document doc = new Document();
129
doc.add(newField("content", value, Field.Store.NO, Field.Index.ANALYZED));
130
writer.addDocument(doc);