alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file 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.

Other links

The source code

package org.apache.lucene.search;

/**
 * Copyright 2002-2004 The Apache Software Foundation
 *
 * Licensed 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,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import java.util.HashSet;
import java.util.Iterator;

import org.apache.lucene.index.IndexReader;

/** The abstract base class for queries.
    

Instantiable subclasses are:

  • {@link TermQuery}
  • {@link MultiTermQuery}
  • {@link BooleanQuery}
  • {@link WildcardQuery}
  • {@link PhraseQuery}
  • {@link PrefixQuery}
  • {@link PhrasePrefixQuery}
  • {@link FuzzyQuery}
  • {@link RangeQuery}
  • {@link org.apache.lucene.search.spans.SpanQuery}

A parser for queries is contained in:

  • {@link org.apache.lucene.queryParser.QueryParser QueryParser}
*/ public abstract class Query implements java.io.Serializable, Cloneable { private float boost = 1.0f; // query boost factor /** Sets the boost for this query clause to b. Documents * matching this clause will (in addition to the normal weightings) have * their score multiplied by b. */ public void setBoost(float b) { boost = b; } /** Gets the boost for this clause. Documents matching * this clause will (in addition to the normal weightings) have their score * multiplied by b. The boost is 1.0 by default. */ public float getBoost() { return boost; } /** Prints a query to a string, with field as the default field * for terms.

The representation used is one that is readable by * {@link org.apache.lucene.queryParser.QueryParser QueryParser} * (although, if the query was created by the parser, the printed * representation may not be exactly what was parsed). */ public abstract String toString(String field); /** Prints a query to a string. */ public String toString() { return toString(""); } /** Expert: Constructs an appropriate Weight implementation for this query. * *

Only implemented by primitive queries, which re-write to themselves. */ protected Weight createWeight(Searcher searcher) { throw new UnsupportedOperationException(); } /** Expert: Constructs an initializes a Weight for a top-level query. */ public Weight weight(Searcher searcher) throws IOException { Query query = searcher.rewrite(this); Weight weight = query.createWeight(searcher); float sum = weight.sumOfSquaredWeights(); float norm = getSimilarity(searcher).queryNorm(sum); weight.normalize(norm); return weight; } /** Expert: called to re-write queries into primitive queries. */ public Query rewrite(IndexReader reader) throws IOException { return this; } /** Expert: called when re-writing queries under MultiSearcher. * *

Only implemented by derived queries, with no * {@link #createWeight(Searcher)} implementatation. */ public Query combine(Query[] queries) { throw new UnsupportedOperationException(); } /** Expert: merges the clauses of a set of BooleanQuery's into a single * BooleanQuery. * *

A utility for use by {@link #combine(Query[])} implementations. */ public static Query mergeBooleanQueries(Query[] queries) { HashSet allClauses = new HashSet(); for (int i = 0; i < queries.length; i++) { BooleanClause[] clauses = ((BooleanQuery)queries[i]).getClauses(); for (int j = 0; j < clauses.length; j++) { allClauses.add(clauses[j]); } } BooleanQuery result = new BooleanQuery(); Iterator i = allClauses.iterator(); while (i.hasNext()) { result.add((BooleanClause)i.next()); } return result; } /** Expert: Returns the Similarity implementation to be used for this query. * Subclasses may override this method to specify their own Similarity * implementation, perhaps one that delegates through that of the Searcher. * By default the Searcher's Similarity implementation is returned.*/ public Similarity getSimilarity(Searcher searcher) { return searcher.getSimilarity(); } /** Returns a clone of this query. */ public Object clone() { try { return (Query)super.clone(); } catch (CloneNotSupportedException e) { throw new RuntimeException("Clone not supported: " + e.getMessage()); } } }

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.