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 org.apache.lucene.util.LuceneTestCase;
21 import org.apache.lucene.store.Directory;
22 import org.apache.lucene.analysis.MockAnalyzer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
26 import java.io.IOException;
28 public class TestSegmentTermDocs extends LuceneTestCase {
29 private Document testDoc = new Document();
30 private Directory dir;
31 private SegmentInfo info;
34 public void setUp() throws Exception {
37 DocHelper.setupDoc(testDoc);
38 info = DocHelper.writeDoc(random, dir, testDoc);
42 public void tearDown() throws Exception {
48 assertTrue(dir != null);
51 public void testTermDocs() throws IOException {
55 public void testTermDocs(int indexDivisor) throws IOException {
56 //After adding the document, we should be able to read it back in
57 SegmentReader reader = SegmentReader.get(true, info, indexDivisor);
58 assertTrue(reader != null);
59 assertEquals(indexDivisor, reader.getTermInfosIndexDivisor());
60 SegmentTermDocs segTermDocs = new SegmentTermDocs(reader);
61 segTermDocs.seek(new Term(DocHelper.TEXT_FIELD_2_KEY, "field"));
62 if (segTermDocs.next() == true)
64 int docId = segTermDocs.doc();
65 assertTrue(docId == 0);
66 int freq = segTermDocs.freq();
67 assertTrue(freq == 3);
72 public void testBadSeek() throws IOException {
76 public void testBadSeek(int indexDivisor) throws IOException {
78 //After adding the document, we should be able to read it back in
79 SegmentReader reader = SegmentReader.get(true, info, indexDivisor);
80 assertTrue(reader != null);
81 SegmentTermDocs segTermDocs = new SegmentTermDocs(reader);
82 segTermDocs.seek(new Term("textField2", "bad"));
83 assertTrue(segTermDocs.next() == false);
87 //After adding the document, we should be able to read it back in
88 SegmentReader reader = SegmentReader.get(true, info, indexDivisor);
89 assertTrue(reader != null);
90 SegmentTermDocs segTermDocs = new SegmentTermDocs(reader);
91 segTermDocs.seek(new Term("junk", "bad"));
92 assertTrue(segTermDocs.next() == false);
97 public void testSkipTo() throws IOException {
101 public void testSkipTo(int indexDivisor) throws IOException {
102 Directory dir = newDirectory();
103 IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
105 Term ta = new Term("content","aaa");
106 for(int i = 0; i < 10; i++)
107 addDoc(writer, "aaa aaa aaa aaa");
109 Term tb = new Term("content","bbb");
110 for(int i = 0; i < 16; i++)
111 addDoc(writer, "bbb bbb bbb bbb");
113 Term tc = new Term("content","ccc");
114 for(int i = 0; i < 50; i++)
115 addDoc(writer, "ccc ccc ccc ccc");
117 // assure that we deal with a single segment
121 IndexReader reader = IndexReader.open(dir, null, true, indexDivisor);
123 TermDocs tdocs = reader.termDocs();
125 // without optimization (assumption skipInterval == 16)
129 assertTrue(tdocs.next());
130 assertEquals(0, tdocs.doc());
131 assertEquals(4, tdocs.freq());
132 assertTrue(tdocs.next());
133 assertEquals(1, tdocs.doc());
134 assertEquals(4, tdocs.freq());
135 assertTrue(tdocs.skipTo(0));
136 assertEquals(2, tdocs.doc());
137 assertTrue(tdocs.skipTo(4));
138 assertEquals(4, tdocs.doc());
139 assertTrue(tdocs.skipTo(9));
140 assertEquals(9, tdocs.doc());
141 assertFalse(tdocs.skipTo(10));
145 assertTrue(tdocs.skipTo(0));
146 assertEquals(0, tdocs.doc());
147 assertTrue(tdocs.skipTo(4));
148 assertEquals(4, tdocs.doc());
149 assertTrue(tdocs.skipTo(9));
150 assertEquals(9, tdocs.doc());
151 assertFalse(tdocs.skipTo(10));
153 // exactly skipInterval documents and therefore with optimization
157 assertTrue(tdocs.next());
158 assertEquals(10, tdocs.doc());
159 assertEquals(4, tdocs.freq());
160 assertTrue(tdocs.next());
161 assertEquals(11, tdocs.doc());
162 assertEquals(4, tdocs.freq());
163 assertTrue(tdocs.skipTo(5));
164 assertEquals(12, tdocs.doc());
165 assertTrue(tdocs.skipTo(15));
166 assertEquals(15, tdocs.doc());
167 assertTrue(tdocs.skipTo(24));
168 assertEquals(24, tdocs.doc());
169 assertTrue(tdocs.skipTo(25));
170 assertEquals(25, tdocs.doc());
171 assertFalse(tdocs.skipTo(26));
175 assertTrue(tdocs.skipTo(5));
176 assertEquals(10, tdocs.doc());
177 assertTrue(tdocs.skipTo(15));
178 assertEquals(15, tdocs.doc());
179 assertTrue(tdocs.skipTo(24));
180 assertEquals(24, tdocs.doc());
181 assertTrue(tdocs.skipTo(25));
182 assertEquals(25, tdocs.doc());
183 assertFalse(tdocs.skipTo(26));
185 // much more than skipInterval documents and therefore with optimization
189 assertTrue(tdocs.next());
190 assertEquals(26, tdocs.doc());
191 assertEquals(4, tdocs.freq());
192 assertTrue(tdocs.next());
193 assertEquals(27, tdocs.doc());
194 assertEquals(4, tdocs.freq());
195 assertTrue(tdocs.skipTo(5));
196 assertEquals(28, tdocs.doc());
197 assertTrue(tdocs.skipTo(40));
198 assertEquals(40, tdocs.doc());
199 assertTrue(tdocs.skipTo(57));
200 assertEquals(57, tdocs.doc());
201 assertTrue(tdocs.skipTo(74));
202 assertEquals(74, tdocs.doc());
203 assertTrue(tdocs.skipTo(75));
204 assertEquals(75, tdocs.doc());
205 assertFalse(tdocs.skipTo(76));
209 assertTrue(tdocs.skipTo(5));
210 assertEquals(26, tdocs.doc());
211 assertTrue(tdocs.skipTo(40));
212 assertEquals(40, tdocs.doc());
213 assertTrue(tdocs.skipTo(57));
214 assertEquals(57, tdocs.doc());
215 assertTrue(tdocs.skipTo(74));
216 assertEquals(74, tdocs.doc());
217 assertTrue(tdocs.skipTo(75));
218 assertEquals(75, tdocs.doc());
219 assertFalse(tdocs.skipTo(76));
226 public void testIndexDivisor() throws IOException {
227 testDoc = new Document();
228 DocHelper.setupDoc(testDoc);
229 DocHelper.writeDoc(random, dir, testDoc);
235 private void addDoc(IndexWriter writer, String value) throws IOException
237 Document doc = new Document();
238 doc.add(newField("content", value, Field.Store.NO, Field.Index.ANALYZED));
239 writer.addDocument(doc);