pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / TestConstantScoreQuery.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.document.Document;
21 import org.apache.lucene.document.Field;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.RandomIndexWriter;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.LuceneTestCase;
27
28 import java.io.IOException;
29
30 /** This class only tests some basic functionality in CSQ, the main parts are mostly
31  * tested by MultiTermQuery tests, explanations seems to be tested in TestExplanations! */
32 public class TestConstantScoreQuery extends LuceneTestCase {
33   
34   public void testCSQ() throws Exception {
35     final Query q1 = new ConstantScoreQuery(new TermQuery(new Term("a", "b")));
36     final Query q2 = new ConstantScoreQuery(new TermQuery(new Term("a", "c")));
37     final Query q3 = new ConstantScoreQuery(new TermRangeFilter("a", "b", "c", true, true));
38     QueryUtils.check(q1);
39     QueryUtils.check(q2);
40     QueryUtils.checkEqual(q1,q1);
41     QueryUtils.checkEqual(q2,q2);
42     QueryUtils.checkEqual(q3,q3);
43     QueryUtils.checkUnequal(q1,q2);
44     QueryUtils.checkUnequal(q2,q3);
45     QueryUtils.checkUnequal(q1,q3);
46     QueryUtils.checkUnequal(q1, new TermQuery(new Term("a", "b")));
47   }
48   
49   private void checkHits(Searcher searcher, Query q, final float expectedScore, final String scorerClassName, final String innerScorerClassName) throws IOException {
50     final int[] count = new int[1];
51     searcher.search(q, new Collector() {
52       private Scorer scorer;
53     
54       @Override
55       public void setScorer(Scorer scorer) {
56         this.scorer = scorer;
57         assertEquals("Scorer is implemented by wrong class", scorerClassName, scorer.getClass().getName());
58         if (innerScorerClassName != null && scorer instanceof ConstantScoreQuery.ConstantScorer) {
59           final ConstantScoreQuery.ConstantScorer innerScorer = (ConstantScoreQuery.ConstantScorer) scorer;
60           assertEquals("inner Scorer is implemented by wrong class", innerScorerClassName, innerScorer.docIdSetIterator.getClass().getName());
61         }
62       }
63       
64       @Override
65       public void collect(int doc) throws IOException {
66         assertEquals("Score differs from expected", expectedScore, this.scorer.score(), 0);
67         count[0]++;
68       }
69       
70       @Override
71       public void setNextReader(IndexReader reader, int docBase) {
72       }
73       
74       @Override
75       public boolean acceptsDocsOutOfOrder() {
76         return true;
77       }
78     });
79     assertEquals("invalid number of results", 1, count[0]);
80   }
81   
82   public void testWrapped2Times() throws Exception {
83     Directory directory = null;
84     IndexReader reader = null;
85     IndexSearcher searcher = null;
86     try {
87       directory = newDirectory();
88       RandomIndexWriter writer = new RandomIndexWriter (random, directory);
89
90       Document doc = new Document();
91       doc.add(newField("field", "term", Field.Store.NO, Field.Index.NOT_ANALYZED));
92       writer.addDocument(doc);
93
94       reader = writer.getReader();
95       writer.close();
96       searcher = newSearcher(reader);
97       
98       // set a similarity that does not normalize our boost away
99       searcher.setSimilarity(new DefaultSimilarity() {
100         @Override
101         public float queryNorm(float sumOfSquaredWeights) {
102           return 1.0f;
103         }
104       });
105       
106       final Query csq1 = new ConstantScoreQuery(new TermQuery(new Term ("field", "term")));
107       csq1.setBoost(2.0f);
108       final Query csq2 = new ConstantScoreQuery(csq1);
109       csq2.setBoost(5.0f);
110       
111       final BooleanQuery bq = new BooleanQuery();
112       bq.add(csq1, BooleanClause.Occur.SHOULD);
113       bq.add(csq2, BooleanClause.Occur.SHOULD);
114       
115       final Query csqbq = new ConstantScoreQuery(bq);
116       csqbq.setBoost(17.0f);
117       
118       checkHits(searcher, csq1, csq1.getBoost(), ConstantScoreQuery.ConstantScorer.class.getName(), null);
119       checkHits(searcher, csq2, csq2.getBoost(), ConstantScoreQuery.ConstantScorer.class.getName(), ConstantScoreQuery.ConstantScorer.class.getName());
120       
121       // for the combined BQ, the scorer should always be BooleanScorer's BucketScorer, because our scorer supports out-of order collection!
122       final String bucketScorerClass = BooleanScorer.class.getName() + "$BucketScorer";
123       checkHits(searcher, bq, csq1.getBoost() + csq2.getBoost(), bucketScorerClass, null);
124       checkHits(searcher, csqbq, csqbq.getBoost(), ConstantScoreQuery.ConstantScorer.class.getName(), bucketScorerClass);
125     } finally {
126       if (searcher != null) searcher.close();
127       if (reader != null) reader.close();
128       if (directory != null) directory.close();
129     }
130   }
131   
132 }