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.document.Document;
21 import org.apache.lucene.document.Field.Index;
22 import org.apache.lucene.document.Field.Store;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.RandomIndexWriter;
25 import org.apache.lucene.index.Term;
26 import org.apache.lucene.search.BooleanClause.Occur;
27 import org.apache.lucene.store.Directory;
28 import org.apache.lucene.util.LuceneTestCase;
30 public class TestQueryWrapperFilter extends LuceneTestCase {
32 public void testBasic() throws Exception {
33 Directory dir = newDirectory();
34 RandomIndexWriter writer = new RandomIndexWriter(random, dir);
35 Document doc = new Document();
36 doc.add(newField("field", "value", Store.NO, Index.ANALYZED));
37 writer.addDocument(doc);
38 IndexReader reader = writer.getReader();
41 TermQuery termQuery = new TermQuery(new Term("field", "value"));
43 // should not throw exception with primitive query
44 QueryWrapperFilter qwf = new QueryWrapperFilter(termQuery);
46 IndexSearcher searcher = newSearcher(reader);
47 TopDocs hits = searcher.search(new MatchAllDocsQuery(), qwf, 10);
48 assertEquals(1, hits.totalHits);
49 hits = searcher.search(new MatchAllDocsQuery(), new CachingWrapperFilter(qwf), 10);
50 assertEquals(1, hits.totalHits);
52 // should not throw exception with complex primitive query
53 BooleanQuery booleanQuery = new BooleanQuery();
54 booleanQuery.add(termQuery, Occur.MUST);
55 booleanQuery.add(new TermQuery(new Term("field", "missing")),
57 qwf = new QueryWrapperFilter(termQuery);
59 hits = searcher.search(new MatchAllDocsQuery(), qwf, 10);
60 assertEquals(1, hits.totalHits);
61 hits = searcher.search(new MatchAllDocsQuery(), new CachingWrapperFilter(qwf), 10);
62 assertEquals(1, hits.totalHits);
64 // should not throw exception with non primitive Query (doesn't implement
65 // Query#createWeight)
66 qwf = new QueryWrapperFilter(new FuzzyQuery(new Term("field", "valu")));
68 hits = searcher.search(new MatchAllDocsQuery(), qwf, 10);
69 assertEquals(1, hits.totalHits);
70 hits = searcher.search(new MatchAllDocsQuery(), new CachingWrapperFilter(qwf), 10);
71 assertEquals(1, hits.totalHits);
73 // test a query with no hits
74 termQuery = new TermQuery(new Term("field", "not_exist"));
75 qwf = new QueryWrapperFilter(termQuery);
76 hits = searcher.search(new MatchAllDocsQuery(), qwf, 10);
77 assertEquals(0, hits.totalHits);
78 hits = searcher.search(new MatchAllDocsQuery(), new CachingWrapperFilter(qwf), 10);
79 assertEquals(0, hits.totalHits);