rangequery.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 495 行 · 第 1/2 页
JAVA
495 行
/* * 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, * 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. */package org.apache.jackrabbit.core.query.lucene;import org.apache.lucene.search.Query;import org.apache.lucene.search.Weight;import org.apache.lucene.search.Searcher;import org.apache.lucene.search.Scorer;import org.apache.lucene.search.Explanation;import org.apache.lucene.search.Similarity;import org.apache.lucene.search.BooleanQuery;import org.apache.lucene.index.Term;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.TermEnum;import org.apache.lucene.index.TermDocs;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.IOException;import java.util.BitSet;import java.util.Map;import java.util.List;import java.util.ArrayList;import java.util.Iterator;import java.util.HashMap;import java.util.Set;/** * Implements a variant of the lucene class {@link org.apache.lucene.search.RangeQuery}. * This class does not rewrite to basic {@link org.apache.lucene.search.TermQuery} * but will calculate the matching documents itself. That way a * <code>TooManyClauses</code> can be avoided. */public class RangeQuery extends Query implements TransformConstants { /** * Logger instance for this class. */ private static final Logger log = LoggerFactory.getLogger(RangeQuery.class); /** * The lower term. May be <code>null</code> if <code>upperTerm</code> is not * <code>null</code>. */ private Term lowerTerm; /** * The upper term. May be <code>null</code> if <code>lowerTerm</code> is not * <code>null</code>. */ private Term upperTerm; /** * If <code>true</code> the range interval is inclusive. */ private boolean inclusive; /** * How the term enum is transformed before it is compared to lower and upper * term. */ private final int transform; /** * The rewritten range query or <code>null</code> if the range spans more * than {@link org.apache.lucene.search.BooleanQuery#maxClauseCount} terms. */ private Query stdRangeQuery; /** * Creates a new RangeQuery. The lower or the upper term may be * <code>null</code>, but not both! * * @param lowerTerm the lower term of the interval, or <code>null</code> * @param upperTerm the upper term of the interval, or <code>null</code>. * @param inclusive if <code>true</code> the interval is inclusive. * @param transform how term enums are transformed when read from the index. */ public RangeQuery(Term lowerTerm, Term upperTerm, boolean inclusive, int transform) { if (lowerTerm == null && upperTerm == null) { throw new IllegalArgumentException("At least one term must be non-null"); } if (lowerTerm != null && upperTerm != null && lowerTerm.field() != upperTerm.field()) { throw new IllegalArgumentException("Both terms must be for the same field"); } // if we have a lowerTerm, start there. otherwise, start at beginning if (lowerTerm != null) { this.lowerTerm = lowerTerm; } else { this.lowerTerm = new Term(upperTerm.field(), ""); } this.upperTerm = upperTerm; this.inclusive = inclusive; this.transform = transform; } /** * Tries to rewrite this query into a standard lucene RangeQuery. * This rewrite might fail with a TooManyClauses exception. If that * happens, we use our own implementation. * * @param reader the index reader. * @return the rewritten query or this query if rewriting is not possible. * @throws IOException if an error occurs. */ public Query rewrite(IndexReader reader) throws IOException { if (transform == TRANSFORM_NONE) { Query stdRangeQueryImpl = new org.apache.lucene.search.RangeQuery(lowerTerm, upperTerm, inclusive); try { stdRangeQuery = stdRangeQueryImpl.rewrite(reader); return stdRangeQuery; } catch (BooleanQuery.TooManyClauses e) { log.debug("Too many terms to enumerate, using custom RangeQuery"); // failed, use own implementation return this; } } else { // always use our implementation when we need to transform the // term enum return this; } } /** * Creates the <code>Weight</code> for this query. * * @param searcher the searcher to use for the <code>Weight</code>. * @return the <code>Weigth</code> for this query. */ protected Weight createWeight(Searcher searcher) { return new RangeQueryWeight(searcher); } /** * Returns a string representation of this query. * @param field the field name for which to create a string representation. * @return a string representation of this query. */ public String toString(String field) { StringBuffer buffer = new StringBuffer(); if (!getField().equals(field)) { buffer.append(getField()); buffer.append(":"); } buffer.append(inclusive ? "[" : "{"); buffer.append(lowerTerm != null ? lowerTerm.text() : "null"); buffer.append(" TO "); buffer.append(upperTerm != null ? upperTerm.text() : "null"); buffer.append(inclusive ? "]" : "}"); if (getBoost() != 1.0f) { buffer.append("^"); buffer.append(Float.toString(getBoost())); } return buffer.toString(); } /** * {@inheritDoc} */ public void extractTerms(Set terms) { if (stdRangeQuery != null) { stdRangeQuery.extractTerms(terms); } } /** * Returns the field name for this query. */ private String getField() { return (lowerTerm != null ? lowerTerm.field() : upperTerm.field()); } //--------------------------< RangeQueryWeight >---------------------------- /** * The <code>Weight</code> implementation for this <code>RangeQuery</code>. */ private class RangeQueryWeight extends AbstractWeight { /** * Creates a new <code>RangeQueryWeight</code> instance using * <code>searcher</code>. * * @param searcher a <code>Searcher</code> instance. */ RangeQueryWeight(Searcher searcher) { super(searcher); } /** * Creates a {@link RangeQueryScorer} instance. * * @param reader index reader * @return a {@link RangeQueryScorer} instance */ protected Scorer createScorer(IndexReader reader) { return new RangeQueryScorer(searcher.getSimilarity(), reader); }; /** * Returns this <code>RangeQuery</code>. * * @return this <code>RangeQuery</code>. */ public Query getQuery() { return RangeQuery.this; } /** * {@inheritDoc} */ public float getValue() { return 1.0f; } /** * {@inheritDoc} */ public float sumOfSquaredWeights() throws IOException { return 1.0f; } /** * {@inheritDoc} */ public void normalize(float norm) { } /** * {@inheritDoc} */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?