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;
21
import java.io.Reader;
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;
43
public class TestDocumentWriter extends LuceneTestCase {
44
private Directory dir;
47
public void setUp() throws Exception {
53
public void tearDown() throws Exception {
59
assertTrue(dir != null);
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);
68
SegmentInfo info = writer.newestSegment();
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);
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());
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());
87
fields = doc.getFields("keyField");
88
assertTrue(fields != null && fields.length == 1);
89
assertTrue(fields[0].stringValue().equals(DocHelper.KEYWORD_TEXT));
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));
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));
99
// test that the norms are not present in the segment if
101
for (int i = 0; i < reader.core.fieldInfos.size(); i++) {
102
FieldInfo fi = reader.core.fieldInfos.fieldInfo(i);
104
assertTrue(fi.omitNorms == !reader.hasNorms(fi.name));
110
public void testPositionIncrementGap() throws IOException {
111
Analyzer analyzer = new Analyzer() {
113
public TokenStream tokenStream(String fieldName, Reader reader) {
114
return new WhitespaceTokenizer(TEST_VERSION_CURRENT, reader);
118
public int getPositionIncrementGap(String fieldName) {
123
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
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));
129
writer.addDocument(doc);
131
SegmentInfo info = writer.newestSegment();
133
SegmentReader reader = SegmentReader.get(true, info, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
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());
144
public void testTokenReuse() throws IOException {
145
Analyzer analyzer = new Analyzer() {
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;
153
public boolean incrementToken() throws IOException {
156
payloadAtt.setPayload(null);
157
posIncrAtt.setPositionIncrement(0);
158
termAtt.setEmpty().append("b");
163
boolean hasNext = input.incrementToken();
164
if (!hasNext) return false;
165
if (Character.isDigit(termAtt.buffer()[0])) {
166
posIncrAtt.setPositionIncrement(termAtt.buffer()[0] - '0');
169
// set payload on first position only
170
payloadAtt.setPayload(new Payload(new byte[]{100}));
174
// index a "synonym" for every token
175
state = captureState();
181
public void reset() throws IOException {
187
final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
188
final PayloadAttribute payloadAtt = addAttribute(PayloadAttribute.class);
189
final PositionIncrementAttribute posIncrAtt = addAttribute(PositionIncrementAttribute.class);
194
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer));
196
Document doc = new Document();
197
doc.add(newField("f1", "a 5 a a", Field.Store.YES, Field.Index.ANALYZED));
199
writer.addDocument(doc);
201
SegmentInfo info = writer.newestSegment();
203
SegmentReader reader = SegmentReader.get(true, info, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
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());
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();
224
doc.add(new Field("preanalyzed", new TokenStream() {
225
private String[] tokens = new String[] {"term1", "term2", "term3", "term2"};
226
private int index = 0;
228
private CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
231
public boolean incrementToken() throws IOException {
232
if (index == tokens.length) {
236
termAtt.setEmpty().append(tokens[index++]);
243
writer.addDocument(doc);
245
SegmentInfo info = writer.newestSegment();
247
SegmentReader reader = SegmentReader.get(true, info, IndexReader.DEFAULT_TERMS_INDEX_DIVISOR);
249
TermPositions termPositions = reader.termPositions(new Term("preanalyzed", "term1"));
250
assertTrue(termPositions.next());
251
assertEquals(1, termPositions.freq());
252
assertEquals(0, termPositions.nextPosition());
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());
260
termPositions.seek(new Term("preanalyzed", "term3"));
261
assertTrue(termPositions.next());
262
assertEquals(1, termPositions.freq());
263
assertEquals(2, termPositions.nextPosition());
268
* Test adding two fields with the same name, but
269
* with different term vector setting (LUCENE-766).
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));
280
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
281
TEST_VERSION_CURRENT, new MockAnalyzer(random)));
282
writer.addDocument(doc);
285
_TestUtil.checkIndex(dir);
287
IndexReader reader = IndexReader.open(dir, true);
289
TermFreqVector tfv1 = reader.getTermFreqVector(0, "f1");
291
assertEquals("the 'with_tv' setting should rule!",2,tfv1.getTerms().length);
293
TermFreqVector tfv2 = reader.getTermFreqVector(0, "f2");
295
assertEquals("the 'with_tv' setting should rule!",2,tfv2.getTerms().length);
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)
304
public void testLUCENE_1590() throws Exception {
305
Document doc = new Document();
307
doc.add(newField("f1", "v1", Store.NO, Index.ANALYZED_NO_NORMS));
308
doc.add(newField("f1", "v2", Store.YES, Index.NO));
310
Field f = newField("f2", "v1", Store.NO, Index.ANALYZED);
311
f.setIndexOptions(IndexOptions.DOCS_ONLY);
313
doc.add(newField("f2", "v2", Store.YES, Index.NO));
315
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(
316
TEST_VERSION_CURRENT, new MockAnalyzer(random)));
317
writer.addDocument(doc);
318
writer.optimize(); // be sure to have a single segment
321
_TestUtil.checkIndex(dir);
323
SegmentReader reader = SegmentReader.getOnlySegmentReader(dir);
324
FieldInfos fi = reader.fieldInfos();
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);
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);