2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.lucene.search;
19
import java.io.IOException;
21
import org.apache.lucene.analysis.Analyzer;
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.IndexWriter;
26
import org.apache.lucene.index.Term;
27
import org.apache.lucene.index.IndexReader;
28
import org.apache.lucene.queryParser.QueryParser;
29
import org.apache.lucene.store.Directory;
31
import org.apache.lucene.util.LuceneTestCase;
34
* Tests MatchAllDocsQuery.
37
public class TestMatchAllDocsQuery extends LuceneTestCase {
38
private Analyzer analyzer = new MockAnalyzer(random);
40
public void testQuery() throws Exception {
41
Directory dir = newDirectory();
42
IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(
43
TEST_VERSION_CURRENT, analyzer).setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy()));
44
addDoc("one", iw, 1f);
45
addDoc("two", iw, 20f);
46
addDoc("three four", iw, 300f);
49
IndexReader ir = IndexReader.open(dir, false);
50
IndexSearcher is = newSearcher(ir);
53
// assert with norms scoring turned off
55
hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs;
56
assertEquals(3, hits.length);
57
assertEquals("one", is.doc(hits[0].doc).get("key"));
58
assertEquals("two", is.doc(hits[1].doc).get("key"));
59
assertEquals("three four", is.doc(hits[2].doc).get("key"));
61
// assert with norms scoring turned on
63
MatchAllDocsQuery normsQuery = new MatchAllDocsQuery("key");
64
hits = is.search(normsQuery, null, 1000).scoreDocs;
65
assertEquals(3, hits.length);
67
assertEquals("three four", is.doc(hits[0].doc).get("key"));
68
assertEquals("two", is.doc(hits[1].doc).get("key"));
69
assertEquals("one", is.doc(hits[2].doc).get("key"));
71
// change norm & retest
72
is.getIndexReader().setNorm(0, "key", is.getSimilarity().encodeNormValue(400f));
73
normsQuery = new MatchAllDocsQuery("key");
74
hits = is.search(normsQuery, null, 1000).scoreDocs;
75
assertEquals(3, hits.length);
77
assertEquals("one", is.doc(hits[0].doc).get("key"));
78
assertEquals("three four", is.doc(hits[1].doc).get("key"));
79
assertEquals("two", is.doc(hits[2].doc).get("key"));
81
// some artificial queries to trigger the use of skipTo():
83
BooleanQuery bq = new BooleanQuery();
84
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
85
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
86
hits = is.search(bq, null, 1000).scoreDocs;
87
assertEquals(3, hits.length);
89
bq = new BooleanQuery();
90
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
91
bq.add(new TermQuery(new Term("key", "three")), BooleanClause.Occur.MUST);
92
hits = is.search(bq, null, 1000).scoreDocs;
93
assertEquals(1, hits.length);
96
is.getIndexReader().deleteDocument(0);
97
hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs;
98
assertEquals(2, hits.length);
100
// test parsable toString()
101
QueryParser qp = new QueryParser(TEST_VERSION_CURRENT, "key", analyzer);
102
hits = is.search(qp.parse(new MatchAllDocsQuery().toString()), null, 1000).scoreDocs;
103
assertEquals(2, hits.length);
105
// test parsable toString() with non default boost
106
Query maq = new MatchAllDocsQuery();
108
Query pq = qp.parse(maq.toString());
109
hits = is.search(pq, null, 1000).scoreDocs;
110
assertEquals(2, hits.length);
117
public void testEquals() {
118
Query q1 = new MatchAllDocsQuery();
119
Query q2 = new MatchAllDocsQuery();
120
assertTrue(q1.equals(q2));
122
assertFalse(q1.equals(q2));
125
private void addDoc(String text, IndexWriter iw, float boost) throws IOException {
126
Document doc = new Document();
127
Field f = newField("key", text, Field.Store.YES, Field.Index.ANALYZED);