add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / search / TestDocBoost.java
1 package org.apache.lucene.search;
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
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;
29
30 /** Document boost unit test.
31  *
32  *
33  * @version $Revision: 1099728 $
34  */
35 public class TestDocBoost extends LuceneTestCase {
36
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()));
40
41     Fieldable f1 = newField("field", "word", Field.Store.YES, Field.Index.ANALYZED);
42     Fieldable f2 = newField("field", "word", Field.Store.YES, Field.Index.ANALYZED);
43     f2.setBoost(2.0f);
44
45     Document d1 = new Document();
46     Document d2 = new Document();
47     Document d3 = new Document();
48     Document d4 = new Document();
49     d3.setBoost(3.0f);
50     d4.setBoost(2.0f);
51
52     d1.add(f1);                                 // boost = 1
53     d2.add(f2);                                 // boost = 2
54     d3.add(f1);                                 // boost = 3
55     d4.add(f2);                                 // boost = 4
56
57     writer.addDocument(d1);
58     writer.addDocument(d2);
59     writer.addDocument(d3);
60     writer.addDocument(d4);
61
62     IndexReader reader = writer.getReader();
63     writer.close();
64
65     final float[] scores = new float[4];
66
67     newSearcher(reader).search
68       (new TermQuery(new Term("field", "word")),
69        new Collector() {
70          private int base = 0;
71          private Scorer scorer;
72          @Override
73          public void setScorer(Scorer scorer) throws IOException {
74           this.scorer = scorer;
75          }
76          @Override
77          public final void collect(int doc) throws IOException {
78            scores[doc + base] = scorer.score();
79          }
80          @Override
81          public void setNextReader(IndexReader reader, int docBase) {
82            base = docBase;
83          }
84          @Override
85          public boolean acceptsDocsOutOfOrder() {
86            return true;
87          }
88        });
89
90     float lastScore = 0.0f;
91
92     for (int i = 0; i < 4; i++) {
93       assertTrue(scores[i] > lastScore);
94       lastScore = scores[i];
95     }
96     
97     reader.close();
98     store.close();
99   }
100 }