home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (TestConstantScoreQuery.java)

This example Lucene source code file (TestConstantScoreQuery.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

booleanquery, constantscorequery, constantscorequery, document, io, ioexception, override, override, query, query, scorer, string, term, termquery, termquery

The Lucene TestConstantScoreQuery.java source code

package org.apache.lucene.search;

 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;

import java.io.IOException;

/** This class only tests some basic functionality in CSQ, the main parts are mostly
 * tested by MultiTermQuery tests, explanations seems to be tested in TestExplanations! */
public class TestConstantScoreQuery extends LuceneTestCase {
  public void testCSQ() throws Exception {
    final Query q1 = new ConstantScoreQuery(new TermQuery(new Term("a", "b")));
    final Query q2 = new ConstantScoreQuery(new TermQuery(new Term("a", "c")));
    final Query q3 = new ConstantScoreQuery(new TermRangeFilter("a", "b", "c", true, true));
    QueryUtils.checkUnequal(q1, new TermQuery(new Term("a", "b")));
  private void checkHits(Searcher searcher, Query q, final float expectedScore, final String scorerClassName, final String innerScorerClassName) throws IOException {
    final int[] count = new int[1];
    searcher.search(q, new Collector() {
      private Scorer scorer;
      public void setScorer(Scorer scorer) {
        this.scorer = scorer;
        assertEquals("Scorer is implemented by wrong class", scorerClassName, scorer.getClass().getName());
        if (innerScorerClassName != null && scorer instanceof ConstantScoreQuery.ConstantScorer) {
          final ConstantScoreQuery.ConstantScorer innerScorer = (ConstantScoreQuery.ConstantScorer) scorer;
          assertEquals("inner Scorer is implemented by wrong class", innerScorerClassName, innerScorer.docIdSetIterator.getClass().getName());
      public void collect(int doc) throws IOException {
        assertEquals("Score differs from expected", expectedScore, this.scorer.score());
      public void setNextReader(IndexReader reader, int docBase) {
      public boolean acceptsDocsOutOfOrder() {
        return true;
    assertEquals("invalid number of results", 1, count[0]);
  public void testWrapped2Times() throws Exception {
    Directory directory = null;
    IndexReader reader = null;
    IndexSearcher searcher = null;
    try {
      directory = newDirectory();
      RandomIndexWriter writer = new RandomIndexWriter (random, directory);

      Document doc = new Document();
      doc.add(newField("field", "term", Field.Store.NO, Field.Index.NOT_ANALYZED));

      reader = writer.getReader();
      searcher = newSearcher(reader);
      // set a similarity that does not normalize our boost away
      searcher.setSimilarity(new DefaultSimilarity() {
        public float queryNorm(float sumOfSquaredWeights) {
          return 1.0f;
      final Query csq1 = new ConstantScoreQuery(new TermQuery(new Term ("field", "term")));
      final Query csq2 = new ConstantScoreQuery(csq1);
      final BooleanQuery bq = new BooleanQuery();
      bq.add(csq1, BooleanClause.Occur.SHOULD);
      bq.add(csq2, BooleanClause.Occur.SHOULD);
      final Query csqbq = new ConstantScoreQuery(bq);
      checkHits(searcher, csq1, csq1.getBoost(), ConstantScoreQuery.ConstantScorer.class.getName(), null);
      checkHits(searcher, csq2, csq2.getBoost(), ConstantScoreQuery.ConstantScorer.class.getName(), ConstantScoreQuery.ConstantScorer.class.getName());
      // for the combined BQ, the scorer should always be BooleanScorer's BucketScorer, because our scorer supports out-of order collection!
      final String bucketScorerClass = BooleanScorer.class.getName() + "$BucketScorer";
      checkHits(searcher, bq, csq1.getBoost() + csq2.getBoost(), bucketScorerClass, null);
      checkHits(searcher, csqbq, csqbq.getBoost(), ConstantScoreQuery.ConstantScorer.class.getName(), bucketScorerClass);
    } finally {
      if (searcher != null) searcher.close();
      if (reader != null) reader.close();
      if (directory != null) directory.close();

Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene TestConstantScoreQuery.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.