pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / search / TestMatchAllDocsQuery.java
1 /**
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 package org.apache.lucene.search;
18
19 import java.io.IOException;
20
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;
30
31 import org.apache.lucene.util.LuceneTestCase;
32
33 /**
34  * Tests MatchAllDocsQuery.
35  *
36  */
37 public class TestMatchAllDocsQuery extends LuceneTestCase {
38   private Analyzer analyzer = new MockAnalyzer(random);
39   
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);
47     iw.close();
48
49     IndexReader ir = IndexReader.open(dir, false);
50     IndexSearcher is = newSearcher(ir);
51     ScoreDoc[] hits;
52
53     // assert with norms scoring turned off
54
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"));
60
61     // assert with norms scoring turned on
62
63     MatchAllDocsQuery normsQuery = new MatchAllDocsQuery("key");
64     hits = is.search(normsQuery, null, 1000).scoreDocs;
65     assertEquals(3, hits.length);
66
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"));
70
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);
76
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"));
80     
81     // some artificial queries to trigger the use of skipTo():
82     
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);
88
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);
94
95     // delete a document:
96     is.getIndexReader().deleteDocument(0);
97     hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs;
98     assertEquals(2, hits.length);
99     
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);
104
105     // test parsable toString() with non default boost
106     Query maq = new MatchAllDocsQuery();
107     maq.setBoost(2.3f);
108     Query pq = qp.parse(maq.toString());
109     hits = is.search(pq, null, 1000).scoreDocs;
110     assertEquals(2, hits.length);
111     
112     is.close();
113     ir.close();
114     dir.close();
115   }
116
117   public void testEquals() {
118     Query q1 = new MatchAllDocsQuery();
119     Query q2 = new MatchAllDocsQuery();
120     assertTrue(q1.equals(q2));
121     q1.setBoost(1.5f);
122     assertFalse(q1.equals(q2));
123   }
124   
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);
128     f.setBoost(boost);
129     doc.add(f);
130     iw.addDocument(doc);
131   }
132
133 }