KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestDocBoost


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import junit.framework.TestCase;
20
21 import org.apache.lucene.index.Term;
22 import org.apache.lucene.index.IndexWriter;
23 import org.apache.lucene.search.IndexSearcher;
24 import org.apache.lucene.store.RAMDirectory;
25 import org.apache.lucene.analysis.SimpleAnalyzer;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field;
28
29 /** Document boost unit test.
30  *
31  * @author Doug Cutting
32  * @version $Revision: 1.4 $
33  */

34 public class TestDocBoost extends TestCase {
35   public TestDocBoost(String JavaDoc name) {
36     super(name);
37   }
38   
39   public void testDocBoost() throws Exception JavaDoc {
40     RAMDirectory store = new RAMDirectory();
41     IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true);
42     
43     Field f1 = Field.Text("field", "word");
44     Field f2 = Field.Text("field", "word");
45     f2.setBoost(2.0f);
46     
47     Document d1 = new Document();
48     Document d2 = new Document();
49     Document d3 = new Document();
50     Document d4 = new Document();
51     d3.setBoost(3.0f);
52     d4.setBoost(2.0f);
53     
54     d1.add(f1); // boost = 1
55
d2.add(f2); // boost = 2
56
d3.add(f1); // boost = 3
57
d4.add(f2); // boost = 4
58

59     writer.addDocument(d1);
60     writer.addDocument(d2);
61     writer.addDocument(d3);
62     writer.addDocument(d4);
63     writer.optimize();
64     writer.close();
65
66     final float[] scores = new float[4];
67
68     new IndexSearcher(store).search
69       (new TermQuery(new Term("field", "word")),
70        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 }
84
Popular Tags