pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / TestExplanations.java
1 package org.apache.lucene.search;
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.queryParser.QueryParser;
21 import org.apache.lucene.queryParser.ParseException;
22 import org.apache.lucene.analysis.MockAnalyzer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.RandomIndexWriter;
27 import org.apache.lucene.index.Term;
28 import org.apache.lucene.search.spans.SpanFirstQuery;
29 import org.apache.lucene.search.spans.SpanNearQuery;
30 import org.apache.lucene.search.spans.SpanNotQuery;
31 import org.apache.lucene.search.spans.SpanOrQuery;
32 import org.apache.lucene.search.spans.SpanQuery;
33 import org.apache.lucene.search.spans.SpanTermQuery;
34 import org.apache.lucene.store.Directory;
35 import org.apache.lucene.util.LuceneTestCase;
36
37 /**
38  * Tests primitive queries (ie: that rewrite to themselves) to
39  * insure they match the expected set of docs, and that the score of each
40  * match is equal to the value of the scores explanation.
41  *
42  * <p>
43  * The assumption is that if all of the "primitive" queries work well,
44  * then anything that rewrites to a primitive will work well also.
45  * </p>
46  *
47  * @see "Subclasses for actual tests"
48  */
49 public class TestExplanations extends LuceneTestCase {
50   protected IndexSearcher searcher;
51   protected IndexReader reader;
52   protected Directory directory;
53   
54   public static final String KEY = "KEY";
55   // boost on this field is the same as the iterator for the doc
56   public static final String FIELD = "field";
57   // same contents, but no field boost
58   public static final String ALTFIELD = "alt";
59   public static final QueryParser qp =
60     new QueryParser(TEST_VERSION_CURRENT, FIELD, new MockAnalyzer(random));
61
62   @Override
63   public void tearDown() throws Exception {
64     searcher.close();
65     reader.close();
66     directory.close();
67     super.tearDown();
68   }
69   
70   @Override
71   public void setUp() throws Exception {
72     super.setUp();
73     directory = newDirectory();
74     RandomIndexWriter writer= new RandomIndexWriter(random, directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
75     for (int i = 0; i < docFields.length; i++) {
76       Document doc = new Document();
77       doc.add(newField(KEY, ""+i, Field.Store.NO, Field.Index.NOT_ANALYZED));
78       Field f = newField(FIELD, docFields[i], Field.Store.NO, Field.Index.ANALYZED);
79       f.setBoost(i);
80       doc.add(f);
81       doc.add(newField(ALTFIELD, docFields[i], Field.Store.NO, Field.Index.ANALYZED));
82       writer.addDocument(doc);
83     }
84     reader = writer.getReader();
85     writer.close();
86     searcher = newSearcher(reader);
87   }
88
89   protected String[] docFields = {
90     "w1 w2 w3 w4 w5",
91     "w1 w3 w2 w3 zz",
92     "w1 xx w2 yy w3",
93     "w1 w3 xx w2 yy w3 zz"
94   };
95
96   public Query makeQuery(String queryText) throws ParseException {
97     return qp.parse(queryText);
98   }
99
100   /** check the expDocNrs first, then check the query (and the explanations) */
101   public void qtest(String queryText, int[] expDocNrs) throws Exception {
102     qtest(makeQuery(queryText), expDocNrs);
103   }
104   
105   /** check the expDocNrs first, then check the query (and the explanations) */
106   public void qtest(Query q, int[] expDocNrs) throws Exception {
107     CheckHits.checkHitCollector(random, q, FIELD, searcher, expDocNrs);
108   }
109
110   /**
111    * Tests a query using qtest after wrapping it with both optB and reqB
112    * @see #qtest
113    * @see #reqB
114    * @see #optB
115    */
116   public void bqtest(Query q, int[] expDocNrs) throws Exception {
117     qtest(reqB(q), expDocNrs);
118     qtest(optB(q), expDocNrs);
119   }
120   /**
121    * Tests a query using qtest after wrapping it with both optB and reqB
122    * @see #qtest
123    * @see #reqB
124    * @see #optB
125    */
126   public void bqtest(String queryText, int[] expDocNrs) throws Exception {
127     bqtest(makeQuery(queryText), expDocNrs);
128   }
129   
130   /** 
131    * Convenience subclass of FieldCacheTermsFilter
132    */
133   public static class ItemizedFilter extends FieldCacheTermsFilter {
134     private static String[] int2str(int [] terms) {
135       String [] out = new String[terms.length];
136       for (int i = 0; i < terms.length; i++) {
137         out[i] = ""+terms[i];
138       }
139       return out;
140     }
141     public ItemizedFilter(String keyField, int [] keys) {
142       super(keyField, int2str(keys));
143     }
144     public ItemizedFilter(int [] keys) {
145       super(KEY, int2str(keys));
146     }
147   }
148
149   /** helper for generating MultiPhraseQueries */
150   public static Term[] ta(String[] s) {
151     Term[] t = new Term[s.length];
152     for (int i = 0; i < s.length; i++) {
153       t[i] = new Term(FIELD, s[i]);
154     }
155     return t;
156   }
157
158   /** MACRO for SpanTermQuery */
159   public SpanTermQuery st(String s) {
160     return new SpanTermQuery(new Term(FIELD,s));
161   }
162   
163   /** MACRO for SpanNotQuery */
164   public SpanNotQuery snot(SpanQuery i, SpanQuery e) {
165     return new SpanNotQuery(i,e);
166   }
167
168   /** MACRO for SpanOrQuery containing two SpanTerm queries */
169   public SpanOrQuery sor(String s, String e) {
170     return sor(st(s), st(e));
171   }
172   /** MACRO for SpanOrQuery containing two SpanQueries */
173   public SpanOrQuery sor(SpanQuery s, SpanQuery e) {
174     return new SpanOrQuery(new SpanQuery[] { s, e });
175   }
176   
177   /** MACRO for SpanOrQuery containing three SpanTerm queries */
178   public SpanOrQuery sor(String s, String m, String e) {
179     return sor(st(s), st(m), st(e));
180   }
181   /** MACRO for SpanOrQuery containing two SpanQueries */
182   public SpanOrQuery sor(SpanQuery s, SpanQuery m, SpanQuery e) {
183     return new SpanOrQuery(new SpanQuery[] { s, m, e });
184   }
185   
186   /** MACRO for SpanNearQuery containing two SpanTerm queries */
187   public SpanNearQuery snear(String s, String e, int slop, boolean inOrder) {
188     return snear(st(s), st(e), slop, inOrder);
189   }
190   /** MACRO for SpanNearQuery containing two SpanQueries */
191   public SpanNearQuery snear(SpanQuery s, SpanQuery e,
192                              int slop, boolean inOrder) {
193     return new SpanNearQuery(new SpanQuery[] { s, e }, slop, inOrder);
194   }
195   
196   
197   /** MACRO for SpanNearQuery containing three SpanTerm queries */
198   public SpanNearQuery snear(String s, String m, String e,
199                              int slop, boolean inOrder) {
200     return snear(st(s), st(m), st(e), slop, inOrder);
201   }
202   /** MACRO for SpanNearQuery containing three SpanQueries */
203   public SpanNearQuery snear(SpanQuery s, SpanQuery m, SpanQuery e,
204                              int slop, boolean inOrder) {
205     return new SpanNearQuery(new SpanQuery[] { s, m, e }, slop, inOrder);
206   }
207   
208   /** MACRO for SpanFirst(SpanTermQuery) */
209   public SpanFirstQuery sf(String s, int b) {
210     return new SpanFirstQuery(st(s), b);
211   }
212
213   /**
214    * MACRO: Wraps a Query in a BooleanQuery so that it is optional, along
215    * with a second prohibited clause which will never match anything
216    */
217   public Query optB(String q) throws Exception {
218     return optB(makeQuery(q));
219   }
220   /**
221    * MACRO: Wraps a Query in a BooleanQuery so that it is optional, along
222    * with a second prohibited clause which will never match anything
223    */
224   public Query optB(Query q) throws Exception {
225     BooleanQuery bq = new BooleanQuery(true);
226     bq.add(q, BooleanClause.Occur.SHOULD);
227     bq.add(new TermQuery(new Term("NEVER","MATCH")), BooleanClause.Occur.MUST_NOT);
228     return bq;
229   }
230   
231   /**
232    * MACRO: Wraps a Query in a BooleanQuery so that it is required, along
233    * with a second optional clause which will match everything
234    */
235   public Query reqB(String q) throws Exception {
236     return reqB(makeQuery(q));
237   }
238   /**
239    * MACRO: Wraps a Query in a BooleanQuery so that it is required, along
240    * with a second optional clause which will match everything
241    */
242   public Query reqB(Query q) throws Exception {
243     BooleanQuery bq = new BooleanQuery(true);
244     bq.add(q, BooleanClause.Occur.MUST);
245     bq.add(new TermQuery(new Term(FIELD,"w1")), BooleanClause.Occur.SHOULD);
246     return bq;
247   }
248   
249   /**
250    * Placeholder: JUnit freaks if you don't have one test ... making
251    * class abstract doesn't help
252    */
253   public void testNoop() {
254     /* NOOP */
255   }
256 }