Skip to content

Commit 592ab04

Browse files
alexshadow007jimczi
authored andcommitted
Change default value to true for transpositions parameter of fuzzy query (#26901)
1 parent d97b21d commit 592ab04

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

core/src/main/java/org/elasticsearch/index/query/FuzzyQueryBuilder.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder<FuzzyQueryBuilder> i
5454
public static final int DEFAULT_MAX_EXPANSIONS = FuzzyQuery.defaultMaxExpansions;
5555

5656
/** Default as to whether transpositions should be treated as a primitive edit operation,
57-
* instead of classic Levenshtein algorithm. Defaults to false. */
58-
public static final boolean DEFAULT_TRANSPOSITIONS = false;
57+
* instead of classic Levenshtein algorithm. Defaults to true. */
58+
public static final boolean DEFAULT_TRANSPOSITIONS = FuzzyQuery.defaultTranspositions;
5959

6060
private static final ParseField TERM_FIELD = new ParseField("term");
6161
private static final ParseField VALUE_FIELD = new ParseField("value");
@@ -74,7 +74,6 @@ public class FuzzyQueryBuilder extends AbstractQueryBuilder<FuzzyQueryBuilder> i
7474

7575
private int maxExpansions = DEFAULT_MAX_EXPANSIONS;
7676

77-
//LUCENE 4 UPGRADE we need a testcase for this + documentation
7877
private boolean transpositions = DEFAULT_TRANSPOSITIONS;
7978

8079
private String rewrite;

core/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java

+16
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ public void testFromJson() throws IOException {
241241
checkGeneratedJson(json, parsed);
242242
assertEquals(json, 42.0, parsed.boost(), 0.00001);
243243
assertEquals(json, 2, parsed.fuzziness().asFloat(), 0f);
244+
assertEquals(json, false, parsed.transpositions());
244245
}
245246

246247
public void testParseFailsWithMultipleFields() throws IOException {
@@ -290,4 +291,19 @@ public void testParseFailsWithValueArray() {
290291
ParsingException e = expectThrows(ParsingException.class, () -> parseQuery(query));
291292
assertEquals("[fuzzy] unexpected token [START_ARRAY] after [value]", e.getMessage());
292293
}
294+
295+
public void testToQueryWithTranspositions() throws Exception {
296+
assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0);
297+
Query query = new FuzzyQueryBuilder(STRING_FIELD_NAME, "text").toQuery(createShardContext());
298+
assertThat(query, instanceOf(FuzzyQuery.class));
299+
assertEquals(FuzzyQuery.defaultTranspositions, ((FuzzyQuery)query).getTranspositions());
300+
301+
query = new FuzzyQueryBuilder(STRING_FIELD_NAME, "text").transpositions(true).toQuery(createShardContext());
302+
assertThat(query, instanceOf(FuzzyQuery.class));
303+
assertEquals(true, ((FuzzyQuery)query).getTranspositions());
304+
305+
query = new FuzzyQueryBuilder(STRING_FIELD_NAME, "text").transpositions(false).toQuery(createShardContext());
306+
assertThat(query, instanceOf(FuzzyQuery.class));
307+
assertEquals(false, ((FuzzyQuery)query).getTranspositions());
308+
}
293309
}

docs/reference/migration/migrate_7_0/search.asciidoc

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
[[breaking_70_search_changes]]
2-
=== Search changes
2+
=== Search and Query DSL changes
3+
4+
==== Changes to queries
5+
* The default value for `transpositions` parameter of `fuzzy` query
6+
has been changed to `true`.
37

48
==== Adaptive replica selection enabled by default
59

docs/reference/query-dsl/fuzzy-query.asciidoc

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ GET /_search
3636
"boost" : 1.0,
3737
"fuzziness" : 2,
3838
"prefix_length" : 0,
39-
"max_expansions": 100
39+
"max_expansions": 100,
40+
"transpositions": false
4041
}
4142
}
4243
}
@@ -66,7 +67,7 @@ GET /_search
6667
`transpositions`::
6768

6869
Whether fuzzy transpositions (`ab` -> `ba`) are supported.
69-
Default is `false`.
70+
Default is `true`.
7071

7172
WARNING: This query can be very heavy if `prefix_length` is set to `0` and if
7273
`max_expansions` is set to a high number. It could result in every term in the

0 commit comments

Comments
 (0)