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;
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;
28 import java.io.IOException;
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 {
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));
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")));
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;
55 public void setScorer(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());
65 public void collect(int doc) throws IOException {
66 assertEquals("Score differs from expected", expectedScore, this.scorer.score());
71 public void setNextReader(IndexReader reader, int docBase) {
75 public boolean acceptsDocsOutOfOrder() {
79 assertEquals("invalid number of results", 1, count[0]);
82 public void testWrapped2Times() throws Exception {
83 Directory directory = null;
84 IndexReader reader = null;
85 IndexSearcher searcher = null;
87 directory = newDirectory();
88 RandomIndexWriter writer = new RandomIndexWriter (random, directory);
90 Document doc = new Document();
91 doc.add(newField("field", "term", Field.Store.NO, Field.Index.NOT_ANALYZED));
92 writer.addDocument(doc);
94 reader = writer.getReader();
96 searcher = newSearcher(reader);
98 // set a similarity that does not normalize our boost away
99 searcher.setSimilarity(new DefaultSimilarity() {
101 public float queryNorm(float sumOfSquaredWeights) {
106 final Query csq1 = new ConstantScoreQuery(new TermQuery(new Term ("field", "term")));
108 final Query csq2 = new ConstantScoreQuery(csq1);
111 final BooleanQuery bq = new BooleanQuery();
112 bq.add(csq1, BooleanClause.Occur.SHOULD);
113 bq.add(csq2, BooleanClause.Occur.SHOULD);
115 final Query csqbq = new ConstantScoreQuery(bq);
116 csqbq.setBoost(17.0f);
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());
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);
126 if (searcher != null) searcher.close();
127 if (reader != null) reader.close();
128 if (directory != null) directory.close();