|
@@ -42,7 +42,7 @@ import java.util.Set;
|
|
|
*/
|
|
*/
|
|
|
public class MoreLikeThisQuery extends Query {
|
|
public class MoreLikeThisQuery extends Query {
|
|
|
|
|
|
|
|
- public static final float DEFAULT_PERCENT_TERMS_TO_MATCH = 0.3f;
|
|
|
|
|
|
|
+ public static final String DEFAULT_MINIMUM_SHOULD_MATCH = "30%";
|
|
|
|
|
|
|
|
private TFIDFSimilarity similarity;
|
|
private TFIDFSimilarity similarity;
|
|
|
|
|
|
|
@@ -50,7 +50,7 @@ public class MoreLikeThisQuery extends Query {
|
|
|
private Fields[] likeFields;
|
|
private Fields[] likeFields;
|
|
|
private String[] moreLikeFields;
|
|
private String[] moreLikeFields;
|
|
|
private Analyzer analyzer;
|
|
private Analyzer analyzer;
|
|
|
- private float percentTermsToMatch = DEFAULT_PERCENT_TERMS_TO_MATCH;
|
|
|
|
|
|
|
+ private String minimumShouldMatch = DEFAULT_MINIMUM_SHOULD_MATCH;
|
|
|
private int minTermFrequency = XMoreLikeThis.DEFAULT_MIN_TERM_FREQ;
|
|
private int minTermFrequency = XMoreLikeThis.DEFAULT_MIN_TERM_FREQ;
|
|
|
private int maxQueryTerms = XMoreLikeThis.DEFAULT_MAX_QUERY_TERMS;
|
|
private int maxQueryTerms = XMoreLikeThis.DEFAULT_MAX_QUERY_TERMS;
|
|
|
private Set<?> stopWords = XMoreLikeThis.DEFAULT_STOP_WORDS;
|
|
private Set<?> stopWords = XMoreLikeThis.DEFAULT_STOP_WORDS;
|
|
@@ -84,7 +84,7 @@ public class MoreLikeThisQuery extends Query {
|
|
|
result = 31 * result + minTermFrequency;
|
|
result = 31 * result + minTermFrequency;
|
|
|
result = 31 * result + minWordLen;
|
|
result = 31 * result + minWordLen;
|
|
|
result = 31 * result + Arrays.hashCode(moreLikeFields);
|
|
result = 31 * result + Arrays.hashCode(moreLikeFields);
|
|
|
- result = 31 * result + Float.floatToIntBits(percentTermsToMatch);
|
|
|
|
|
|
|
+ result = 31 * result + minimumShouldMatch.hashCode();
|
|
|
result = 31 * result + (stopWords == null ? 0 : stopWords.hashCode());
|
|
result = 31 * result + (stopWords == null ? 0 : stopWords.hashCode());
|
|
|
result = 31 * result + Float.floatToIntBits(getBoost());
|
|
result = 31 * result + Float.floatToIntBits(getBoost());
|
|
|
return result;
|
|
return result;
|
|
@@ -119,7 +119,7 @@ public class MoreLikeThisQuery extends Query {
|
|
|
return false;
|
|
return false;
|
|
|
if (!Arrays.equals(moreLikeFields, other.moreLikeFields))
|
|
if (!Arrays.equals(moreLikeFields, other.moreLikeFields))
|
|
|
return false;
|
|
return false;
|
|
|
- if (percentTermsToMatch != other.percentTermsToMatch)
|
|
|
|
|
|
|
+ if (!minimumShouldMatch.equals(other.minimumShouldMatch))
|
|
|
return false;
|
|
return false;
|
|
|
if (similarity == null) {
|
|
if (similarity == null) {
|
|
|
if (other.similarity != null)
|
|
if (other.similarity != null)
|
|
@@ -153,7 +153,7 @@ public class MoreLikeThisQuery extends Query {
|
|
|
BooleanQuery bq = new BooleanQuery();
|
|
BooleanQuery bq = new BooleanQuery();
|
|
|
if (this.likeFields != null) {
|
|
if (this.likeFields != null) {
|
|
|
Query mltQuery = mlt.like(this.likeFields);
|
|
Query mltQuery = mlt.like(this.likeFields);
|
|
|
- setMinimumShouldMatch((BooleanQuery) mltQuery, percentTermsToMatch);
|
|
|
|
|
|
|
+ Queries.applyMinimumShouldMatch((BooleanQuery) mltQuery, minimumShouldMatch);
|
|
|
bq.add(mltQuery, BooleanClause.Occur.SHOULD);
|
|
bq.add(mltQuery, BooleanClause.Occur.SHOULD);
|
|
|
}
|
|
}
|
|
|
if (this.likeText != null) {
|
|
if (this.likeText != null) {
|
|
@@ -163,7 +163,7 @@ public class MoreLikeThisQuery extends Query {
|
|
|
}
|
|
}
|
|
|
//LUCENE 4 UPGRADE this mapps the 3.6 behavior (only use the first field)
|
|
//LUCENE 4 UPGRADE this mapps the 3.6 behavior (only use the first field)
|
|
|
Query mltQuery = mlt.like(moreLikeFields[0], readers);
|
|
Query mltQuery = mlt.like(moreLikeFields[0], readers);
|
|
|
- setMinimumShouldMatch((BooleanQuery) mltQuery, percentTermsToMatch);
|
|
|
|
|
|
|
+ Queries.applyMinimumShouldMatch((BooleanQuery) mltQuery, minimumShouldMatch);
|
|
|
bq.add(mltQuery, BooleanClause.Occur.SHOULD);
|
|
bq.add(mltQuery, BooleanClause.Occur.SHOULD);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -231,12 +231,24 @@ public class MoreLikeThisQuery extends Query {
|
|
|
this.analyzer = analyzer;
|
|
this.analyzer = analyzer;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public float getPercentTermsToMatch() {
|
|
|
|
|
- return percentTermsToMatch;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Number of terms that must match the generated query expressed in the
|
|
|
|
|
+ * common syntax for minimum should match.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @see org.elasticsearch.common.lucene.search.Queries#calculateMinShouldMatch(int, String)
|
|
|
|
|
+ */
|
|
|
|
|
+ public String getMinimumShouldMatch() {
|
|
|
|
|
+ return minimumShouldMatch;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void setPercentTermsToMatch(float percentTermsToMatch) {
|
|
|
|
|
- this.percentTermsToMatch = percentTermsToMatch;
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Number of terms that must match the generated query expressed in the
|
|
|
|
|
+ * common syntax for minimum should match. Defaults to <tt>30%</tt>.
|
|
|
|
|
+ *
|
|
|
|
|
+ * @see org.elasticsearch.common.lucene.search.Queries#calculateMinShouldMatch(int, String)
|
|
|
|
|
+ */
|
|
|
|
|
+ public void setMinimumShouldMatch(String minimumShouldMatch) {
|
|
|
|
|
+ this.minimumShouldMatch = minimumShouldMatch;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public int getMinTermFrequency() {
|
|
public int getMinTermFrequency() {
|
|
@@ -310,9 +322,4 @@ public class MoreLikeThisQuery extends Query {
|
|
|
public void setBoostTermsFactor(float boostTermsFactor) {
|
|
public void setBoostTermsFactor(float boostTermsFactor) {
|
|
|
this.boostTermsFactor = boostTermsFactor;
|
|
this.boostTermsFactor = boostTermsFactor;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- private static void setMinimumShouldMatch(BooleanQuery bq, float percentTermsToMatch) {
|
|
|
|
|
- BooleanClause[] clauses = bq.getClauses();
|
|
|
|
|
- bq.setMinimumNumberShouldMatch((int) (clauses.length * percentTermsToMatch));
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|