1 package org.apache.lucene.search; 2 3 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 34 public class TestDocBoost extends TestCase { 35 public TestDocBoost(String name) { 36 super(name); 37 } 38 39 public void testDocBoost() throws Exception { 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); d2.add(f2); d3.add(f1); d4.add(f2); 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 |