From 5d47ad4648d600eaf80a443bf011929f21825987 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Sun, 28 Oct 2012 09:57:38 +0100 Subject: [PATCH] lucene 4: upgraded FuzzyQueryParser + Builder to use integer edit distance rather than floats (bw compatible) --- .../elasticsearch/index/query/FuzzyQueryBuilder.java | 11 +++++++++++ .../elasticsearch/index/query/FuzzyQueryParser.java | 9 ++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) 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);