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.analysis.Analyzer;
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.FieldInfo.IndexOptions;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.LuceneTestCase;
30 * @lucene.experimental
32 public class TestOmitPositions extends LuceneTestCase {
34 public void testBasic() throws Exception {
35 Directory dir = newDirectory();
36 RandomIndexWriter w = new RandomIndexWriter(random, dir);
37 Document doc = new Document();
38 Field f = newField("foo", "this is a test test", Field.Index.ANALYZED);
39 f.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
41 for (int i = 0; i < 100; i++) {
45 IndexReader reader = w.getReader();
48 TermPositions tp = reader.termPositions(new Term("foo", "test"));
50 assertEquals(2, tp.freq());
51 assertEquals(0, tp.nextPosition());
52 assertEquals(0, tp.nextPosition());
55 TermDocs te = reader.termDocs(new Term("foo", "test"));
57 assertEquals(2, te.freq());
64 // Tests whether the DocumentWriter correctly enable the
65 // omitTermFreqAndPositions bit in the FieldInfo
66 public void testPositions() throws Exception {
67 Directory ram = newDirectory();
68 Analyzer analyzer = new MockAnalyzer(random);
69 IndexWriter writer = new IndexWriter(ram, newIndexWriterConfig( TEST_VERSION_CURRENT, analyzer));
70 Document d = new Document();
72 // f1,f2,f3: docs only
73 Field f1 = newField("f1", "This field has docs only", Field.Store.NO, Field.Index.ANALYZED);
74 f1.setIndexOptions(IndexOptions.DOCS_ONLY);
77 Field f2 = newField("f2", "This field has docs only", Field.Store.NO, Field.Index.ANALYZED);
78 f2.setIndexOptions(IndexOptions.DOCS_ONLY);
81 Field f3 = newField("f3", "This field has docs only", Field.Store.NO, Field.Index.ANALYZED);
82 f3.setIndexOptions(IndexOptions.DOCS_ONLY);
85 // f4,f5,f6 docs and freqs
86 Field f4 = newField("f4", "This field has docs and freqs", Field.Store.NO, Field.Index.ANALYZED);
87 f4.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
90 Field f5 = newField("f5", "This field has docs and freqs", Field.Store.NO, Field.Index.ANALYZED);
91 f5.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
94 Field f6 = newField("f6", "This field has docs and freqs", Field.Store.NO, Field.Index.ANALYZED);
95 f6.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
98 // f7,f8,f9 docs/freqs/positions
99 Field f7 = newField("f7", "This field has docs and freqs and positions", Field.Store.NO, Field.Index.ANALYZED);
100 f7.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
103 Field f8 = newField("f8", "This field has docs and freqs and positions", Field.Store.NO, Field.Index.ANALYZED);
104 f8.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
107 Field f9 = newField("f9", "This field has docs and freqs and positions", Field.Store.NO, Field.Index.ANALYZED);
108 f9.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
111 writer.addDocument(d);
112 writer.forceMerge(1);
114 // now we add another document which has docs-only for f1, f4, f7, docs/freqs for f2, f5, f8,
115 // and docs/freqs/positions for f3, f6, f9
118 // f1,f4,f7: docs only
119 f1 = newField("f1", "This field has docs only", Field.Store.NO, Field.Index.ANALYZED);
120 f1.setIndexOptions(IndexOptions.DOCS_ONLY);
123 f4 = newField("f4", "This field has docs only", Field.Store.NO, Field.Index.ANALYZED);
124 f4.setIndexOptions(IndexOptions.DOCS_ONLY);
127 f7 = newField("f7", "This field has docs only", Field.Store.NO, Field.Index.ANALYZED);
128 f7.setIndexOptions(IndexOptions.DOCS_ONLY);
131 // f2, f5, f8: docs and freqs
132 f2 = newField("f2", "This field has docs and freqs", Field.Store.NO, Field.Index.ANALYZED);
133 f2.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
136 f5 = newField("f5", "This field has docs and freqs", Field.Store.NO, Field.Index.ANALYZED);
137 f5.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
140 f8 = newField("f8", "This field has docs and freqs", Field.Store.NO, Field.Index.ANALYZED);
141 f8.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
144 // f3, f6, f9: docs and freqs and positions
145 f3 = newField("f3", "This field has docs and freqs and positions", Field.Store.NO, Field.Index.ANALYZED);
146 f3.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
149 f6 = newField("f6", "This field has docs and freqs and positions", Field.Store.NO, Field.Index.ANALYZED);
150 f6.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
153 f9 = newField("f9", "This field has docs and freqs and positions", Field.Store.NO, Field.Index.ANALYZED);
154 f9.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
157 writer.addDocument(d);
160 writer.forceMerge(1);
164 SegmentReader reader = SegmentReader.getOnlySegmentReader(ram);
165 FieldInfos fi = reader.fieldInfos();
166 // docs + docs = docs
167 assertEquals(IndexOptions.DOCS_ONLY, fi.fieldInfo("f1").indexOptions);
168 // docs + docs/freqs = docs
169 assertEquals(IndexOptions.DOCS_ONLY, fi.fieldInfo("f2").indexOptions);
170 // docs + docs/freqs/pos = docs
171 assertEquals(IndexOptions.DOCS_ONLY, fi.fieldInfo("f3").indexOptions);
172 // docs/freqs + docs = docs
173 assertEquals(IndexOptions.DOCS_ONLY, fi.fieldInfo("f4").indexOptions);
174 // docs/freqs + docs/freqs = docs/freqs
175 assertEquals(IndexOptions.DOCS_AND_FREQS, fi.fieldInfo("f5").indexOptions);
176 // docs/freqs + docs/freqs/pos = docs/freqs
177 assertEquals(IndexOptions.DOCS_AND_FREQS, fi.fieldInfo("f6").indexOptions);
178 // docs/freqs/pos + docs = docs
179 assertEquals(IndexOptions.DOCS_ONLY, fi.fieldInfo("f7").indexOptions);
180 // docs/freqs/pos + docs/freqs = docs/freqs
181 assertEquals(IndexOptions.DOCS_AND_FREQS, fi.fieldInfo("f8").indexOptions);
182 // docs/freqs/pos + docs/freqs/pos = docs/freqs/pos
183 assertEquals(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, fi.fieldInfo("f9").indexOptions);
189 private void assertNoPrx(Directory dir) throws Throwable {
190 final String[] files = dir.listAll();
191 for(int i=0;i<files.length;i++) {
192 assertFalse(files[i].endsWith(".prx"));
193 assertFalse(files[i].endsWith(".pos"));
197 // Verifies no *.prx exists when all fields omit term positions:
198 public void testNoPrxFile() throws Throwable {
199 Directory ram = newDirectory();
200 Analyzer analyzer = new MockAnalyzer(random);
201 IndexWriter writer = new IndexWriter(ram, newIndexWriterConfig(
202 TEST_VERSION_CURRENT, analyzer).setMaxBufferedDocs(3).setMergePolicy(newLogMergePolicy()));
203 LogMergePolicy lmp = (LogMergePolicy) writer.getConfig().getMergePolicy();
204 lmp.setMergeFactor(2);
205 lmp.setUseCompoundFile(false);
206 Document d = new Document();
208 Field f1 = newField("f1", "This field has term freqs", Field.Store.NO, Field.Index.ANALYZED);
209 f1.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
212 for(int i=0;i<30;i++)
213 writer.addDocument(d);
219 // now add some documents with positions, and check there is no prox after optimization
221 f1 = newField("f1", "This field has positions", Field.Store.NO, Field.Index.ANALYZED);
224 for(int i=0;i<30;i++)
225 writer.addDocument(d);
228 writer.forceMerge(1);