| 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 |
|
|
| 21 |
|
import java.util.Collection; |
| 22 |
|
|
| 23 |
|
import org.apache.lucene.index.Term; |
| 24 |
|
import org.apache.lucene.index.IndexWriter; |
| 25 |
|
import org.apache.lucene.search.IndexSearcher; |
| 26 |
|
import org.apache.lucene.store.RAMDirectory; |
| 27 |
|
import org.apache.lucene.analysis.SimpleAnalyzer; |
| 28 |
|
import org.apache.lucene.document.Document; |
| 29 |
|
import org.apache.lucene.document.Field; |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
@author |
| 34 |
|
@version |
| 35 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (38) |
Complexity: 6 |
Complexity Density: 0.19 |
|
| 36 |
|
public class TestSimilarity extends TestCase { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 37 |
1
|
public TestSimilarity(String name) {... |
| 38 |
1
|
super(name); |
| 39 |
|
} |
| 40 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 7 |
Complexity Density: 1 |
|
| 41 |
|
public static class SimpleSimilarity extends Similarity { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 42 |
2
|
public float lengthNorm(String field, int numTerms) { return 1.0f; }... |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 43 |
4
|
public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }... |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 44 |
99
|
public float tf(float freq) { return freq; }... |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 45 |
2
|
public float sloppyFreq(int distance) { return 2.0f; }... |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 46 |
2
|
public float idf(Collection terms, Searcher searcher) { return 1.0f; }... |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 47 |
3
|
public float idf(int docFreq, int numDocs) { return 1.0f; }... |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 48 |
3
|
public float coord(int overlap, int maxOverlap) { return 1.0f; }... |
| 49 |
|
} |
| 50 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (27) |
Complexity: 1 |
Complexity Density: 0.04 |
1
PASS
|
|
| 51 |
1
|
public void testSimilarity() throws Exception {... |
| 52 |
1
|
RAMDirectory store = new RAMDirectory(); |
| 53 |
1
|
IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true); |
| 54 |
1
|
writer.setSimilarity(new SimpleSimilarity()); |
| 55 |
|
|
| 56 |
1
|
Document d1 = new Document(); |
| 57 |
1
|
d1.add(new Field("field", "a c", Field.Store.YES, Field.Index.TOKENIZED)); |
| 58 |
|
|
| 59 |
1
|
Document d2 = new Document(); |
| 60 |
1
|
d2.add(new Field("field", "a b c", Field.Store.YES, Field.Index.TOKENIZED)); |
| 61 |
|
|
| 62 |
1
|
writer.addDocument(d1); |
| 63 |
1
|
writer.addDocument(d2); |
| 64 |
1
|
writer.optimize(); |
| 65 |
1
|
writer.close(); |
| 66 |
|
|
| 67 |
1
|
Searcher searcher = new IndexSearcher(store); |
| 68 |
1
|
searcher.setSimilarity(new SimpleSimilarity()); |
| 69 |
|
|
| 70 |
1
|
Term a = new Term("field", "a"); |
| 71 |
1
|
Term b = new Term("field", "b"); |
| 72 |
1
|
Term c = new Term("field", "c"); |
| 73 |
|
|
| 74 |
1
|
searcher.search |
| 75 |
|
(new TermQuery(b), |
| 76 |
|
new HitCollector() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 77 |
1
|
public final void collect(int doc, float score) {... |
| 78 |
1
|
assertTrue(score == 1.0f); |
| 79 |
|
} |
| 80 |
|
}); |
| 81 |
|
|
| 82 |
1
|
BooleanQuery bq = new BooleanQuery(); |
| 83 |
1
|
bq.add(new TermQuery(a), BooleanClause.Occur.SHOULD); |
| 84 |
1
|
bq.add(new TermQuery(b), BooleanClause.Occur.SHOULD); |
| 85 |
|
|
| 86 |
1
|
searcher.search |
| 87 |
|
(bq, |
| 88 |
|
new HitCollector() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 89 |
2
|
public final void collect(int doc, float score) {... |
| 90 |
|
|
| 91 |
2
|
assertTrue(score == (float)doc+1); |
| 92 |
|
} |
| 93 |
|
}); |
| 94 |
|
|
| 95 |
1
|
PhraseQuery pq = new PhraseQuery(); |
| 96 |
1
|
pq.add(a); |
| 97 |
1
|
pq.add(c); |
| 98 |
|
|
| 99 |
1
|
searcher.search |
| 100 |
|
(pq, |
| 101 |
|
new HitCollector() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 102 |
1
|
public final void collect(int doc, float score) {... |
| 103 |
|
|
| 104 |
1
|
assertTrue(score == 1.0f); |
| 105 |
|
} |
| 106 |
|
}); |
| 107 |
|
|
| 108 |
1
|
pq.setSlop(2); |
| 109 |
|
|
| 110 |
1
|
searcher.search |
| 111 |
|
(pq, |
| 112 |
|
new HitCollector() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 113 |
2
|
public final void collect(int doc, float score) {... |
| 114 |
|
|
| 115 |
2
|
assertTrue(score == 2.0f); |
| 116 |
|
} |
| 117 |
|
}); |
| 118 |
|
} |
| 119 |
|
} |