| 1 |
|
package org.apache.lucene.search; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
import java.io.IOException; |
| 5 |
|
import java.util.ArrayList; |
| 6 |
|
import java.util.List; |
| 7 |
|
|
| 8 |
|
import junit.framework.TestCase; |
| 9 |
|
import org.apache.lucene.analysis.WhitespaceAnalyzer; |
| 10 |
|
import org.apache.lucene.document.Document; |
| 11 |
|
import org.apache.lucene.document.Field; |
| 12 |
|
import org.apache.lucene.index.IndexReader; |
| 13 |
|
import org.apache.lucene.index.IndexWriter; |
| 14 |
|
import org.apache.lucene.index.Term; |
| 15 |
|
import org.apache.lucene.store.RAMDirectory; |
| 16 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (75) |
Complexity: 9 |
Complexity Density: 0.14 |
|
| 17 |
|
public class TestTermScorer extends TestCase |
| 18 |
|
{ |
| 19 |
|
protected RAMDirectory directory; |
| 20 |
|
private static final String FIELD = "field"; |
| 21 |
|
|
| 22 |
|
protected String[] values = new String[]{"all", "dogs dogs", "like", "playing", "fetch", "all"}; |
| 23 |
|
protected IndexSearcher indexSearcher; |
| 24 |
|
protected IndexReader indexReader; |
| 25 |
|
|
| 26 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 27 |
4
|
public TestTermScorer(String s)... |
| 28 |
|
{ |
| 29 |
4
|
super(s); |
| 30 |
|
} |
| 31 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 32 |
4
|
protected void setUp() throws IOException... |
| 33 |
|
{ |
| 34 |
4
|
directory = new RAMDirectory(); |
| 35 |
|
|
| 36 |
|
|
| 37 |
4
|
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true); |
| 38 |
28
|
for (int i = 0; i < values.length; i++) |
| 39 |
|
{ |
| 40 |
24
|
Document doc = new Document(); |
| 41 |
24
|
doc.add(new Field(FIELD, values[i], Field.Store.YES, Field.Index.TOKENIZED)); |
| 42 |
24
|
writer.addDocument(doc); |
| 43 |
|
} |
| 44 |
4
|
writer.close(); |
| 45 |
4
|
indexSearcher = new IndexSearcher(directory); |
| 46 |
4
|
indexReader = indexSearcher.getIndexReader(); |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
} |
| 50 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 51 |
4
|
protected void tearDown()... |
| 52 |
|
{ |
| 53 |
|
|
| 54 |
|
} |
| 55 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1
PASS
|
|
| 56 |
1
|
public void test() throws IOException... |
| 57 |
|
{ |
| 58 |
|
|
| 59 |
1
|
Term allTerm = new Term(FIELD, "all"); |
| 60 |
1
|
TermQuery termQuery = new TermQuery(allTerm); |
| 61 |
|
|
| 62 |
1
|
Weight weight = termQuery.weight(indexSearcher); |
| 63 |
|
|
| 64 |
1
|
TermScorer ts = new TermScorer(weight, |
| 65 |
|
indexReader.termDocs(allTerm), indexSearcher.getSimilarity(), |
| 66 |
|
indexReader.norms(FIELD)); |
| 67 |
1
|
assertTrue("ts is null and it shouldn't be", ts != null); |
| 68 |
|
|
| 69 |
1
|
final List docs = new ArrayList(); |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
1
|
ts.score(new HitCollector() |
| 74 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 75 |
2
|
public void collect(int doc, float score)... |
| 76 |
|
{ |
| 77 |
2
|
docs.add(new TestHit(doc, score)); |
| 78 |
2
|
assertTrue("score " + score + " is not greater than 0", score > 0); |
| 79 |
2
|
assertTrue("Doc: " + doc + " does not equal: " + 0 + |
| 80 |
|
" or doc does not equaal: " + 5, doc == 0 || doc == 5); |
| 81 |
|
} |
| 82 |
|
}); |
| 83 |
1
|
assertTrue("docs Size: " + docs.size() + " is not: " + 2, docs.size() == 2); |
| 84 |
1
|
TestHit doc0 = (TestHit) docs.get(0); |
| 85 |
1
|
TestHit doc5 = (TestHit) docs.get(1); |
| 86 |
|
|
| 87 |
1
|
assertTrue(doc0.score + " does not equal: " + doc5.score, doc0.score == doc5.score); |
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
|
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
|
| 105 |
1
|
assertTrue(doc0.score + " does not equal: " + 1.6931472f, doc0.score == 1.6931472f); |
| 106 |
|
} |
| 107 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1
PASS
|
|
| 108 |
1
|
public void testNext() throws Exception... |
| 109 |
|
{ |
| 110 |
|
|
| 111 |
1
|
Term allTerm = new Term(FIELD, "all"); |
| 112 |
1
|
TermQuery termQuery = new TermQuery(allTerm); |
| 113 |
|
|
| 114 |
1
|
Weight weight = termQuery.weight(indexSearcher); |
| 115 |
|
|
| 116 |
1
|
TermScorer ts = new TermScorer(weight, |
| 117 |
|
indexReader.termDocs(allTerm), indexSearcher.getSimilarity(), |
| 118 |
|
indexReader.norms(FIELD)); |
| 119 |
1
|
assertTrue("ts is null and it shouldn't be", ts != null); |
| 120 |
1
|
assertTrue("next did not return a doc", ts.next() == true); |
| 121 |
1
|
assertTrue("score is not correct", ts.score() == 1.6931472f); |
| 122 |
1
|
assertTrue("next did not return a doc", ts.next() == true); |
| 123 |
1
|
assertTrue("score is not correct", ts.score() == 1.6931472f); |
| 124 |
1
|
assertTrue("next returned a doc and it should not have", ts.next() == false); |
| 125 |
|
} |
| 126 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 127 |
1
|
public void testSkipTo() throws Exception... |
| 128 |
|
{ |
| 129 |
|
|
| 130 |
1
|
Term allTerm = new Term(FIELD, "all"); |
| 131 |
1
|
TermQuery termQuery = new TermQuery(allTerm); |
| 132 |
|
|
| 133 |
1
|
Weight weight = termQuery.weight(indexSearcher); |
| 134 |
|
|
| 135 |
1
|
TermScorer ts = new TermScorer(weight, |
| 136 |
|
indexReader.termDocs(allTerm), indexSearcher.getSimilarity(), |
| 137 |
|
indexReader.norms(FIELD)); |
| 138 |
1
|
assertTrue("ts is null and it shouldn't be", ts != null); |
| 139 |
1
|
assertTrue("Didn't skip", ts.skipTo(3) == true); |
| 140 |
|
|
| 141 |
1
|
assertTrue("doc should be number 5", ts.doc() == 5); |
| 142 |
|
} |
| 143 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (23) |
Complexity: 1 |
Complexity Density: 0.04 |
1
PASS
|
|
| 144 |
1
|
public void testExplain() throws Exception... |
| 145 |
|
{ |
| 146 |
1
|
Term allTerm = new Term(FIELD, "all"); |
| 147 |
1
|
TermQuery termQuery = new TermQuery(allTerm); |
| 148 |
|
|
| 149 |
1
|
Weight weight = termQuery.weight(indexSearcher); |
| 150 |
|
|
| 151 |
1
|
TermScorer ts = new TermScorer(weight, |
| 152 |
|
indexReader.termDocs(allTerm), indexSearcher.getSimilarity(), |
| 153 |
|
indexReader.norms(FIELD)); |
| 154 |
1
|
assertTrue("ts is null and it shouldn't be", ts != null); |
| 155 |
1
|
Explanation explanation = ts.explain(0); |
| 156 |
1
|
assertTrue("explanation is null and it shouldn't be", explanation != null); |
| 157 |
|
|
| 158 |
|
|
| 159 |
1
|
assertTrue("term frq is not 1", explanation.getValue() == 1); |
| 160 |
1
|
explanation = ts.explain(1); |
| 161 |
1
|
assertTrue("explanation is null and it shouldn't be", explanation != n |