| 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.Document; |
| 22 |
|
import org.apache.lucene.document.Field; |
| 23 |
|
import org.apache.lucene.index.*; |
| 24 |
|
import org.apache.lucene.store.Directory; |
| 25 |
|
import org.apache.lucene.store.RAMDirectory; |
| 26 |
|
import org.apache.lucene.util.English; |
| 27 |
|
|
| 28 |
|
import java.io.IOException; |
| 29 |
|
import java.util.HashMap; |
| 30 |
|
import java.util.Map; |
| 31 |
|
|
|
|
|
| 90.6% |
Uncovered Elements: 18 (192) |
Complexity: 32 |
Complexity Density: 0.22 |
|
| 32 |
|
public class TestTermVectors extends TestCase { |
| 33 |
|
private IndexSearcher searcher; |
| 34 |
|
private RAMDirectory directory = new RAMDirectory(); |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 35 |
5
|
public TestTermVectors(String s) {... |
| 36 |
5
|
super(s); |
| 37 |
|
} |
| 38 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 6 |
Complexity Density: 0.35 |
|
| 39 |
5
|
public void setUp() throws Exception { ... |
| 40 |
5
|
IndexWriter writer |
| 41 |
|
= new IndexWriter(directory, new SimpleAnalyzer(), true); |
| 42 |
|
|
| 43 |
|
|
| 44 |
5005
|
for (int i = 0; i < 1000; i++) { |
| 45 |
5000
|
Document doc = new Document(); |
| 46 |
5000
|
Field.TermVector termVector; |
| 47 |
5000
|
int mod3 = i % 3; |
| 48 |
5000
|
int mod2 = i % 2; |
| 49 |
5000
|
if (mod2 == 0 && mod3 == 0){ |
| 50 |
835
|
termVector = Field.TermVector.WITH_POSITIONS_OFFSETS; |
| 51 |
|
} |
| 52 |
4165
|
else if (mod2 == 0){ |
| 53 |
1665
|
termVector = Field.TermVector.WITH_POSITIONS; |
| 54 |
|
} |
| 55 |
2500
|
else if (mod3 == 0){ |
| 56 |
835
|
termVector = Field.TermVector.WITH_OFFSETS; |
| 57 |
|
} |
| 58 |
|
else { |
| 59 |
1665
|
termVector = Field.TermVector.YES; |
| 60 |
|
} |
| 61 |
5000
|
doc.add(new Field("field", English.intToEnglish(i), |
| 62 |
|
Field.Store.YES, Field.Index.TOKENIZED, termVector)); |
| 63 |
5000
|
writer.addDocument(doc); |
| 64 |
|
} |
| 65 |
5
|
writer.close(); |
| 66 |
5
|
searcher = new IndexSearcher(directory); |
| 67 |
|
} |
| 68 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 69 |
5
|
protected void tearDown() {... |
| 70 |
|
|
| 71 |
|
} |
| 72 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
| 73 |
1
|
public void test() {... |
| 74 |
1
|
assertTrue(searcher != null); |
| 75 |
|
} |
| 76 |
|
|
|
|
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
1
PASS
|
|
| 77 |
1
|
public void testTermVectors() {... |
| 78 |
1
|
Query query = new TermQuery(new Term("field", "seventy")); |
| 79 |
1
|
try { |
| 80 |
1
|
Hits hits = searcher.search(query); |
| 81 |
1
|
assertEquals(100, hits.length()); |
| 82 |
|
|
| 83 |
101
|
for (int i = 0; i < hits.length(); i++) |
| 84 |
|
{ |
| 85 |
100
|
TermFreqVector [] vector = searcher.reader.getTermFreqVectors(hits.id(i)); |
| 86 |
100
|
assertTrue(vector != null); |
| 87 |
100
|
assertTrue(vector.length == 1); |
| 88 |
|
} |
| 89 |
|
} catch (IOException e) { |
| 90 |
0
|
assertTrue(false); |
| 91 |
|
} |
| 92 |
|
} |
| 93 |
|
|
|
|
|
| 70.8% |
Uncovered Elements: 14 (48) |
Complexity: 9 |
Complexity Density: 0.26 |
1
PASS
|
|
| 94 |
1
|
public void testTermPositionVectors() {... |
| 95 |
1
|
Query query = new TermQuery(new Term("field", "zero")); |
| 96 |
1
|
try { |
| 97 |
1
|
Hits hits = searcher.search(query); |
| 98 |
1
|
assertEquals(1, hits.length()); |
| 99 |
|
|
| 100 |
2
|
for (int i = 0; i < hits.length(); i++) |
| 101 |
|
{ |
| 102 |
1
|
TermFreqVector [] vector = searcher.reader.getTermFreqVectors(hits.id(i)); |
| 103 |
1
|
assertTrue(vector != null); |
| 104 |
1
|
assertTrue(vector.length == 1); |
| 105 |
|
|
| 106 |
1
|
boolean shouldBePosVector = (hits.id(i) % 2 == 0) ? true : false; |
| 107 |
1
|
assertTrue((shouldBePosVector == false) || (shouldBePosVector == true && (vector[0] instanceof TermPositionVector == true))); |
| 108 |
|
|
| 109 |
1
|
boolean shouldBeOffVector = (hits.id(i) % 3 == 0) ? true : false; |
| 110 |
1
|
assertTrue((shouldBeOffVector == false) || (shouldBeOffVector == true && (vector[0] instanceof TermPositionVector == true))); |
| 111 |
|
|
| 112 |
1
|
if(shouldBePosVector || shouldBeOffVector){ |
| 113 |
1
|
TermPositionVector posVec = (TermPositionVector)vector[0]; |
| 114 |
1
|
String [] terms = posVec.getTerms(); |
| 115 |
1
|
assertTrue(terms != null && terms.length > 0); |
| 116 |
|
|
| 117 |
2
|
for (int j = 0; j < terms.length; j++) { |
| 118 |
1
|
int [] positions = posVec.getTermPositions(j); |
| 119 |
1
|
TermVectorOffsetInfo [] offsets = posVec.getOffsets(j); |
| 120 |
|
|
| 121 |
1
|
if(shouldBePosVector){ |
| 122 |
1
|
assertTrue(positions != null); |
| 123 |
1
|
assertTrue(positions.length > 0); |
| 124 |
|
} |
| 125 |
|
else |
| 126 |
0
|
assertTrue(positions == null); |
| 127 |
|
|
| 128 |
1
|
if(shouldBeOffVector){ |
| 129 |
1
|
assertTrue(offsets != null); |
| 130 |
1
|
assertTrue(offsets.length > 0); |
| 131 |
|
} |
| 132 |
|
else |
| 133 |
0
|
assertTrue(offsets == null); |
| 134 |
|
} |
| 135 |
|
} |
| 136 |
|
else{ |
| 137 |
0
|
try{ |
| 138 |
0
|
TermPositionVector posVec = (TermPositionVector)vector[0]; |
| 139 |
0
|
assertTrue(false); |
| 140 |
|
} |
| 141 |
|
catch(ClassCastException ignore){ |
| 142 |
0
|
TermFreqVector freqVec = vector[0]; |
| 143 |
0
|
String [] terms = freqVec.getTerms(); |
| 144 |
0
|
assertTrue(terms != null && terms.length > 0); |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
} |
| 148 |
|
|
| 149 |
|
} |
| 150 |
|
} catch (IOException e) { |
| 151 |
0
|
assertTrue(false); |
| 152 |
|
} |
| 153 |
|
} |
| 154 |
|
|
|
|
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
1
PASS
|
|
| 155 |
1
|
public void testTermOffsetVectors() {... |
| 156 |
1
|
Query query = new TermQuery(new Term("field", "fifty")); |
| 157 |
1
|
try { |
| 158 |
1
|
Hits hits = searcher.search(query); |
| 159 |
1
|
assertEquals(100, hits.length()); |
| 160 |
|
|
| 161 |
101
|
for (int i = 0; i < hits.length(); i++) |
| 162 |
|
{ |
| 163 |
100
|
TermFreqVector [] vector = searcher.reader.getTermFreqVectors(hits.id(i)); |
| 164 |
100
|
assertTrue(vector != null); |
| 165 |
100
|
assertTrue(vector.length == 1); |
| 166 |
|
|
| 167 |
|
|
|