1
package org.apache.lucene.search;
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
11
* http://www.apache.org/licenses/LICENSE-2.0
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.
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;
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.
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.
47
* @see "Subclasses for actual tests"
49
public class TestExplanations extends LuceneTestCase {
50
protected IndexSearcher searcher;
51
protected IndexReader reader;
52
protected Directory directory;
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));
63
public void tearDown() throws Exception {
71
public void setUp() throws Exception {
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);
81
doc.add(newField(ALTFIELD, docFields[i], Field.Store.NO, Field.Index.ANALYZED));
82
writer.addDocument(doc);
84
reader = writer.getReader();
86
searcher = newSearcher(reader);
89
protected String[] docFields = {
93
"w1 w3 xx w2 yy w3 zz"
96
public Query makeQuery(String queryText) throws ParseException {
97
return qp.parse(queryText);
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);
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);
111
* Tests a query using qtest after wrapping it with both optB and reqB
116
public void bqtest(Query q, int[] expDocNrs) throws Exception {
117
qtest(reqB(q), expDocNrs);
118
qtest(optB(q), expDocNrs);
121
* Tests a query using qtest after wrapping it with both optB and reqB
126
public void bqtest(String queryText, int[] expDocNrs) throws Exception {
127
bqtest(makeQuery(queryText), expDocNrs);
131
* Convenience subclass of FieldCacheTermsFilter
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];
141
public ItemizedFilter(String keyField, int [] keys) {
142
super(keyField, int2str(keys));
144
public ItemizedFilter(int [] keys) {
145
super(KEY, int2str(keys));
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]);
158
/** MACRO for SpanTermQuery */
159
public SpanTermQuery st(String s) {
160
return new SpanTermQuery(new Term(FIELD,s));
163
/** MACRO for SpanNotQuery */
164
public SpanNotQuery snot(SpanQuery i, SpanQuery e) {
165
return new SpanNotQuery(i,e);
168
/** MACRO for SpanOrQuery containing two SpanTerm queries */
169
public SpanOrQuery sor(String s, String e) {
170
return sor(st(s), st(e));
172
/** MACRO for SpanOrQuery containing two SpanQueries */
173
public SpanOrQuery sor(SpanQuery s, SpanQuery e) {
174
return new SpanOrQuery(new SpanQuery[] { s, e });
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));
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 });
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);
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);
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);
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);
208
/** MACRO for SpanFirst(SpanTermQuery) */
209
public SpanFirstQuery sf(String s, int b) {
210
return new SpanFirstQuery(st(s), b);
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
217
public Query optB(String q) throws Exception {
218
return optB(makeQuery(q));
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
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);
232
* MACRO: Wraps a Query in a BooleanQuery so that it is required, along
233
* with a second optional clause which will match everything
235
public Query reqB(String q) throws Exception {
236
return reqB(makeQuery(q));
239
* MACRO: Wraps a Query in a BooleanQuery so that it is required, along
240
* with a second optional clause which will match everything
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);
250
* Placeholder: JUnit freaks if you don't have one test ... making
251
* class abstract doesn't help
253
public void testNoop() {