| 1 |
|
package org.apache.lucene.search; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import junit.framework.TestCase; |
| 20 |
|
import org.apache.lucene.analysis.SimpleAnalyzer; |
| 21 |
|
import org.apache.lucene.document.*; |
| 22 |
|
import org.apache.lucene.index.IndexWriter; |
| 23 |
|
import org.apache.lucene.index.Term; |
| 24 |
|
import org.apache.lucene.store.RAMDirectory; |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
@author |
| 29 |
|
@version |
| 30 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 4 |
Complexity Density: 0.14 |
|
| 31 |
|
public class TestDocBoost extends TestCase { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 32 |
1
|
public TestDocBoost(String name) {... |
| 33 |
1
|
super(name); |
| 34 |
|
} |
| 35 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (29) |
Complexity: 2 |
Complexity Density: 0.07 |
1
PASS
|
|
| 36 |
1
|
public void testDocBoost() throws Exception {... |
| 37 |
1
|
RAMDirectory store = new RAMDirectory(); |
| 38 |
1
|
IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true); |
| 39 |
|
|
| 40 |
1
|
Fieldable f1 = new Field("field", "word", Field.Store.YES, Field.Index.TOKENIZED); |
| 41 |
1
|
Fieldable f2 = new Field("field", "word", Field.Store.YES, Field.Index.TOKENIZED); |
| 42 |
1
|
f2.setBoost(2.0f); |
| 43 |
|
|
| 44 |
1
|
Document d1 = new Document(); |
| 45 |
1
|
Document d2 = new Document(); |
| 46 |
1
|
Document d3 = new Document(); |
| 47 |
1
|
Document d4 = new Document(); |
| 48 |
1
|
d3.setBoost(3.0f); |
| 49 |
1
|
d4.setBoost(2.0f); |
| 50 |
|
|
| 51 |
1
|
d1.add(f1); |
| 52 |
1
|
d2.add(f2); |
| 53 |
1
|
d3.add(f1); |
| 54 |
1
|
d4.add(f2); |
| 55 |
|
|
| 56 |
1
|
writer.addDocument(d1); |
| 57 |
1
|
writer.addDocument(d2); |
| 58 |
1
|
writer.addDocument(d3); |
| 59 |
1
|
writer.addDocument(d4); |
| 60 |
1
|
writer.optimize(); |
| 61 |
1
|
writer.close(); |
| 62 |
|
|
| 63 |
1
|
final float[] scores = new float[4]; |
| 64 |
|
|
| 65 |
1
|
new IndexSearcher(store).search |
| 66 |
|
(new TermQuery(new Term("field", "word")), |
| 67 |
|
new HitCollector() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 68 |
4
|
public final void collect(int doc, float score) {... |
| 69 |
4
|
scores[doc] = score; |
| 70 |
|
} |
| 71 |
|
}); |
| 72 |
|
|
| 73 |
1
|
float lastScore = 0.0f; |
| 74 |
|
|
| 75 |
5
|
for (int i = 0; i < 4; i++) { |
| 76 |
4
|
assertTrue(scores[i] > lastScore); |
| 77 |
4
|
lastScore = scores[i]; |
| 78 |
|
} |
| 79 |
|
} |
| 80 |
|
} |