KickJava   Java API By Example, From Geeks To Geeks.

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


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.index.IndexReader;
24 import org.apache.lucene.search.IndexSearcher;
25 import org.apache.lucene.store.RAMDirectory;
26 import org.apache.lucene.analysis.SimpleAnalyzer;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29
30 /** Document boost unit test.
31  *
32  * @author Doug Cutting
33  * @version $Revision: 1.2 $
34  */

35 public class TestSetNorm extends TestCase {
36   public TestSetNorm(String JavaDoc name) {
37     super(name);
38   }
39   
40   public void testSetNorm() throws Exception JavaDoc {
41     RAMDirectory store = new RAMDirectory();
42     IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true);
43     
44     // add the same document four times
45
Field f1 = Field.Text("field", "word");
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
66       (new TermQuery(new Term("field", "word")),
67        new HitCollector() {
68          public final void collect(int doc, float score) {
69            scores[doc] = score;
70          }
71        });
72     
73     float lastScore = 0.0f;
74
75     for (int i = 0; i < 4; i++) {
76       assertTrue(scores[i] > lastScore);
77       lastScore = scores[i];
78     }
79   }
80 }
81
Popular Tags