pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / TestSetNorm.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.util.LuceneTestCase;
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.document.*;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.IndexWriter;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.store.Directory;
29
30 /** Document boost unit test.
31  *
32  *
33  * @version $Revision: 1091277 $
34  */
35 public class TestSetNorm extends LuceneTestCase {
36
37   public void testSetNorm() throws Exception {
38     Directory store = newDirectory();
39     IndexWriter writer = new IndexWriter(store, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
40
41     // add the same document four times
42     Fieldable f1 = newField("field", "word", Field.Store.YES, Field.Index.ANALYZED);
43     Document d1 = new Document();
44     d1.add(f1);
45     writer.addDocument(d1);
46     writer.addDocument(d1);
47     writer.addDocument(d1);
48     writer.addDocument(d1);
49     writer.close();
50
51     // reset the boost of each instance of this document
52     IndexReader reader = IndexReader.open(store, false);
53     reader.setNorm(0, "field", 1.0f);
54     reader.setNorm(1, "field", 2.0f);
55     reader.setNorm(2, "field", 4.0f);
56     reader.setNorm(3, "field", 16.0f);
57     reader.close();
58
59     // check that searches are ordered by this boost
60     final float[] scores = new float[4];
61
62     IndexSearcher is = new IndexSearcher(store, true);
63     is.search
64       (new TermQuery(new Term("field", "word")),
65        new Collector() {
66          private int base = 0;
67          private Scorer scorer;
68          @Override
69          public void setScorer(Scorer scorer) throws IOException {
70           this.scorer = scorer;
71          }
72          @Override
73          public final void collect(int doc) throws IOException {
74            scores[doc + base] = scorer.score();
75          }
76          @Override
77          public void setNextReader(IndexReader reader, int docBase) {
78            base = docBase;
79          }
80          @Override
81          public boolean acceptsDocsOutOfOrder() {
82            return true;
83          }
84        });
85     is.close();
86     float lastScore = 0.0f;
87
88     for (int i = 0; i < 4; i++) {
89       assertTrue(scores[i] > lastScore);
90       lastScore = scores[i];
91     }
92     store.close();
93   }
94 }