1 package org.apache.lucene.search;
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;
22 import org.apache.lucene.analysis.MockAnalyzer;
23 import org.apache.lucene.document.*;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.store.Directory;
28 import org.apache.lucene.util.LuceneTestCase;
30 /** Document boost unit test.
33 * @version $Revision: 1099728 $
35 public class TestDocBoost extends LuceneTestCase {
37 public void testDocBoost() throws Exception {
38 Directory store = newDirectory();
39 RandomIndexWriter writer = new RandomIndexWriter(random, store, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
41 Fieldable f1 = newField("field", "word", Field.Store.YES, Field.Index.ANALYZED);
42 Fieldable f2 = newField("field", "word", Field.Store.YES, Field.Index.ANALYZED);
45 Document d1 = new Document();
46 Document d2 = new Document();
47 Document d3 = new Document();
48 Document d4 = new Document();
52 d1.add(f1); // boost = 1
53 d2.add(f2); // boost = 2
54 d3.add(f1); // boost = 3
55 d4.add(f2); // boost = 4
57 writer.addDocument(d1);
58 writer.addDocument(d2);
59 writer.addDocument(d3);
60 writer.addDocument(d4);
62 IndexReader reader = writer.getReader();
65 final float[] scores = new float[4];
67 newSearcher(reader).search
68 (new TermQuery(new Term("field", "word")),
71 private Scorer scorer;
73 public void setScorer(Scorer scorer) throws IOException {
77 public final void collect(int doc) throws IOException {
78 scores[doc + base] = scorer.score();
81 public void setNextReader(IndexReader reader, int docBase) {
85 public boolean acceptsDocsOutOfOrder() {
90 float lastScore = 0.0f;
92 for (int i = 0; i < 4; i++) {
93 assertTrue(scores[i] > lastScore);
94 lastScore = scores[i];