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.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;
30
/** Document boost unit test.
33
* @version $Revision: 1202680 $
35
public class TestSetNorm extends LuceneTestCase {
37
public void testSetNorm() throws Exception {
38
Directory store = newDirectory();
39
IndexWriter writer = new IndexWriter(store, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
41
// add the same document four times
42
Fieldable f1 = newField("field", "word", Field.Store.YES, Field.Index.ANALYZED);
43
Document d1 = new Document();
45
writer.addDocument(d1);
46
writer.addDocument(d1);
47
writer.addDocument(d1);
48
writer.addDocument(d1);
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);
59
// check that searches are ordered by this boost
60
final float[] scores = new float[4];
62
IndexReader ir = IndexReader.open(store);
63
IndexSearcher is = new IndexSearcher(ir);
65
(new TermQuery(new Term("field", "word")),
68
private Scorer scorer;
70
public void setScorer(Scorer scorer) throws IOException {
74
public final void collect(int doc) throws IOException {
75
scores[doc + base] = scorer.score();
78
public void setNextReader(IndexReader reader, int docBase) {
82
public boolean acceptsDocsOutOfOrder() {
88
float lastScore = 0.0f;
90
for (int i = 0; i < 4; i++) {
91
assertTrue(scores[i] > lastScore);
92
lastScore = scores[i];