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.Explanation;
021: import org.apache.lucene.search.IndexSearcher;
022: import org.apache.lucene.search.Scorer;
023: import org.apache.lucene.search.Weight;
024: import org.apache.lucene.search.CheckHits;
025: import org.apache.lucene.store.RAMDirectory;
026:
027: import org.apache.lucene.index.IndexWriter;
028: import org.apache.lucene.index.Term;
029:
030: import org.apache.lucene.analysis.WhitespaceAnalyzer;
031:
032: import org.apache.lucene.document.Document;
033: import org.apache.lucene.document.Field;
034:
035: import org.apache.lucene.queryParser.QueryParser;
036:
037: import org.apache.lucene.util.LuceneTestCase;
038:
039: public class TestNearSpansOrdered extends LuceneTestCase {
040: protected IndexSearcher searcher;
041:
042: public static final String FIELD = "field";
043: public static final QueryParser qp = new QueryParser(FIELD,
044: new WhitespaceAnalyzer());
045:
046: public void tearDown() throws Exception {
047: super .tearDown();
048: searcher.close();
049: }
050:
051: public void setUp() throws Exception {
052: super .setUp();
053: RAMDirectory directory = new RAMDirectory();
054: IndexWriter writer = new IndexWriter(directory,
055: new WhitespaceAnalyzer(), true);
056: for (int i = 0; i < docFields.length; i++) {
057: Document doc = new Document();
058: doc.add(new Field(FIELD, docFields[i], Field.Store.NO,
059: Field.Index.TOKENIZED));
060: writer.addDocument(doc);
061: }
062: writer.close();
063: searcher = new IndexSearcher(directory);
064: }
065:
066: protected String[] docFields = { "w1 w2 w3 w4 w5",
067: "w1 w3 w2 w3 zz", "w1 xx w2 yy w3", "w1 w3 xx w2 yy w3 zz" };
068:
069: protected SpanNearQuery makeQuery(String s1, String s2, String s3,
070: int slop, boolean inOrder) {
071: return new SpanNearQuery(new SpanQuery[] {
072: new SpanTermQuery(new Term(FIELD, s1)),
073: new SpanTermQuery(new Term(FIELD, s2)),
074: new SpanTermQuery(new Term(FIELD, s3)) }, slop, inOrder);
075: }
076:
077: protected SpanNearQuery makeQuery() {
078: return makeQuery("w1", "w2", "w3", 1, true);
079: }
080:
081: public void testSpanNearQuery() throws Exception {
082: SpanNearQuery q = makeQuery();
083: CheckHits.checkHits(q, FIELD, searcher, new int[] { 0, 1 });
084: }
085:
086: public String s(Spans span) {
087: return s(span.doc(), span.start(), span.end());
088: }
089:
090: public String s(int doc, int start, int end) {
091: return "s(" + doc + "," + start + "," + end + ")";
092: }
093:
094: public void testNearSpansNext() throws Exception {
095: SpanNearQuery q = makeQuery();
096: Spans span = q.getSpans(searcher.getIndexReader());
097: assertEquals(true, span.next());
098: assertEquals(s(0, 0, 3), s(span));
099: assertEquals(true, span.next());
100: assertEquals(s(1, 0, 4), s(span));
101: assertEquals(false, span.next());
102: }
103:
104: /**
105: * test does not imply that skipTo(doc+1) should work exactly the
106: * same as next -- it's only applicable in this case since we know doc
107: * does not contain more than one span
108: */
109: public void testNearSpansSkipToLikeNext() throws Exception {
110: SpanNearQuery q = makeQuery();
111: Spans span = q.getSpans(searcher.getIndexReader());
112: assertEquals(true, span.skipTo(0));
113: assertEquals(s(0, 0, 3), s(span));
114: assertEquals(true, span.skipTo(1));
115: assertEquals(s(1, 0, 4), s(span));
116: assertEquals(false, span.skipTo(2));
117: }
118:
119: public void testNearSpansNextThenSkipTo() throws Exception {
120: SpanNearQuery q = makeQuery();
121: Spans span = q.getSpans(searcher.getIndexReader());
122: assertEquals(true, span.next());
123: assertEquals(s(0, 0, 3), s(span));
124: assertEquals(true, span.skipTo(1));
125: assertEquals(s(1, 0, 4), s(span));
126: assertEquals(false, span.next());
127: }
128:
129: public void testNearSpansNextThenSkipPast() throws Exception {
130: SpanNearQuery q = makeQuery();
131: Spans span = q.getSpans(searcher.getIndexReader());
132: assertEquals(true, span.next());
133: assertEquals(s(0, 0, 3), s(span));
134: assertEquals(false, span.skipTo(2));
135: }
136:
137: public void testNearSpansSkipPast() throws Exception {
138: SpanNearQuery q = makeQuery();
139: Spans span = q.getSpans(searcher.getIndexReader());
140: assertEquals(false, span.skipTo(2));
141: }
142:
143: public void testNearSpansSkipTo0() throws Exception {
144: SpanNearQuery q = makeQuery();
145: Spans span = q.getSpans(searcher.getIndexReader());
146: assertEquals(true, span.skipTo(0));
147: assertEquals(s(0, 0, 3), s(span));
148: }
149:
150: public void testNearSpansSkipTo1() throws Exception {
151: SpanNearQuery q = makeQuery();
152: Spans span = q.getSpans(searcher.getIndexReader());
153: assertEquals(true, span.skipTo(1));
154: assertEquals(s(1, 0, 4), s(span));
155: }
156:
157: /**
158: * not a direct test of NearSpans, but a demonstration of how/when
159: * this causes problems
160: */
161: public void testSpanNearScorerSkipTo1() throws Exception {
162: SpanNearQuery q = makeQuery();
163: Weight w = q.createWeight(searcher);
164: Scorer s = w.scorer(searcher.getIndexReader());
165: assertEquals(true, s.skipTo(1));
166: assertEquals(1, s.doc());
167: }
168:
169: /**
170: * not a direct test of NearSpans, but a demonstration of how/when
171: * this causes problems
172: */
173: public void testSpanNearScorerExplain() throws Exception {
174: SpanNearQuery q = makeQuery();
175: Weight w = q.createWeight(searcher);
176: Scorer s = w.scorer(searcher.getIndexReader());
177: Explanation e = s.explain(1);
178: assertTrue(
179: "Scorer explanation value for doc#1 isn't positive: "
180: + e.toString(), 0.0f < e.getValue());
181: }
182:
183: }
|