01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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 org.apache.lucene.util.LuceneTestCase;
21: import org.apache.lucene.analysis.SimpleAnalyzer;
22: import org.apache.lucene.document.*;
23: import org.apache.lucene.index.IndexReader;
24: import org.apache.lucene.index.IndexWriter;
25: import org.apache.lucene.index.Term;
26: import org.apache.lucene.store.RAMDirectory;
27:
28: /** Document boost unit test.
29: *
30: *
31: * @version $Revision: 583534 $
32: */
33: public class TestSetNorm extends LuceneTestCase {
34: public TestSetNorm(String name) {
35: super (name);
36: }
37:
38: public void testSetNorm() throws Exception {
39: RAMDirectory store = new RAMDirectory();
40: IndexWriter writer = new IndexWriter(store,
41: new SimpleAnalyzer(), true);
42:
43: // add the same document four times
44: Fieldable f1 = new Field("field", "word", Field.Store.YES,
45: Field.Index.TOKENIZED);
46: Document d1 = new Document();
47: d1.add(f1);
48: writer.addDocument(d1);
49: writer.addDocument(d1);
50: writer.addDocument(d1);
51: writer.addDocument(d1);
52: writer.close();
53:
54: // reset the boost of each instance of this document
55: IndexReader reader = IndexReader.open(store);
56: reader.setNorm(0, "field", 1.0f);
57: reader.setNorm(1, "field", 2.0f);
58: reader.setNorm(2, "field", 4.0f);
59: reader.setNorm(3, "field", 16.0f);
60: reader.close();
61:
62: // check that searches are ordered by this boost
63: final float[] scores = new float[4];
64:
65: new IndexSearcher(store).search(new TermQuery(new Term("field",
66: "word")), new HitCollector() {
67: public final void collect(int doc, float score) {
68: scores[doc] = score;
69: }
70: });
71:
72: float lastScore = 0.0f;
73:
74: for (int i = 0; i < 4; i++) {
75: assertTrue(scores[i] > lastScore);
76: lastScore = scores[i];
77: }
78: }
79: }
|