|
|
@@ -17,11 +17,7 @@ package org.apache.lucene.queries;
|
|
|
* specific language governing permissions and limitations
|
|
|
* under the License.
|
|
|
*/
|
|
|
-import org.apache.lucene.index.Term;
|
|
|
-import org.apache.lucene.index.TermContext;
|
|
|
-import org.apache.lucene.search.*;
|
|
|
import org.apache.lucene.search.BooleanClause.Occur;
|
|
|
-import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.common.lucene.search.Queries;
|
|
|
|
|
|
/**
|
|
|
@@ -74,70 +70,4 @@ public class ExtendedCommonTermsQuery extends CommonTermsQuery {
|
|
|
public String getLowFreqMinimumNumberShouldMatchSpec() {
|
|
|
return lowFreqMinNumShouldMatchSpec;
|
|
|
}
|
|
|
-
|
|
|
- static {
|
|
|
- assert Version.CURRENT.luceneVersion.compareTo(org.apache.lucene.util.Version.LUCENE_45) == 0 : "Remove buildQuery() once 4.6 is out";
|
|
|
- }
|
|
|
- @Override
|
|
|
- protected Query buildQuery(final int maxDoc,
|
|
|
- final TermContext[] contextArray, final Term[] queryTerms) {
|
|
|
- BooleanQuery lowFreq = new BooleanQuery(disableCoord);
|
|
|
- BooleanQuery highFreq = new BooleanQuery(disableCoord);
|
|
|
- highFreq.setBoost(highFreqBoost);
|
|
|
- lowFreq.setBoost(lowFreqBoost);
|
|
|
- BooleanQuery query = new BooleanQuery(true);
|
|
|
- for (int i = 0; i < queryTerms.length; i++) {
|
|
|
- TermContext termContext = contextArray[i];
|
|
|
- if (termContext == null) {
|
|
|
- lowFreq.add(new TermQuery(queryTerms[i]), lowFreqOccur);
|
|
|
- } else {
|
|
|
- if ((maxTermFrequency >= 1f && termContext.docFreq() > maxTermFrequency)
|
|
|
- || (termContext.docFreq() > (int) Math.ceil(maxTermFrequency
|
|
|
- * (float) maxDoc))) {
|
|
|
- highFreq
|
|
|
- .add(new TermQuery(queryTerms[i], termContext), highFreqOccur);
|
|
|
- } else {
|
|
|
- lowFreq.add(new TermQuery(queryTerms[i], termContext), lowFreqOccur);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- final int numLowFreqClauses = lowFreq.clauses().size();
|
|
|
- final int numHighFreqClauses = highFreq.clauses().size();
|
|
|
- if (lowFreqOccur == Occur.SHOULD && numLowFreqClauses > 0) {
|
|
|
- int minMustMatch = calcLowFreqMinimumNumberShouldMatch(numLowFreqClauses);
|
|
|
- lowFreq.setMinimumNumberShouldMatch(minMustMatch);
|
|
|
- }
|
|
|
-
|
|
|
- if (highFreqOccur == Occur.SHOULD && numHighFreqClauses > 0) {
|
|
|
- int minMustMatch = calcHighFreqMinimumNumberShouldMatch(numHighFreqClauses);
|
|
|
- highFreq.setMinimumNumberShouldMatch(minMustMatch);
|
|
|
- }
|
|
|
-
|
|
|
- if (lowFreq.clauses().isEmpty()) {
|
|
|
- /*
|
|
|
- * if lowFreq is empty we rewrite the high freq terms in a conjunction to
|
|
|
- * prevent slow queries.
|
|
|
- * Only if a specic high_freq should_match is not specified.
|
|
|
- */
|
|
|
- if (highFreq.getMinimumNumberShouldMatch() == 0 && highFreqOccur != Occur.MUST) {
|
|
|
- for (BooleanClause booleanClause : highFreq) {
|
|
|
- booleanClause.setOccur(Occur.MUST);
|
|
|
- }
|
|
|
- }
|
|
|
- highFreq.setBoost(getBoost());
|
|
|
- return highFreq;
|
|
|
- } else if (highFreq.clauses().isEmpty()) {
|
|
|
- // only do low freq terms - we don't have high freq terms
|
|
|
- lowFreq.setBoost(getBoost());
|
|
|
- return lowFreq;
|
|
|
- } else {
|
|
|
- query.add(highFreq, Occur.SHOULD);
|
|
|
- query.add(lowFreq, Occur.MUST);
|
|
|
- query.setBoost(getBoost());
|
|
|
- return query;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|