pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / spans / TestNearSpansOrdered.java
1 package org.apache.lucene.search.spans;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.analysis.MockAnalyzer;
21
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.queryParser.QueryParser;
28 import org.apache.lucene.search.CheckHits;
29 import org.apache.lucene.search.Explanation;
30 import org.apache.lucene.search.IndexSearcher;
31 import org.apache.lucene.search.Weight;
32 import org.apache.lucene.search.Scorer;
33 import org.apache.lucene.store.Directory;
34 import org.apache.lucene.util.LuceneTestCase;
35
36 public class TestNearSpansOrdered extends LuceneTestCase {
37   protected IndexSearcher searcher;
38   protected Directory directory;
39   protected IndexReader reader;
40
41   public static final String FIELD = "field";
42   public static final QueryParser qp =
43     new QueryParser(TEST_VERSION_CURRENT, FIELD, new MockAnalyzer(random));
44
45   @Override
46   public void tearDown() throws Exception {
47     searcher.close();
48     reader.close();
49     directory.close();
50     super.tearDown();
51   }
52   
53   @Override
54   public void setUp() throws Exception {
55     super.setUp();
56     directory = newDirectory();
57     RandomIndexWriter writer= new RandomIndexWriter(random, directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
58     for (int i = 0; i < docFields.length; i++) {
59       Document doc = new Document();
60       doc.add(newField(FIELD, docFields[i], Field.Store.NO, Field.Index.ANALYZED));
61       writer.addDocument(doc);
62     }
63     reader = writer.getReader();
64     writer.close();
65     searcher = newSearcher(reader);
66   }
67
68   protected String[] docFields = {
69     "w1 w2 w3 w4 w5",
70     "w1 w3 w2 w3 zz",
71     "w1 xx w2 yy w3",
72     "w1 w3 xx w2 yy w3 zz"
73   };
74
75   protected SpanNearQuery makeQuery(String s1, String s2, String s3,
76                                     int slop, boolean inOrder) {
77     return new SpanNearQuery
78       (new SpanQuery[] {
79         new SpanTermQuery(new Term(FIELD, s1)),
80         new SpanTermQuery(new Term(FIELD, s2)),
81         new SpanTermQuery(new Term(FIELD, s3)) },
82        slop,
83        inOrder);
84   }
85   protected SpanNearQuery makeQuery() {
86     return makeQuery("w1","w2","w3",1,true);
87   }
88   
89   public void testSpanNearQuery() throws Exception {
90     SpanNearQuery q = makeQuery();
91     CheckHits.checkHits(random, q, FIELD, searcher, new int[] {0,1});
92   }
93
94   public String s(Spans span) {
95     return s(span.doc(), span.start(), span.end());
96   }
97   public String s(int doc, int start, int end) {
98     return "s(" + doc + "," + start + "," + end +")";
99   }
100   
101   public void testNearSpansNext() throws Exception {
102     SpanNearQuery q = makeQuery();
103     Spans span = q.getSpans(searcher.getIndexReader());
104     assertEquals(true, span.next());
105     assertEquals(s(0,0,3), s(span));
106     assertEquals(true, span.next());
107     assertEquals(s(1,0,4), s(span));
108     assertEquals(false, span.next());
109   }
110
111   /**
112    * test does not imply that skipTo(doc+1) should work exactly the
113    * same as next -- it's only applicable in this case since we know doc
114    * does not contain more than one span
115    */
116   public void testNearSpansSkipToLikeNext() throws Exception {
117     SpanNearQuery q = makeQuery();
118     Spans span = q.getSpans(searcher.getIndexReader());
119     assertEquals(true, span.skipTo(0));
120     assertEquals(s(0,0,3), s(span));
121     assertEquals(true, span.skipTo(1));
122     assertEquals(s(1,0,4), s(span));
123     assertEquals(false, span.skipTo(2));
124   }
125   
126   public void testNearSpansNextThenSkipTo() throws Exception {
127     SpanNearQuery q = makeQuery();
128     Spans span = q.getSpans(searcher.getIndexReader());
129     assertEquals(true, span.next());
130     assertEquals(s(0,0,3), s(span));
131     assertEquals(true, span.skipTo(1));
132     assertEquals(s(1,0,4), s(span));
133     assertEquals(false, span.next());
134   }
135   
136   public void testNearSpansNextThenSkipPast() throws Exception {
137     SpanNearQuery q = makeQuery();
138     Spans span = q.getSpans(searcher.getIndexReader());
139     assertEquals(true, span.next());
140     assertEquals(s(0,0,3), s(span));
141     assertEquals(false, span.skipTo(2));
142   }
143   
144   public void testNearSpansSkipPast() throws Exception {
145     SpanNearQuery q = makeQuery();
146     Spans span = q.getSpans(searcher.getIndexReader());
147     assertEquals(false, span.skipTo(2));
148   }
149   
150   public void testNearSpansSkipTo0() throws Exception {
151     SpanNearQuery q = makeQuery();
152     Spans span = q.getSpans(searcher.getIndexReader());
153     assertEquals(true, span.skipTo(0));
154     assertEquals(s(0,0,3), s(span));
155   }
156
157   public void testNearSpansSkipTo1() throws Exception {
158     SpanNearQuery q = makeQuery();
159     Spans span = q.getSpans(searcher.getIndexReader());
160     assertEquals(true, span.skipTo(1));
161     assertEquals(s(1,0,4), s(span));
162   }
163
164   /**
165    * not a direct test of NearSpans, but a demonstration of how/when
166    * this causes problems
167    */
168   public void testSpanNearScorerSkipTo1() throws Exception {
169     SpanNearQuery q = makeQuery();
170     Weight w = searcher.createNormalizedWeight(q);
171     Scorer s = w.scorer(searcher.getIndexReader(), true, false);
172     assertEquals(1, s.advance(1));
173   }
174   
175   /**
176    * not a direct test of NearSpans, but a demonstration of how/when
177    * this causes problems
178    */
179   public void testSpanNearScorerExplain() throws Exception {
180     SpanNearQuery q = makeQuery();
181     Explanation e = searcher.explain(q, 1);
182     assertTrue("Scorer explanation value for doc#1 isn't positive: "
183                + e.toString(),
184                0.0f < e.getValue());
185   }
186   
187 }