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.util.Collection;
23 import org.apache.lucene.util.LuceneTestCase;
24 import org.apache.lucene.util._TestUtil;
25 import org.apache.lucene.analysis.Analyzer;
26 import org.apache.lucene.analysis.MockAnalyzer;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29 import org.apache.lucene.index.FieldInfo.IndexOptions;
30 import org.apache.lucene.search.*;
31 import org.apache.lucene.search.BooleanClause.Occur;
32 import org.apache.lucene.store.Directory;
33 import org.apache.lucene.search.Explanation.IDFExplanation;
36 public class TestOmitTf extends LuceneTestCase {
38 public static class SimpleSimilarity extends Similarity {
39 @Override public float computeNorm(String field, FieldInvertState state) { return state.getBoost(); }
40 @Override public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
41 @Override public float tf(float freq) { return freq; }
42 @Override public float sloppyFreq(int distance) { return 2.0f; }
43 @Override public float idf(int docFreq, int numDocs) { return 1.0f; }
44 @Override public float coord(int overlap, int maxOverlap) { return 1.0f; }
45 @Override public IDFExplanation idfExplain(Collection<Term> terms, Searcher searcher) throws IOException {
46 return new IDFExplanation() {
48 public float getIdf() {
52 public String explain() {
53 return "Inexplicable";
59 // Tests whether the DocumentWriter correctly enable the
60 // omitTermFreqAndPositions bit in the FieldInfo
61 public void testOmitTermFreqAndPositions() throws Exception {
62 Directory ram = newDirectory();
63 Analyzer analyzer = new MockAnalyzer(random);
64 IndexWriter writer = new IndexWriter(ram, newIndexWriterConfig( TEST_VERSION_CURRENT, analyzer));
65 Document d = new Document();
67 // this field will have Tf
68 Field f1 = newField("f1", "This field has term freqs", Field.Store.NO, Field.Index.ANALYZED);
71 // this field will NOT have Tf
72 Field f2 = newField("f2", "This field has NO Tf in all docs", Field.Store.NO, Field.Index.ANALYZED);
73 f2.setIndexOptions(IndexOptions.DOCS_ONLY);
76 writer.addDocument(d);
78 // now we add another document which has term freq for field f2 and not for f1 and verify if the SegmentMerger
79 // keep things constant
83 f1.setIndexOptions(IndexOptions.DOCS_ONLY);
86 f2.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
89 writer.addDocument(d);
95 SegmentReader reader = SegmentReader.getOnlySegmentReader(ram);
96 FieldInfos fi = reader.fieldInfos();
97 assertEquals("OmitTermFreqAndPositions field bit should be set.", IndexOptions.DOCS_ONLY, fi.fieldInfo("f1").indexOptions);
98 assertEquals("OmitTermFreqAndPositions field bit should be set.", IndexOptions.DOCS_ONLY, fi.fieldInfo("f2").indexOptions);
104 // Tests whether merging of docs that have different
105 // omitTermFreqAndPositions for the same field works
106 public void testMixedMerge() throws Exception {
107 Directory ram = newDirectory();
108 Analyzer analyzer = new MockAnalyzer(random);
109 IndexWriter writer = new IndexWriter(
111 newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).
112 setMaxBufferedDocs(3).
113 setMergePolicy(newLogMergePolicy(2))
115 writer.setInfoStream(VERBOSE ? System.out : null);
116 Document d = new Document();
118 // this field will have Tf
119 Field f1 = newField("f1", "This field has term freqs", Field.Store.NO, Field.Index.ANALYZED);
122 // this field will NOT have Tf
123 Field f2 = newField("f2", "This field has NO Tf in all docs", Field.Store.NO, Field.Index.ANALYZED);
124 f2.setIndexOptions(IndexOptions.DOCS_ONLY);
127 for(int i=0;i<30;i++)
128 writer.addDocument(d);
130 // now we add another document which has term freq for field f2 and not for f1 and verify if the SegmentMerger
131 // keep things constant
135 f1.setIndexOptions(IndexOptions.DOCS_ONLY);
138 f2.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
141 for(int i=0;i<30;i++)
142 writer.addDocument(d);
149 SegmentReader reader = SegmentReader.getOnlySegmentReader(ram);
150 FieldInfos fi = reader.fieldInfos();
151 assertEquals("OmitTermFreqAndPositions field bit should be set.", IndexOptions.DOCS_ONLY, fi.fieldInfo("f1").indexOptions);
152 assertEquals("OmitTermFreqAndPositions field bit should be set.", IndexOptions.DOCS_ONLY, fi.fieldInfo("f2").indexOptions);
158 // Make sure first adding docs that do not omitTermFreqAndPositions for
159 // field X, then adding docs that do omitTermFreqAndPositions for that same
161 public void testMixedRAM() throws Exception {
162 Directory ram = newDirectory();
163 Analyzer analyzer = new MockAnalyzer(random);
164 IndexWriter writer = new IndexWriter(
166 newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).
167 setMaxBufferedDocs(10).
168 setMergePolicy(newLogMergePolicy(2))
170 Document d = new Document();
172 // this field will have Tf
173 Field f1 = newField("f1", "This field has term freqs", Field.Store.NO, Field.Index.ANALYZED);
176 // this field will NOT have Tf
177 Field f2 = newField("f2", "This field has NO Tf in all docs", Field.Store.NO, Field.Index.ANALYZED);
181 writer.addDocument(d);
183 f2.setIndexOptions(IndexOptions.DOCS_ONLY);
185 for(int i=0;i<20;i++)
186 writer.addDocument(d);
194 SegmentReader reader = SegmentReader.getOnlySegmentReader(ram);
195 FieldInfos fi = reader.fieldInfos();
196 assertEquals("OmitTermFreqAndPositions field bit should not be set.", IndexOptions.DOCS_AND_FREQS_AND_POSITIONS, fi.fieldInfo("f1").indexOptions);
197 assertEquals("OmitTermFreqAndPositions field bit should be set.", IndexOptions.DOCS_ONLY, fi.fieldInfo("f2").indexOptions);
203 private void assertNoPrx(Directory dir) throws Throwable {
204 final String[] files = dir.listAll();
205 for(int i=0;i<files.length;i++) {
206 assertFalse(files[i].endsWith(".prx"));
207 assertFalse(files[i].endsWith(".pos"));
211 // Verifies no *.prx exists when all fields omit term freq:
212 public void testNoPrxFile() throws Throwable {
213 Directory ram = newDirectory();
214 Analyzer analyzer = new MockAnalyzer(random);
215 IndexWriter writer = new IndexWriter(ram, newIndexWriterConfig(
216 TEST_VERSION_CURRENT, analyzer).setMaxBufferedDocs(3).setMergePolicy(newLogMergePolicy()));
217 LogMergePolicy lmp = (LogMergePolicy) writer.getConfig().getMergePolicy();
218 lmp.setMergeFactor(2);
219 lmp.setUseCompoundFile(false);
220 Document d = new Document();
222 Field f1 = newField("f1", "This field has no term freqs", Field.Store.NO, Field.Index.ANALYZED);
223 f1.setIndexOptions(IndexOptions.DOCS_ONLY);
226 for(int i=0;i<30;i++)
227 writer.addDocument(d);
233 // now add some documents with positions, and check there is no prox after optimization
235 f1 = newField("f1", "This field has positions", Field.Store.NO, Field.Index.ANALYZED);
238 for(int i=0;i<30;i++)
239 writer.addDocument(d);
250 // Test scores with one field with Term Freqs and one without, otherwise with equal content
251 public void testBasic() throws Exception {
252 Directory dir = newDirectory();
253 Analyzer analyzer = new MockAnalyzer(random);
254 IndexWriter writer = new IndexWriter(
256 newIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).
257 setMaxBufferedDocs(2).
258 setSimilarity(new SimpleSimilarity()).
259 setMergePolicy(newLogMergePolicy(2))
261 writer.setInfoStream(VERBOSE ? System.out : null);
263 StringBuilder sb = new StringBuilder(265);
264 String term = "term";
265 for(int i = 0; i<30; i++){
266 Document d = new Document();
267 sb.append(term).append(" ");
268 String content = sb.toString();
269 Field noTf = newField("noTf", content + (i%2==0 ? "" : " notf"), Field.Store.NO, Field.Index.ANALYZED);
270 noTf.setIndexOptions(IndexOptions.DOCS_ONLY);
273 Field tf = newField("tf", content + (i%2==0 ? " tf" : ""), Field.Store.NO, Field.Index.ANALYZED);
276 writer.addDocument(d);
277 //System.out.println(d);
287 Searcher searcher = new IndexSearcher(dir, true);
288 searcher.setSimilarity(new SimpleSimilarity());
290 Term a = new Term("noTf", term);
291 Term b = new Term("tf", term);
292 Term c = new Term("noTf", "notf");
293 Term d = new Term("tf", "tf");
294 TermQuery q1 = new TermQuery(a);
295 TermQuery q2 = new TermQuery(b);
296 TermQuery q3 = new TermQuery(c);
297 TermQuery q4 = new TermQuery(d);
301 new CountingHitCollector() {
302 private Scorer scorer;
304 public final void setScorer(Scorer scorer) {
305 this.scorer = scorer;
308 public final void collect(int doc) throws IOException {
309 //System.out.println("Q1: Doc=" + doc + " score=" + score);
310 float score = scorer.score();
311 assertTrue(score==1.0f);
315 //System.out.println(CountingHitCollector.getCount());
319 new CountingHitCollector() {
320 private Scorer scorer;
322 public final void setScorer(Scorer scorer) {
323 this.scorer = scorer;
326 public final void collect(int doc) throws IOException {
327 //System.out.println("Q2: Doc=" + doc + " score=" + score);
328 float score = scorer.score();
329 assertEquals(1.0f+doc, score, 0.00001f);
333 //System.out.println(CountingHitCollector.getCount());
340 new CountingHitCollector() {
341 private Scorer scorer;
343 public final void setScorer(Scorer scorer) {
344 this.scorer = scorer;
347 public final void collect(int doc) throws IOException {
348 //System.out.println("Q1: Doc=" + doc + " score=" + score);
349 float score = scorer.score();
350 assertTrue(score==1.0f);
351 assertFalse(doc%2==0);
355 //System.out.println(CountingHitCollector.getCount());
359 new CountingHitCollector() {
360 private Scorer scorer;
362 public final void setScorer(Scorer scorer) {
363 this.scorer = scorer;
366 public final void collect(int doc) throws IOException {
367 float score = scorer.score();
368 //System.out.println("Q1: Doc=" + doc + " score=" + score);
369 assertTrue(score==1.0f);
370 assertTrue(doc%2==0);
374 //System.out.println(CountingHitCollector.getCount());
378 BooleanQuery bq = new BooleanQuery();
379 bq.add(q1,Occur.MUST);
380 bq.add(q4,Occur.MUST);
383 new CountingHitCollector() {
385 public final void collect(int doc) throws IOException {
386 //System.out.println("BQ: Doc=" + doc + " score=" + score);
390 assertTrue(15 == CountingHitCollector.getCount());
396 public static class CountingHitCollector extends Collector {
399 private int docBase = -1;
400 CountingHitCollector(){count=0;sum=0;}
402 public void setScorer(Scorer scorer) throws IOException {}
404 public void collect(int doc) throws IOException {
406 sum += doc + docBase; // use it to avoid any possibility of being optimized away
409 public static int getCount() { return count; }
410 public static int getSum() { return sum; }
413 public void setNextReader(IndexReader reader, int docBase) {
414 this.docBase = docBase;
417 public boolean acceptsDocsOutOfOrder() {