| 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 org.apache.lucene.analysis.WhitespaceAnalyzer; |
| 20 |
|
import org.apache.lucene.document.Document; |
| 21 |
|
import org.apache.lucene.document.Field; |
| 22 |
|
import org.apache.lucene.index.IndexReader; |
| 23 |
|
import org.apache.lucene.index.IndexWriter; |
| 24 |
|
import org.apache.lucene.index.Term; |
| 25 |
|
import org.apache.lucene.store.Directory; |
| 26 |
|
import org.apache.lucene.store.RAMDirectory; |
| 27 |
|
|
| 28 |
|
import java.io.IOException; |
| 29 |
|
|
| 30 |
|
import junit.framework.Assert; |
| 31 |
|
|
|
|
|
| 98.9% |
Uncovered Elements: 2 (174) |
Complexity: 17 |
Complexity Density: 0.11 |
|
| 32 |
|
public class TestConstantScoreRangeQuery extends BaseTestRangeFilter { |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
public static final float SCORE_COMP_THRESH = 1e-6f; |
| 36 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 37 |
7
|
public TestConstantScoreRangeQuery(String name) {... |
| 38 |
7
|
super(name); |
| 39 |
|
} |
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 40 |
0
|
public TestConstantScoreRangeQuery() {... |
| 41 |
0
|
super(); |
| 42 |
|
} |
| 43 |
|
|
| 44 |
|
Directory small; |
| 45 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 46 |
11
|
void assertEquals(String m, float e, float a) {... |
| 47 |
11
|
assertEquals(m, e, a, SCORE_COMP_THRESH); |
| 48 |
|
} |
| 49 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 50 |
44
|
static public void assertEquals(String m, int e, int a) {... |
| 51 |
44
|
Assert.assertEquals(m, e, a); |
| 52 |
|
} |
| 53 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
| 54 |
7
|
public void setUp() throws Exception {... |
| 55 |
7
|
super.setUp(); |
| 56 |
|
|
| 57 |
7
|
String[] data = new String [] { |
| 58 |
|
"A 1 2 3 4 5 6", |
| 59 |
|
"Z 4 5 6", |
| 60 |
|
null, |
| 61 |
|
"B 2 4 5 6", |
| 62 |
|
"Y 3 5 6", |
| 63 |
|
null, |
| 64 |
|
"C 3 6", |
| 65 |
|
"X 4 5 6" |
| 66 |
|
}; |
| 67 |
|
|
| 68 |
7
|
small = new RAMDirectory(); |
| 69 |
7
|
IndexWriter writer = new IndexWriter(small, |
| 70 |
|
new WhitespaceAnalyzer(), |
| 71 |
|
true); |
| 72 |
|
|
| 73 |
63
|
for (int i = 0; i < data.length; i++) { |
| 74 |
56
|
Document doc = new Document(); |
| 75 |
56
|
doc.add(new Field("id", String.valueOf(i), Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 76 |
56
|
doc.add(new Field("all", "all", Field.Store.YES, Field.Index.UN_TOKENIZED)); |
| 77 |
56
|
if (null != data[i]) { |
| 78 |
42
|
doc.add(new Field("data", data[i], Field.Store.YES, Field.Index.TOKENIZED)); |
| 79 |
|
} |
| 80 |
56
|
writer.addDocument(doc); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
7
|
writer.optimize(); |
| 84 |
7
|
writer.close(); |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 90 |
45
|
public static Query csrq(String f, String l, String h,... |
| 91 |
|
boolean il, boolean ih) { |
| 92 |
45
|
return new ConstantScoreRangeQuery(f,l,h,il,ih); |
| 93 |
|
} |
| 94 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 95 |
1
|
public void testBasics() throws IOException {... |
| 96 |
1
|
QueryUtils.check(csrq("data","1","6",T,T)); |
| 97 |
1
|
QueryUtils.check(csrq("data","A","Z",T,T)); |
| 98 |
1
|
QueryUtils.checkUnequal(csrq("data","1","6",T,T), csrq("data","A","Z",T,T)); |
| 99 |
|
} |
| 100 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
1
PASS
|
|
| 101 |
1
|
public void testEqualScores() throws IOException {... |
| 102 |
|
|
| 103 |
|
|
| 104 |
1
|
IndexReader reader = IndexReader.open(small); |
| 105 |
1
|
IndexSearcher search = new IndexSearcher(reader); |
| 106 |
|
|
| 107 |
1
|
Hits result; |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
1
|
result = search.search(csrq("data","1","6",T,T)); |
| 112 |
1
|
int numHits = result.length(); |
| 113 |
1
|
assertEquals("wrong number of results", 6, numHits); |
| 114 |
1
|
float score = result.score(0); |
| 115 |
6
|
for (int i = 1; i < numHits; i++) { |
| 116 |
5
|
assertEquals("score for " + i +" was not the same", |
| 117 |
|
score, result.score(i)); |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
} |
| 121 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 1 |
Complexity Density: 0.04 |
1
PASS
|
|
| 122 |
1
|
public void testBoost() throws IOException {... |
| 123 |
|
|
| 124 |
|
|
| 125 |
1
|
IndexReader reader = IndexReader.open(small); |
| 126 |
1
|
IndexSearcher search = new IndexSearcher(reader); |
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
1
|
Query q = csrq("data","1","6",T,T); |
| 131 |
1
|
q.setBoost(100); |
| 132 |
1
|
search.search(q,null, new HitCollector() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 133 |
6
|
public void collect(int doc, float score) {... |
| 134 |
6
|
assertEquals("score for doc " + doc +" was not correct", |
| 135 |
|
1.0f, score); |
| 136 |
|
} |
| 137 |
|
}); |
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
1
|
Query q1 = csrq("data","A","A",T,T); |
| 145 |
1
|
q1.setBoost(.1f); |