|
@@ -20,14 +20,22 @@
|
|
|
package org.elasticsearch.index.query.support;
|
|
|
|
|
|
import org.apache.lucene.search.MultiTermQuery;
|
|
|
-
|
|
|
import org.elasticsearch.common.Nullable;
|
|
|
+import org.elasticsearch.common.ParseField;
|
|
|
+import org.elasticsearch.common.ParseFieldMatcher;
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
public final class QueryParsers {
|
|
|
|
|
|
+ private static final ParseField CONSTANT_SCORE = new ParseField("constant_score", "constant_score_auto", "constant_score_filter");
|
|
|
+ private static final ParseField SCORING_BOOLEAN = new ParseField("scoring_boolean");
|
|
|
+ private static final ParseField CONSTANT_SCORE_BOOLEAN = new ParseField("constant_score_boolean");
|
|
|
+ private static final ParseField TOP_TERMS = new ParseField("top_terms_");
|
|
|
+ private static final ParseField TOP_TERMS_BOOST = new ParseField("top_terms_boost_");
|
|
|
+ private static final ParseField TOP_TERMS_BLENDED_FREQS = new ParseField("top_terms_blended_freqs_");
|
|
|
+
|
|
|
private QueryParsers() {
|
|
|
|
|
|
}
|
|
@@ -39,50 +47,55 @@ public final class QueryParsers {
|
|
|
query.setRewriteMethod(rewriteMethod);
|
|
|
}
|
|
|
|
|
|
- public static void setRewriteMethod(MultiTermQuery query, @Nullable String rewriteMethod) {
|
|
|
+ public static void setRewriteMethod(MultiTermQuery query, ParseFieldMatcher matcher, @Nullable String rewriteMethod) {
|
|
|
if (rewriteMethod == null) {
|
|
|
return;
|
|
|
}
|
|
|
- query.setRewriteMethod(parseRewriteMethod(rewriteMethod));
|
|
|
+ query.setRewriteMethod(parseRewriteMethod(matcher, rewriteMethod));
|
|
|
}
|
|
|
|
|
|
- public static MultiTermQuery.RewriteMethod parseRewriteMethod(@Nullable String rewriteMethod) {
|
|
|
- return parseRewriteMethod(rewriteMethod, MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
|
|
|
+ public static MultiTermQuery.RewriteMethod parseRewriteMethod(ParseFieldMatcher matcher, @Nullable String rewriteMethod) {
|
|
|
+ return parseRewriteMethod(matcher, rewriteMethod, MultiTermQuery.CONSTANT_SCORE_REWRITE);
|
|
|
}
|
|
|
|
|
|
- public static MultiTermQuery.RewriteMethod parseRewriteMethod(@Nullable String rewriteMethod, @Nullable MultiTermQuery.RewriteMethod defaultRewriteMethod) {
|
|
|
+ public static MultiTermQuery.RewriteMethod parseRewriteMethod(ParseFieldMatcher matcher, @Nullable String rewriteMethod, @Nullable MultiTermQuery.RewriteMethod defaultRewriteMethod) {
|
|
|
if (rewriteMethod == null) {
|
|
|
return defaultRewriteMethod;
|
|
|
}
|
|
|
- if ("constant_score_auto".equals(rewriteMethod) || "constant_score_auto".equals(rewriteMethod)) {
|
|
|
- return MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE;
|
|
|
- }
|
|
|
- if ("scoring_boolean".equals(rewriteMethod) || "scoringBoolean".equals(rewriteMethod)) {
|
|
|
- return MultiTermQuery.SCORING_BOOLEAN_QUERY_REWRITE;
|
|
|
- }
|
|
|
- if ("constant_score_boolean".equals(rewriteMethod) || "constantScoreBoolean".equals(rewriteMethod)) {
|
|
|
- return MultiTermQuery.CONSTANT_SCORE_BOOLEAN_QUERY_REWRITE;
|
|
|
- }
|
|
|
- if ("constant_score_filter".equals(rewriteMethod) || "constantScoreFilter".equals(rewriteMethod)) {
|
|
|
- return MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE;
|
|
|
+ if (matcher.match(rewriteMethod, CONSTANT_SCORE)) {
|
|
|
+ return MultiTermQuery.CONSTANT_SCORE_REWRITE;
|
|
|
}
|
|
|
- if (rewriteMethod.startsWith("top_terms_boost_")) {
|
|
|
- int size = Integer.parseInt(rewriteMethod.substring("top_terms_boost_".length()));
|
|
|
- return new MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite(size);
|
|
|
+ if (matcher.match(rewriteMethod, SCORING_BOOLEAN)) {
|
|
|
+ return MultiTermQuery.SCORING_BOOLEAN_REWRITE;
|
|
|
}
|
|
|
- if (rewriteMethod.startsWith("topTermsBoost")) {
|
|
|
- int size = Integer.parseInt(rewriteMethod.substring("topTermsBoost".length()));
|
|
|
- return new MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite(size);
|
|
|
+ if (matcher.match(rewriteMethod, CONSTANT_SCORE_BOOLEAN)) {
|
|
|
+ return MultiTermQuery.CONSTANT_SCORE_BOOLEAN_REWRITE;
|
|
|
}
|
|
|
- if (rewriteMethod.startsWith("top_terms_")) {
|
|
|
- int size = Integer.parseInt(rewriteMethod.substring("top_terms_".length()));
|
|
|
- return new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(size);
|
|
|
+
|
|
|
+ int firstDigit = -1;
|
|
|
+ for (int i = 0; i < rewriteMethod.length(); ++i) {
|
|
|
+ if (Character.isDigit(rewriteMethod.charAt(i))) {
|
|
|
+ firstDigit = i;
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
- if (rewriteMethod.startsWith("topTerms")) {
|
|
|
- int size = Integer.parseInt(rewriteMethod.substring("topTerms".length()));
|
|
|
- return new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(size);
|
|
|
+
|
|
|
+ if (firstDigit >= 0) {
|
|
|
+ final int size = Integer.parseInt(rewriteMethod.substring(firstDigit));
|
|
|
+ String rewriteMethodName = rewriteMethod.substring(0, firstDigit);
|
|
|
+
|
|
|
+ if (matcher.match(rewriteMethodName, TOP_TERMS)) {
|
|
|
+ return new MultiTermQuery.TopTermsScoringBooleanQueryRewrite(size);
|
|
|
+ }
|
|
|
+ if (matcher.match(rewriteMethodName, TOP_TERMS_BOOST)) {
|
|
|
+ return new MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite(size);
|
|
|
+ }
|
|
|
+ if (matcher.match(rewriteMethodName, TOP_TERMS_BLENDED_FREQS)) {
|
|
|
+ return new MultiTermQuery.TopTermsBlendedFreqScoringRewrite(size);
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
throw new IllegalArgumentException("Failed to parse rewrite_method [" + rewriteMethod + "]");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|