diff --git a/core/src/main/java/org/elasticsearch/common/ParseField.java b/core/src/main/java/org/elasticsearch/common/ParseField.java index a0978723d0e..8f5a2bd6706 100644 --- a/core/src/main/java/org/elasticsearch/common/ParseField.java +++ b/core/src/main/java/org/elasticsearch/common/ParseField.java @@ -21,7 +21,9 @@ package org.elasticsearch.common; import org.elasticsearch.common.logging.DeprecationLogger; import org.elasticsearch.common.logging.Loggers; +import java.util.Collections; import java.util.HashSet; +import java.util.Set; /** * Holds a field that can be found in a request while parsing and its different variants, which may be deprecated. @@ -34,6 +36,7 @@ public class ParseField { private final String underscoreName; private final String[] deprecatedNames; private String allReplacedWith = null; + private final String[] allNames; public ParseField(String value, String... deprecatedNames) { camelCaseName = Strings.toCamelCase(value); @@ -48,6 +51,11 @@ public class ParseField { } this.deprecatedNames = set.toArray(new String[set.size()]); } + Set allNames = new HashSet<>(); + allNames.add(camelCaseName); + allNames.add(underscoreName); + Collections.addAll(allNames, this.deprecatedNames); + this.allNames = allNames.toArray(new String[allNames.size()]); } public String getPreferredName(){ @@ -55,12 +63,6 @@ public class ParseField { } public String[] getAllNamesIncludedDeprecated() { - String[] allNames = new String[2 + deprecatedNames.length]; - allNames[0] = camelCaseName; - allNames[1] = underscoreName; - for (int i = 0; i < deprecatedNames.length; i++) { - allNames[i + 2] = deprecatedNames[i]; - } return allNames; } diff --git a/core/src/test/java/org/elasticsearch/common/ParseFieldTests.java b/core/src/test/java/org/elasticsearch/common/ParseFieldTests.java index 3770cd25c10..2b877018206 100644 --- a/core/src/test/java/org/elasticsearch/common/ParseFieldTests.java +++ b/core/src/test/java/org/elasticsearch/common/ParseFieldTests.java @@ -24,6 +24,7 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.CoreMatchers.sameInstance; +import static org.hamcrest.collection.IsArrayContainingInAnyOrder.arrayContainingInAnyOrder; public class ParseFieldTests extends ESTestCase { public void testParse() { @@ -87,4 +88,12 @@ public class ParseFieldTests extends ESTestCase { IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> field.match(randomFrom(allValues), true)); assertThat(e.getMessage(), containsString(" used, replaced by [like]")); } + + public void testGetAllNamesIncludedDeprecated() { + ParseField parseField = new ParseField("terms", "in"); + assertThat(parseField.getAllNamesIncludedDeprecated(), arrayContainingInAnyOrder("terms", "in")); + + parseField = new ParseField("more_like_this", "mlt"); + assertThat(parseField.getAllNamesIncludedDeprecated(), arrayContainingInAnyOrder("more_like_this", "moreLikeThis", "mlt")); + } }