lucene 4: upgraded FuzzyQueryParser + Builder to use integer edit distance rather

than floats (bw compatible)
This commit is contained in:
Simon Willnauer 2012-10-28 09:57:38 +01:00 committed by Shay Banon
parent b1eaec6c6a
commit 5d47ad4648
2 changed files with 19 additions and 1 deletions

View File

@ -41,6 +41,9 @@ public class FuzzyQueryBuilder extends BaseQueryBuilder implements BoostableQuer
private Integer prefixLength;
private Integer maxExpansions;
//LUCENE 4 UPGRADE we need a testcase for this + documentation
private Boolean transpositions = true;
/**
* Constructs a new term query.
@ -81,6 +84,11 @@ public class FuzzyQueryBuilder extends BaseQueryBuilder implements BoostableQuer
this.maxExpansions = maxExpansions;
return this;
}
public FuzzyQueryBuilder transpositions(boolean transpositions) {
this.transpositions = transpositions;
return this;
}
@Override
public void doXContent(XContentBuilder builder, Params params) throws IOException {
@ -93,6 +101,9 @@ public class FuzzyQueryBuilder extends BaseQueryBuilder implements BoostableQuer
if (boost != -1) {
builder.field("boost", boost);
}
if (!transpositions) {
builder.field("transpositions", transpositions);
}
if (minSimilarity != null) {
builder.field("min_similarity", minSimilarity);
}

View File

@ -60,9 +60,11 @@ public class FuzzyQueryParser implements QueryParser {
String value = null;
float boost = 1.0f;
//LUCENE 4 UPGRADE we should find a good default here I'd vote for 1.0 -> 1 edit
String minSimilarity = "0.5";
int prefixLength = FuzzyQuery.defaultPrefixLength;
int maxExpansions = FuzzyQuery.defaultMaxExpansions;
boolean transpositions = true;
MultiTermQuery.RewriteMethod rewriteMethod = null;
token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
@ -83,6 +85,8 @@ public class FuzzyQueryParser implements QueryParser {
prefixLength = parser.intValue();
} else if ("max_expansions".equals(currentFieldName) || "maxExpansions".equals(currentFieldName)) {
maxExpansions = parser.intValue();
} else if ("transpositions".equals(currentFieldName)) {
transpositions = parser.booleanValue();
} else if ("rewrite".equals(currentFieldName)) {
rewriteMethod = QueryParsers.parseRewriteMethod(parser.textOrNull(), null);
} else {
@ -109,7 +113,10 @@ public class FuzzyQueryParser implements QueryParser {
}
}
if (query == null) {
query = new FuzzyQuery(new Term(fieldName, value), Float.parseFloat(minSimilarity), prefixLength, maxExpansions);
//LUCENE 4 UPGRADE we need to document that this should now be an int rather than a float
int edits = FuzzyQuery.floatToEdits(Float.parseFloat(minSimilarity),
value.codePointCount(0, value.length()));
query = new FuzzyQuery(new Term(fieldName, value), edits, prefixLength, maxExpansions, transpositions);
}
if (query instanceof MultiTermQuery) {
QueryParsers.setRewriteMethod((MultiTermQuery) query, rewriteMethod);