| 1 |
|
package org.apache.lucene.search.spans; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import org.apache.lucene.search.Explanation; |
| 20 |
|
import org.apache.lucene.search.IndexSearcher; |
| 21 |
|
import org.apache.lucene.search.Scorer; |
| 22 |
|
import org.apache.lucene.search.Weight; |
| 23 |
|
import org.apache.lucene.search.CheckHits; |
| 24 |
|
import org.apache.lucene.store.RAMDirectory; |
| 25 |
|
|
| 26 |
|
import org.apache.lucene.index.IndexWriter; |
| 27 |
|
import org.apache.lucene.index.Term; |
| 28 |
|
|
| 29 |
|
import org.apache.lucene.analysis.WhitespaceAnalyzer; |
| 30 |
|
|
| 31 |
|
import org.apache.lucene.document.Document; |
| 32 |
|
import org.apache.lucene.document.Field; |
| 33 |
|
|
| 34 |
|
import org.apache.lucene.queryParser.QueryParser; |
| 35 |
|
|
| 36 |
|
import junit.framework.TestCase; |
| 37 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (80) |
Complexity: 17 |
Complexity Density: 0.27 |
|
| 38 |
|
public class TestNearSpansOrdered extends TestCase { |
| 39 |
|
protected IndexSearcher searcher; |
| 40 |
|
|
| 41 |
|
public static final String FIELD = "field"; |
| 42 |
|
public static final QueryParser qp = |
| 43 |
|
new QueryParser(FIELD, new WhitespaceAnalyzer()); |
| 44 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 45 |
10
|
public void tearDown() throws Exception {... |
| 46 |
10
|
searcher.close(); |
| 47 |
|
} |
| 48 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 49 |
10
|
public void setUp() throws Exception {... |
| 50 |
10
|
RAMDirectory directory = new RAMDirectory(); |
| 51 |
10
|
IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true); |
| 52 |
50
|
for (int i = 0; i < docFields.length; i++) { |
| 53 |
40
|
Document doc = new Document(); |
| 54 |
40
|
doc.add(new Field(FIELD, docFields[i], Field.Store.NO, Field.Index.TOKENIZED)); |
| 55 |
40
|
writer.addDocument(doc); |
| 56 |
|
} |
| 57 |
10
|
writer.close(); |
| 58 |
10
|
searcher = new IndexSearcher(directory); |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
protected String[] docFields = { |
| 62 |
|
"w1 w2 w3 w4 w5", |
| 63 |
|
"w1 w3 w2 w3 zz", |
| 64 |
|
"w1 xx w2 yy w3", |
| 65 |
|
"w1 w3 xx w2 yy w3 zz" |
| 66 |
|
}; |
| 67 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 68 |
10
|
protected SpanNearQuery makeQuery(String s1, String s2, String s3,... |
| 69 |
|
int slop, boolean inOrder) { |
| 70 |
10
|
return new SpanNearQuery |
| 71 |
|
(new SpanQuery[] { |
| 72 |
|
new SpanTermQuery(new Term(FIELD, s1)), |
| 73 |
|
new SpanTermQuery(new Term(FIELD, s2)), |
| 74 |
|
new SpanTermQuery(new Term(FIELD, s3)) }, |
| 75 |
|
slop, |
| 76 |
|
inOrder); |
| 77 |
|
} |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 78 |
10
|
protected SpanNearQuery makeQuery() {... |
| 79 |
10
|
return makeQuery("w1","w2","w3",1,true); |
| 80 |
|
} |
| 81 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1
PASS
|
|
| 82 |
1
|
public void testSpanNearQuery() throws Exception {... |
| 83 |
1
|
SpanNearQuery q = makeQuery(); |
| 84 |
1
|
CheckHits.checkHits(q, FIELD, searcher, new int[] {0,1}); |
| 85 |
|
} |
| 86 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 87 |
9
|
public String s(Spans span) {... |
| 88 |
9
|
return s(span.doc(), span.start(), span.end()); |
| 89 |
|
} |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 90 |
18
|
public String s(int doc, int start, int end) {... |
| 91 |
18
|
return "s(" + doc + "," + start + "," + end +")"; |
| 92 |
|
} |
| 93 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 94 |
1
|
public void testNearSpansNext() throws Exception {... |
| 95 |
1
|
SpanNearQuery q = makeQuery(); |
| 96 |
1
|
Spans span = q.getSpans(searcher.getIndexReader()); |
| 97 |
1
|
assertEquals(true, span.next()); |
| 98 |
1
|
assertEquals(s(0,0,3), s(span)); |
| 99 |
1
|
assertEquals(true, span.next()); |
| 100 |
1
|
assertEquals(s(1,0,4), s(span)); |
| 101 |
1
|
assertEquals(false, span.next()); |
| 102 |
|
} |
| 103 |
|
|
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
|
| 108 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 109 |
1
|
public void testNearSpansSkipToLikeNext() throws Exception {... |
| 110 |
1
|
SpanNearQuery q = makeQuery(); |
| 111 |
1
|
Spans span = q.getSpans(searcher.getIndexReader()); |
| 112 |
1
|
assertEquals(true, span.skipTo(0)); |
| 113 |
1
|
assertEquals(s(0,0,3), s(span)); |
| 114 |
1
|
assertEquals(true, span.skipTo(1)); |
| 115 |
1
|
assertEquals(s(1,0,4), s(span)); |
| 116 |
1
|
assertEquals(false, span.skipTo(2)); |
| 117 |
|
} |
| 118 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 119 |
1
|
public void testNearSpansNextThenSkipTo() throws Exception {... |
| 120 |
1
|
|