001: package org.apache.lucene.search.spans;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.search.IndexSearcher;
021: import org.apache.lucene.search.Query;
022: import org.apache.lucene.search.CheckHits;
023: import org.apache.lucene.store.RAMDirectory;
024: import org.apache.lucene.index.IndexWriter;
025: import org.apache.lucene.index.Term;
026: import org.apache.lucene.analysis.WhitespaceAnalyzer;
027: import org.apache.lucene.document.Document;
028: import org.apache.lucene.document.Field;
029: import org.apache.lucene.util.LuceneTestCase;
030:
031: import java.io.IOException;
032:
033: public class TestSpans extends LuceneTestCase {
034: private IndexSearcher searcher;
035:
036: public static final String field = "field";
037:
038: public void setUp() throws Exception {
039: super .setUp();
040: RAMDirectory directory = new RAMDirectory();
041: IndexWriter writer = new IndexWriter(directory,
042: new WhitespaceAnalyzer(), true);
043: for (int i = 0; i < docFields.length; i++) {
044: Document doc = new Document();
045: doc.add(new Field(field, docFields[i], Field.Store.YES,
046: Field.Index.TOKENIZED));
047: writer.addDocument(doc);
048: }
049: writer.close();
050: searcher = new IndexSearcher(directory);
051: //System.out.println("set up " + getName());
052: }
053:
054: private String[] docFields = { "w1 w2 w3 w4 w5", "w1 w3 w2 w3",
055: "w1 xx w2 yy w3", "w1 w3 xx w2 yy w3", "u2 u2 u1",
056: "u2 xx u2 u1", "u2 u2 xx u1", "u2 xx u2 yy u1",
057: "u2 xx u1 u2", "u2 u1 xx u2", "u1 u2 xx u2",
058: "t1 t2 t1 t3 t2 t3" };
059:
060: public SpanTermQuery makeSpanTermQuery(String text) {
061: return new SpanTermQuery(new Term(field, text));
062: }
063:
064: private void checkHits(Query query, int[] results)
065: throws IOException {
066: CheckHits.checkHits(query, field, searcher, results);
067: }
068:
069: private void orderedSlopTest3SQ(SpanQuery q1, SpanQuery q2,
070: SpanQuery q3, int slop, int[] expectedDocs)
071: throws IOException {
072: boolean ordered = true;
073: SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] { q1, q2,
074: q3 }, slop, ordered);
075: checkHits(snq, expectedDocs);
076: }
077:
078: public void orderedSlopTest3(int slop, int[] expectedDocs)
079: throws IOException {
080: orderedSlopTest3SQ(makeSpanTermQuery("w1"),
081: makeSpanTermQuery("w2"), makeSpanTermQuery("w3"), slop,
082: expectedDocs);
083: }
084:
085: public void orderedSlopTest3Equal(int slop, int[] expectedDocs)
086: throws IOException {
087: orderedSlopTest3SQ(makeSpanTermQuery("w1"),
088: makeSpanTermQuery("w3"), makeSpanTermQuery("w3"), slop,
089: expectedDocs);
090: }
091:
092: public void orderedSlopTest1Equal(int slop, int[] expectedDocs)
093: throws IOException {
094: orderedSlopTest3SQ(makeSpanTermQuery("u2"),
095: makeSpanTermQuery("u2"), makeSpanTermQuery("u1"), slop,
096: expectedDocs);
097: }
098:
099: public void testSpanNearOrdered01() throws Exception {
100: orderedSlopTest3(0, new int[] { 0 });
101: }
102:
103: public void testSpanNearOrdered02() throws Exception {
104: orderedSlopTest3(1, new int[] { 0, 1 });
105: }
106:
107: public void testSpanNearOrdered03() throws Exception {
108: orderedSlopTest3(2, new int[] { 0, 1, 2 });
109: }
110:
111: public void testSpanNearOrdered04() throws Exception {
112: orderedSlopTest3(3, new int[] { 0, 1, 2, 3 });
113: }
114:
115: public void testSpanNearOrdered05() throws Exception {
116: orderedSlopTest3(4, new int[] { 0, 1, 2, 3 });
117: }
118:
119: public void testSpanNearOrderedEqual01() throws Exception {
120: orderedSlopTest3Equal(0, new int[] {});
121: }
122:
123: public void testSpanNearOrderedEqual02() throws Exception {
124: orderedSlopTest3Equal(1, new int[] { 1 });
125: }
126:
127: public void testSpanNearOrderedEqual03() throws Exception {
128: orderedSlopTest3Equal(2, new int[] { 1 });
129: }
130:
131: public void testSpanNearOrderedEqual04() throws Exception {
132: orderedSlopTest3Equal(3, new int[] { 1, 3 });
133: }
134:
135: public void testSpanNearOrderedEqual11() throws Exception {
136: orderedSlopTest1Equal(0, new int[] { 4 });
137: }
138:
139: public void testSpanNearOrderedEqual12() throws Exception {
140: orderedSlopTest1Equal(0, new int[] { 4 });
141: }
142:
143: public void testSpanNearOrderedEqual13() throws Exception {
144: orderedSlopTest1Equal(1, new int[] { 4, 5, 6 });
145: }
146:
147: public void testSpanNearOrderedEqual14() throws Exception {
148: orderedSlopTest1Equal(2, new int[] { 4, 5, 6, 7 });
149: }
150:
151: public void testSpanNearOrderedEqual15() throws Exception {
152: orderedSlopTest1Equal(3, new int[] { 4, 5, 6, 7 });
153: }
154:
155: public void testSpanNearOrderedOverlap() throws Exception {
156: boolean ordered = true;
157: int slop = 1;
158: SpanNearQuery snq = new SpanNearQuery(new SpanQuery[] {
159: makeSpanTermQuery("t1"), makeSpanTermQuery("t2"),
160: makeSpanTermQuery("t3") }, slop, ordered);
161: Spans spans = snq.getSpans(searcher.getIndexReader());
162:
163: assertTrue("first range", spans.next());
164: assertEquals("first doc", 11, spans.doc());
165: assertEquals("first start", 0, spans.start());
166: assertEquals("first end", 4, spans.end());
167:
168: assertTrue("second range", spans.next());
169: assertEquals("second doc", 11, spans.doc());
170: assertEquals("second start", 2, spans.start());
171: assertEquals("second end", 6, spans.end());
172:
173: assertFalse("third range", spans.next());
174: }
175:
176: private Spans orSpans(String[] terms) throws Exception {
177: SpanQuery[] sqa = new SpanQuery[terms.length];
178: for (int i = 0; i < terms.length; i++) {
179: sqa[i] = makeSpanTermQuery(terms[i]);
180: }
181: return (new SpanOrQuery(sqa)).getSpans(searcher
182: .getIndexReader());
183: }
184:
185: private void tstNextSpans(Spans spans, int doc, int start, int end)
186: throws Exception {
187: assertTrue("next", spans.next());
188: assertEquals("doc", doc, spans.doc());
189: assertEquals("start", start, spans.start());
190: assertEquals("end", end, spans.end());
191: }
192:
193: public void testSpanOrEmpty() throws Exception {
194: Spans spans = orSpans(new String[0]);
195: assertFalse("empty next", spans.next());
196: }
197:
198: public void testSpanOrSingle() throws Exception {
199: Spans spans = orSpans(new String[] { "w5" });
200: tstNextSpans(spans, 0, 4, 5);
201: assertFalse("final next", spans.next());
202: }
203:
204: public void testSpanOrDouble() throws Exception {
205: Spans spans = orSpans(new String[] { "w5", "yy" });
206: tstNextSpans(spans, 0, 4, 5);
207: tstNextSpans(spans, 2, 3, 4);
208: tstNextSpans(spans, 3, 4, 5);
209: tstNextSpans(spans, 7, 3, 4);
210: assertFalse("final next", spans.next());
211: }
212:
213: public void testSpanOrDoubleSkip() throws Exception {
214: Spans spans = orSpans(new String[] { "w5", "yy" });
215: assertTrue("initial skipTo", spans.skipTo(3));
216: assertEquals("doc", 3, spans.doc());
217: assertEquals("start", 4, spans.start());
218: assertEquals("end", 5, spans.end());
219: tstNextSpans(spans, 7, 3, 4);
220: assertFalse("final next", spans.next());
221: }
222:
223: public void testSpanOrUnused() throws Exception {
224: Spans spans = orSpans(new String[] { "w5", "unusedTerm", "yy" });
225: tstNextSpans(spans, 0, 4, 5);
226: tstNextSpans(spans, 2, 3, 4);
227: tstNextSpans(spans, 3, 4, 5);
228: tstNextSpans(spans, 7, 3, 4);
229: assertFalse("final next", spans.next());
230: }
231:
232: public void testSpanOrTripleSameDoc() throws Exception {
233: Spans spans = orSpans(new String[] { "t1", "t2", "t3" });
234: tstNextSpans(spans, 11, 0, 1);
235: tstNextSpans(spans, 11, 1, 2);
236: tstNextSpans(spans, 11, 2, 3);
237: tstNextSpans(spans, 11, 3, 4);
238: tstNextSpans(spans, 11, 4, 5);
239: tstNextSpans(spans, 11, 5, 6);
240: assertFalse("final next", spans.next());
241: }
242: }
|