diff --git a/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java b/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java index 9a7c6e23139..f5360f30292 100644 --- a/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java +++ b/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java @@ -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); } diff --git a/src/main/java/org/elasticsearch/index/query/FuzzyQueryParser.java b/src/main/java/org/elasticsearch/index/query/FuzzyQueryParser.java index 56aa6dc4ea6..4da392305f0 100644 --- a/src/main/java/org/elasticsearch/index/query/FuzzyQueryParser.java +++ b/src/main/java/org/elasticsearch/index/query/FuzzyQueryParser.java @@ -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);