KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > TestSegmentTermDocs


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import junit.framework.TestCase;
20 import org.apache.lucene.store.RAMDirectory;
21 import org.apache.lucene.store.Directory;
22 import org.apache.lucene.analysis.Analyzer;
23 import org.apache.lucene.analysis.WhitespaceAnalyzer;
24 import org.apache.lucene.search.Similarity;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27
28 import java.io.IOException JavaDoc;
29
30 public class TestSegmentTermDocs extends TestCase {
31   private Document testDoc = new Document();
32   private Directory dir = new RAMDirectory();
33
34   public TestSegmentTermDocs(String JavaDoc s) {
35     super(s);
36   }
37
38   protected void setUp() {
39     DocHelper.setupDoc(testDoc);
40     DocHelper.writeDoc(dir, testDoc);
41   }
42
43
44   protected void tearDown() {
45
46   }
47
48   public void test() {
49     assertTrue(dir != null);
50   }
51   
52   public void testTermDocs() {
53     try {
54       //After adding the document, we should be able to read it back in
55
SegmentReader reader = new SegmentReader(new SegmentInfo("test", 1, dir));
56       assertTrue(reader != null);
57       SegmentTermDocs segTermDocs = new SegmentTermDocs(reader);
58       assertTrue(segTermDocs != null);
59       segTermDocs.seek(new Term(DocHelper.TEXT_FIELD_2_KEY, "field"));
60       if (segTermDocs.next() == true)
61       {
62         int docId = segTermDocs.doc();
63         assertTrue(docId == 0);
64         int freq = segTermDocs.freq();
65         assertTrue(freq == 3);
66       }
67       reader.close();
68     } catch (IOException JavaDoc e) {
69       assertTrue(false);
70     }
71   }
72   
73   public void testBadSeek() {
74     try {
75       //After adding the document, we should be able to read it back in
76
SegmentReader reader = new SegmentReader(new SegmentInfo("test", 3, dir));
77       assertTrue(reader != null);
78       SegmentTermDocs segTermDocs = new SegmentTermDocs(reader);
79       assertTrue(segTermDocs != null);
80       segTermDocs.seek(new Term("textField2", "bad"));
81       assertTrue(segTermDocs.next() == false);
82       reader.close();
83     } catch (IOException JavaDoc e) {
84       assertTrue(false);
85     }
86     try {
87       //After adding the document, we should be able to read it back in
88
SegmentReader reader = new SegmentReader(new SegmentInfo("test", 3, dir));
89       assertTrue(reader != null);
90       SegmentTermDocs segTermDocs = new SegmentTermDocs(reader);
91       assertTrue(segTermDocs != null);
92       segTermDocs.seek(new Term("junk", "bad"));
93       assertTrue(segTermDocs.next() == false);
94       reader.close();
95     } catch (IOException JavaDoc e) {
96       assertTrue(false);
97     }
98   }
99   
100   public void testSkipTo() {
101     try {
102       Directory dir = new RAMDirectory();
103       IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
104       
105       Term ta = new Term("content","aaa");
106       for(int i = 0; i < 10; i++)
107         addDoc(writer, "aaa aaa aaa aaa");
108         
109       Term tb = new Term("content","bbb");
110       for(int i = 0; i < 16; i++)
111         addDoc(writer, "bbb bbb bbb bbb");
112         
113       Term tc = new Term("content","ccc");
114       for(int i = 0; i < 50; i++)
115         addDoc(writer, "ccc ccc ccc ccc");
116         
117       // assure that we deal with a single segment
118
writer.optimize();
119       writer.close();
120       
121       IndexReader reader = IndexReader.open(dir);
122       TermDocs tdocs = reader.termDocs();
123       
124       // without optimization (assumption skipInterval == 16)
125

126       // with next
127
tdocs.seek(ta);
128       assertTrue(tdocs.next());
129       assertEquals(0, tdocs.doc());
130       assertEquals(4, tdocs.freq());
131       assertTrue(tdocs.next());
132       assertEquals(1, tdocs.doc());
133       assertEquals(4, tdocs.freq());
134       assertTrue(tdocs.skipTo(0));
135       assertEquals(2, tdocs.doc());
136       assertTrue(tdocs.skipTo(4));
137       assertEquals(4, tdocs.doc());
138       assertTrue(tdocs.skipTo(9));
139       assertEquals(9, tdocs.doc());
140       assertFalse(tdocs.skipTo(10));
141       
142       // without next
143
tdocs.seek(ta);
144       assertTrue(tdocs.skipTo(0));
145       assertEquals(0, tdocs.doc());
146       assertTrue(tdocs.skipTo(4));
147       assertEquals(4, tdocs.doc());
148       assertTrue(tdocs.skipTo(9));
149       assertEquals(9, tdocs.doc());
150       assertFalse(tdocs.skipTo(10));
151       
152       // exactly skipInterval documents and therefore with optimization
153

154       // with next
155
tdocs.seek(tb);
156       assertTrue(tdocs.next());
157       assertEquals(10, tdocs.doc());
158       assertEquals(4, tdocs.freq());
159       assertTrue(tdocs.next());
160       assertEquals(11, tdocs.doc());
161       assertEquals(4, tdocs.freq());
162       assertTrue(tdocs.skipTo(5));
163       assertEquals(12, tdocs.doc());
164       assertTrue(tdocs.skipTo(15));
165       assertEquals(15, tdocs.doc());
166       assertTrue(tdocs.skipTo(24));
167       assertEquals(24, tdocs.doc());
168       assertTrue(tdocs.skipTo(25));
169       assertEquals(25, tdocs.doc());
170       assertFalse(tdocs.skipTo(26));
171       
172       // without next
173
tdocs.seek(tb);
174       assertTrue(tdocs.skipTo(5));
175       assertEquals(10, tdocs.doc());
176       assertTrue(tdocs.skipTo(15));
177       assertEquals(15, tdocs.doc());
178       assertTrue(tdocs.skipTo(24));
179       assertEquals(24, tdocs.doc());
180       assertTrue(tdocs.skipTo(25));
181       assertEquals(25, tdocs.doc());
182       assertFalse(tdocs.skipTo(26));
183       
184       // much more than skipInterval documents and therefore with optimization
185

186       // with next
187
tdocs.seek(tc);
188       assertTrue(tdocs.next());
189       assertEquals(26, tdocs.doc());
190       assertEquals(4, tdocs.freq());
191       assertTrue(tdocs.next());
192       assertEquals(27, tdocs.doc());
193       assertEquals(4, tdocs.freq());
194       assertTrue(tdocs.skipTo(5));
195       assertEquals(28, tdocs.doc());
196       assertTrue(tdocs.skipTo(40));
197       assertEquals(40, tdocs.doc());
198       assertTrue(tdocs.skipTo(57));
199       assertEquals(57, tdocs.doc());
200       assertTrue(tdocs.skipTo(74));
201       assertEquals(74, tdocs.doc());
202       assertTrue(tdocs.skipTo(75));
203       assertEquals(75, tdocs.doc());
204       assertFalse(tdocs.skipTo(76));
205       
206       //without next
207
tdocs.seek(tc);
208       assertTrue(tdocs.skipTo(5));
209       assertEquals(26, tdocs.doc());
210       assertTrue(tdocs.skipTo(40));
211       assertEquals(40, tdocs.doc());
212       assertTrue(tdocs.skipTo(57));
213       assertEquals(57, tdocs.doc());
214       assertTrue(tdocs.skipTo(74));
215       assertEquals(74, tdocs.doc());
216       assertTrue(tdocs.skipTo(75));
217       assertEquals(75, tdocs.doc());
218       assertFalse(tdocs.skipTo(76));
219       
220       tdocs.close();
221       reader.close();
222       dir.close();
223     } catch (IOException JavaDoc e) {
224         assertTrue(false);
225     }
226   }
227   
228   private void addDoc(IndexWriter writer, String JavaDoc value) throws IOException JavaDoc
229   {
230       Document doc = new Document();
231       doc.add(Field.UnStored("content", value));
232       writer.addDocument(doc);
233   }
234 }
235
Popular Tags