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.IndexWriter;
24: import org.apache.lucene.index.Term;
25: import org.apache.lucene.store.RAMDirectory;
26:
27: /** Document boost unit test.
28: *
29: *
30: * @version $Revision: 583534 $
31: */
32: public class TestDocBoost extends LuceneTestCase {
33: public TestDocBoost(String name) {
34: super (name);
35: }
36:
37: public void testDocBoost() throws Exception {
38: RAMDirectory store = new RAMDirectory();
39: IndexWriter writer = new IndexWriter(store,
40: new SimpleAnalyzer(), true);
41:
42: Fieldable f1 = new Field("field", "word", Field.Store.YES,
43: Field.Index.TOKENIZED);
44: Fieldable f2 = new Field("field", "word", Field.Store.YES,
45: Field.Index.TOKENIZED);
46: f2.setBoost(2.0f);
47:
48: Document d1 = new Document();
49: Document d2 = new Document();
50: Document d3 = new Document();
51: Document d4 = new Document();
52: d3.setBoost(3.0f);
53: d4.setBoost(2.0f);
54:
55: d1.add(f1); // boost = 1
56: d2.add(f2); // boost = 2
57: d3.add(f1); // boost = 3
58: d4.add(f2); // boost = 4
59:
60: writer.addDocument(d1);
61: writer.addDocument(d2);
62: writer.addDocument(d3);
63: writer.addDocument(d4);
64: writer.optimize();
65: writer.close();
66:
67: final float[] scores = new float[4];
68:
69: new IndexSearcher(store).search(new TermQuery(new Term("field",
70: "word")), new HitCollector() {
71: public final void collect(int doc, float score) {
72: scores[doc] = score;
73: }
74: });
75:
76: float lastScore = 0.0f;
77:
78: for (int i = 0; i < 4; i++) {
79: assertTrue(scores[i] > lastScore);
80: lastScore = scores[i];
81: }
82: }
83: }
|