⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inaccurateindexsearcher.java

📁 Lucene Hack之通过缩小搜索结果集来提升性能
💻 JAVA
字号:
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,
 * 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.BitSet;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.util.InaccurateResultAggregation;

/**
 * Implements search over a single IndexReader.
 * 
 * <p>
 * Rewritten by caocao (http://www.caocao.name)
 * 
 * <p>
 * Applications usually need only call the inherited {@link #search(Query)} or
 * {@link #search(Query,Filter)} methods. For performance reasons it is
 * recommended to open only one IndexSearcher and use it for all of your
 * searches.
 * 
 * <p>
 * Note that you can only access InaccurateHits from an InaccurateIndexSearcher
 * as long as it is not yet closed, otherwise an IOException will be thrown.
 */
public class InaccurateIndexSearcher extends IndexSearcher {
	protected int maxNumberOfDocs;

	protected InaccurateResultAggregation inaccurateResultAggregation;

	public InaccurateIndexSearcher(IndexReader reader, int maxNumberOfDocs) {
		super(reader);
		this.maxNumberOfDocs = maxNumberOfDocs;
	}

	public int getMaxNumberOfDocs() {
		return maxNumberOfDocs;
	}

	public void setMaxNumberOfDocs(int maxNumberOfDocs) {
		this.maxNumberOfDocs = maxNumberOfDocs;
	}

	public InaccurateHits search(Query query, Sort sort, boolean ascending)
			throws IOException {
		return new InaccurateHits(this, query, null, sort, ascending);
	}

	public void search(Weight weight, Filter filter,
			final HitCollector results, boolean ascending) throws IOException {
		HitCollector collector = results;
		if (filter != null) {
			final BitSet bits = filter.bits(reader);
			collector = new HitCollector() {
				public final void collect(int doc, float score) {
					if (bits.get(doc)) { // skip docs not in bits
						results.collect(doc, score);
					}
				}
			};
		}
		if (weight.getClass().getSimpleName().equals("BooleanWeight2")) {
			InaccurateBooleanWeight2 inaccurateBooleanWeight2 = new InaccurateBooleanWeight2(
					this, weight.getQuery());
			float sum = inaccurateBooleanWeight2.sumOfSquaredWeights();
			float norm = this.getSimilarity().queryNorm(sum);
			inaccurateBooleanWeight2.normalize(norm);
			InaccurateBooleanScorer2 inaccurateBooleanScorer2 = inaccurateBooleanWeight2
					.getInaccurateBooleanScorer2(reader, maxNumberOfDocs);
			if (inaccurateBooleanScorer2 != null) {
				inaccurateResultAggregation = inaccurateBooleanScorer2
						.getInaccurateTopAggregation(collector, ascending);
			}
		} else {
			Scorer scorer = weight.scorer(reader);
			if (scorer != null) {
				scorer.score(collector);
			}
		}
	}

	public TopFieldDocs search(Weight weight, Filter filter, int nDocs,
			Sort sort, boolean ascending) throws IOException {
		TopFieldDocCollector collector = new TopFieldDocCollector(reader, sort,
				nDocs);
		search(weight, filter, collector, ascending);
		return (TopFieldDocs) collector.topDocs();
	}

	public TopDocs search(Weight weight, Filter filter, int nDocs,
			boolean ascending) throws IOException {
		if (nDocs <= 0) // null might be returned from hq.top() below.
			throw new IllegalArgumentException("nDocs must be > 0");

		TopDocCollector collector = new TopDocCollector(nDocs);
		search(weight, filter, collector, ascending);
		return collector.topDocs();
	}

	public InaccurateResultAggregation getInaccurateResultAggregation() {
		return inaccurateResultAggregation;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -