pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestDocumentWriter.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 import java.io.Reader;
22
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.MockAnalyzer;
25 import org.apache.lucene.analysis.TokenFilter;
26 import org.apache.lucene.analysis.TokenStream;
27 import org.apache.lucene.analysis.WhitespaceTokenizer;
28 import org.apache.lucene.analysis.tokenattributes.PayloadAttribute;
29 import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
30 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
31 import org.apache.lucene.document.Document;
32 import org.apache.lucene.document.Field;
33 import org.apache.lucene.document.Fieldable;
34 import org.apache.lucene.document.Field.Index;
35 import org.apache.lucene.document.Field.Store;
36 import org.apache.lucene.document.Field.TermVector;
37 import org.apache.lucene.index.FieldInfo.IndexOptions;
38 import org.apache.lucene.store.Directory;
39 import org.apache.lucene.util.AttributeSource;
40 import org.apache.lucene.util.LuceneTestCase;
41 import org.apache.lucene.util._TestUtil;
42
43 public class TestDocumentWriter extends LuceneTestCase {
44   private Directory dir;
45
46   @Override
47   public void setUp() throws Exception {
48     super.setUp();
49     dir = newDirectory();
50   }
51   
52   @Override
53   public void tearDown() throws Exception {
54     dir.close();
55     super.tearDown();
56   }
57
58   public void test() {
59     assertTrue(dir != null);
60   }
61
62   public void testAddDocument() throws Exception {
63     Document testDoc = new Document();
64     DocHelper.setupDoc(testDoc);
65     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
66     writer.addDocument(testDoc);
67     writer.commit();
68     SegmentInfo info = writer.newestSegment();
69     writer.close();
70     //After adding the document, we should be able to read it back in
71     SegmentReader reader = SegmentReader.get(true, info, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
72     assertTrue(reader != null);
73     Document doc = reader.document(0);
74     assertTrue(doc != null);
75
76     //System.out.println("Document: " + doc);
77     Fieldable [] fields = doc.getFields("textField2");
78     assertTrue(fields != null && fields.length == 1);
79     assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_2_TEXT));
80     assertTrue(fields[0].isTermVectorStored());
81
82     fields = doc.getFields("textField1");
83     assertTrue(fields != null && fields.length == 1);
84     assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_1_TEXT));
85     assertFalse(fields[0].isTermVectorStored());
86
87     fields = doc.getFields("keyField");
88     assertTrue(fields != null && fields.length == 1);
89     assertTrue(fields[0].stringValue().equals(DocHelper.KEYWORD_TEXT));
90
91     fields = doc.getFields(DocHelper.NO_NORMS_KEY);
92     assertTrue(fields != null && fields.length == 1);
93     assertTrue(fields[0].stringValue().equals(DocHelper.NO_NORMS_TEXT));
94
95     fields = doc.getFields(DocHelper.TEXT_FIELD_3_KEY);
96     assertTrue(fields != null && fields.length == 1);
97     assertTrue(fields[0].stringValue().equals(DocHelper.FIELD_3_TEXT));
98
99     // test that the norms are not present in the segment if
100     // omitNorms is true
101     for (int i = 0; i < reader.core.fieldInfos.size(); i++) {
102       FieldInfo fi = reader.core.fieldInfos.fieldInfo(i);
103       if (fi.isIndexed) {
104         assertTrue(fi.omitNorms == !reader.hasNorms(fi.name));
105       }
106     }
107     reader.close();
108   }
109
110   public void testPositionIncrementGap() throws IOException {
111     Analyzer analyzer = new Analyzer() {
112       @Override
113       public TokenStream tokenStream(String fieldName, Reader reader) {
114         return new WhitespaceTokenizer(TEST_VERSION_CURRENT, reader);
115       }
116
117       @Override
118       public int getPositionIncrementGap(String fieldName) {
119         return 500;
120       }
121     };
122
123     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
124
125     Document doc = new Document();
126     doc.add(newField("repeated", "repeated one", Field.Store.YES, Field.Index.ANALYZED));
127     doc.add(newField("repeated", "repeated two", Field.Store.YES, Field.Index.ANALYZED));
128
129     writer.addDocument(doc);
130     writer.commit();
131     SegmentInfo info = writer.newestSegment();
132     writer.close();
133     SegmentReader reader = SegmentReader.get(true, info, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
134
135     TermPositions termPositions = reader.termPositions(new Term("repeated", "repeated"));
136     assertTrue(termPositions.next());
137     int freq = termPositions.freq();
138     assertEquals(2, freq);
139     assertEquals(0, termPositions.nextPosition());
140     assertEquals(502, termPositions.nextPosition());
141     reader.close();
142   }
143
144   public void testTokenReuse() throws IOException {
145     Analyzer analyzer = new Analyzer() {
146       @Override
147       public TokenStream tokenStream(String fieldName, Reader reader) {
148         return new TokenFilter(new WhitespaceTokenizer(TEST_VERSION_CURRENT, reader)) {
149           boolean first = true;
150           AttributeSource.State state;
151
152           @Override
153           public boolean incrementToken() throws IOException {
154             if (state != null) {
155               restoreState(state);
156               payloadAtt.setPayload(null);
157               posIncrAtt.setPositionIncrement(0);
158               termAtt.setEmpty().append("b");
159               state = null;
160               return true;
161             }
162
163             boolean hasNext = input.incrementToken();
164             if (!hasNext) return false;
165             if (Character.isDigit(termAtt.buffer()[0])) {
166               posIncrAtt.setPositionIncrement(termAtt.buffer()[0] - '0');
167             }
168             if (first) {
169               // set payload on first position only
170               payloadAtt.setPayload(new Payload(new byte[]{100}));
171               first = false;
172             }
173
174             // index a "synonym" for every token
175             state = captureState();
176             return true;
177
178           }
179
180           @Override
181           public void reset() throws IOException {
182             super.reset();
183             first = true;
184             state = null;
185           }
186
187           final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
188           final PayloadAttribute payloadAtt = addAttribute(PayloadAttribute.class);
189           final PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
190         };
191       }
192     };
193
194     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
195
196     Document doc = new Document();
197     doc.add(newField("f1", "a 5 a a", Field.Store.YES, Field.Index.ANALYZED));
198
199     writer.addDocument(doc);
200     writer.commit();
201     SegmentInfo info = writer.newestSegment();
202     writer.close();
203     SegmentReader reader = SegmentReader.get(true, info, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
204
205     TermPositions termPositions = reader.termPositions(new Term("f1", "a"));
206     assertTrue(termPositions.next());
207     int freq = termPositions.freq();
208     assertEquals(3, freq);
209     assertEquals(0, termPositions.nextPosition());
210     assertEquals(true, termPositions.isPayloadAvailable());
211     assertEquals(6, termPositions.nextPosition());
212     assertEquals(false, termPositions.isPayloadAvailable());
213     assertEquals(7, termPositions.nextPosition());
214     assertEquals(false, termPositions.isPayloadAvailable());
215     reader.close();
216   }
217
218
219   public void testPreAnalyzedField() throws IOException {
220     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
221         TEST_VERSION_CURRENT, new MockAnalyzer(random)));
222     Document doc = new Document();
223     
224     doc.add(new Field("preanalyzed", new TokenStream() {
225       private String[] tokens = new String[] {"term1", "term2", "term3", "term2"};
226       private int index = 0;
227       
228       private CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
229       
230       @Override
231       public boolean incrementToken() throws IOException {
232         if (index == tokens.length) {
233           return false;
234         } else {
235           clearAttributes();
236           termAtt.setEmpty().append(tokens[index++]);
237           return true;
238         }        
239       }
240       
241     }, TermVector.NO));
242     
243     writer.addDocument(doc);
244     writer.commit();
245     SegmentInfo info = writer.newestSegment();
246     writer.close();
247     SegmentReader reader = SegmentReader.get(true, info, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
248
249     TermPositions termPositions = reader.termPositions(new Term("preanalyzed", "term1"));
250     assertTrue(termPositions.next());
251     assertEquals(1, termPositions.freq());
252     assertEquals(0, termPositions.nextPosition());
253
254     termPositions.seek(new Term("preanalyzed", "term2"));
255     assertTrue(termPositions.next());
256     assertEquals(2, termPositions.freq());
257     assertEquals(1, termPositions.nextPosition());
258     assertEquals(3, termPositions.nextPosition());
259     
260     termPositions.seek(new Term("preanalyzed", "term3"));
261     assertTrue(termPositions.next());
262     assertEquals(1, termPositions.freq());
263     assertEquals(2, termPositions.nextPosition());
264     reader.close();
265   }
266
267   /**
268    * Test adding two fields with the same name, but 
269    * with different term vector setting (LUCENE-766).
270    */
271   public void testMixedTermVectorSettingsSameField() throws Exception {
272     Document doc = new Document();
273     // f1 first without tv then with tv
274     doc.add(newField("f1", "v1", Store.YES, Index.NOT_ANALYZED, TermVector.NO));
275     doc.add(newField("f1", "v2", Store.YES, Index.NOT_ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
276     // f2 first with tv then without tv
277     doc.add(newField("f2", "v1", Store.YES, Index.NOT_ANALYZED, TermVector.WITH_POSITIONS_OFFSETS));
278     doc.add(newField("f2", "v2", Store.YES, Index.NOT_ANALYZED, TermVector.NO));
279
280     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
281         TEST_VERSION_CURRENT, new MockAnalyzer(random)));
282     writer.addDocument(doc);
283     writer.close();
284
285     _TestUtil.checkIndex(dir);
286
287     IndexReader reader = IndexReader.open(dir, true);
288     // f1
289     TermFreqVector tfv1 = reader.getTermFreqVector(0, "f1");
290     assertNotNull(tfv1);
291     assertEquals("the 'with_tv' setting should rule!",2,tfv1.getTerms().length);
292     // f2
293     TermFreqVector tfv2 = reader.getTermFreqVector(0, "f2");
294     assertNotNull(tfv2);
295     assertEquals("the 'with_tv' setting should rule!",2,tfv2.getTerms().length);
296     reader.close();
297   }
298
299   /**
300    * Test adding two fields with the same name, one indexed
301    * the other stored only. The omitNorms and omitTermFreqAndPositions setting
302    * of the stored field should not affect the indexed one (LUCENE-1590)
303    */
304   public void testLUCENE_1590() throws Exception {
305     Document doc = new Document();
306     // f1 has no norms
307     doc.add(newField("f1", "v1", Store.NO, Index.ANALYZED_NO_NORMS));
308     doc.add(newField("f1", "v2", Store.YES, Index.NO));
309     // f2 has no TF
310     Field f = newField("f2", "v1", Store.NO, Index.ANALYZED);
311     f.setIndexOptions(IndexOptions.DOCS_ONLY);
312     doc.add(f);
313     doc.add(newField("f2", "v2", Store.YES, Index.NO));
314
315     IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
316         TEST_VERSION_CURRENT, new MockAnalyzer(random)));
317     writer.addDocument(doc);
318     writer.forceMerge(1); // be sure to have a single segment
319     writer.close();
320
321     _TestUtil.checkIndex(dir);
322
323     SegmentReader reader = SegmentReader.getOnlySegmentReader(dir);
324     FieldInfos fi = reader.fieldInfos();
325     // f1
326     assertFalse("f1 should have no norms", reader.hasNorms("f1"));
327     assertEquals("omitTermFreqAndPositions field bit should not be set for f1", IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, fi.fieldInfo("f1").indexOptions);
328     // f2
329     assertTrue("f2 should have norms", reader.hasNorms("f2"));
330     assertEquals("omitTermFreqAndPositions field bit should be set for f2", IndexOptions.DOCS_ONLY, fi.fieldInfo("f2").indexOptions);
331     reader.close();
332   }
333 }