mirror of https://github.com/apache/lucene.git
parent
dfaf023d4a
commit
53a7f5b748
|
@ -70,7 +70,9 @@ public abstract class Query {
|
|||
* a PrefixQuery will be rewritten into a BooleanQuery that consists
|
||||
* of TermQuerys.
|
||||
*/
|
||||
public abstract Query rewrite(IndexReader reader, RewriteContext rewriteContext) throws IOException;
|
||||
public Query rewrite(IndexReader reader) throws IOException {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override and implement query instance equivalence properly in a subclass.
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
/*
|
||||
* 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.lucene.search;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.TermContext;
|
||||
|
||||
public class RewriteContext {
|
||||
|
||||
public TermContext buildTermContext(Term term, IndexReader reader) throws IOException {
|
||||
return TermContext.build(reader.getContext(), term);
|
||||
}
|
||||
|
||||
}
|
|
@ -22,7 +22,6 @@ import java.util.Objects;
|
|||
import java.util.Set;
|
||||
|
||||
import org.apache.lucene.index.IndexOptions;
|
||||
import org.apache.lucene.index.IndexReader;
|
||||
import org.apache.lucene.index.IndexReaderContext;
|
||||
import org.apache.lucene.index.LeafReader;
|
||||
import org.apache.lucene.index.LeafReaderContext;
|
||||
|
@ -204,17 +203,25 @@ public class TermQuery extends Query {
|
|||
|
||||
@Override
|
||||
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
|
||||
if (perReaderTermState == null)
|
||||
throw new IllegalStateException("TermQuery must be rewritten before use");
|
||||
if (perReaderTermState.wasBuiltFor(searcher.getTopReaderContext()) == false)
|
||||
throw new IllegalStateException("TermQuery was built against a different IndexReader");
|
||||
return new TermWeight(searcher, scoreMode.needsScores(), boost, perReaderTermState);
|
||||
}
|
||||
final IndexReaderContext context = searcher.getTopReaderContext();
|
||||
final TermContext termState;
|
||||
if (perReaderTermState == null
|
||||
|| perReaderTermState.wasBuiltFor(context) == false) {
|
||||
if (scoreMode.needsScores()) {
|
||||
// make TermQuery single-pass if we don't have a PRTS or if the context
|
||||
// differs!
|
||||
termState = TermContext.build(context, term);
|
||||
} else {
|
||||
// do not compute the term state, this will help save seeks in the terms
|
||||
// dict on segments that have a cache entry for this query
|
||||
termState = null;
|
||||
}
|
||||
} else {
|
||||
// PRTS was pre-build for this IS
|
||||
termState = this.perReaderTermState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query rewrite(IndexReader reader, RewriteContext rewriteContext) throws IOException {
|
||||
TermContext tc = rewriteContext.buildTermContext(term, reader);
|
||||
return new TermQuery(term, tc);
|
||||
return new TermWeight(searcher, scoreMode.needsScores(), boost, termState);
|
||||
}
|
||||
|
||||
/** Prints a user-readable version of this query. */
|
||||
|
|
Loading…
Reference in New Issue