|
9 | 9 |
|
10 | 10 | package org.elasticsearch;
|
11 | 11 |
|
| 12 | +import org.apache.lucene.search.spell.LevenshteinDistance; |
12 | 13 | import org.elasticsearch.common.VersionId;
|
13 | 14 | import org.elasticsearch.common.io.stream.StreamInput;
|
14 | 15 | import org.elasticsearch.common.io.stream.StreamOutput;
|
| 16 | +import org.elasticsearch.core.Tuple; |
15 | 17 | import org.elasticsearch.internal.VersionExtension;
|
16 | 18 | import org.elasticsearch.plugins.ExtensionLoader;
|
17 | 19 |
|
@@ -234,12 +236,29 @@ public static TransportVersion fromId(int id) {
|
234 | 236 | * This will only return the latest known referable transport version for a given name and not its
|
235 | 237 | * patch versions. Patch versions are constructed as a linked list internally and may be found by
|
236 | 238 | * cycling through them in a loop using {@link TransportVersion#nextPatchVersion()}.
|
237 |
| - * |
238 | 239 | */
|
239 | 240 | public static TransportVersion fromName(String name) {
|
240 | 241 | TransportVersion known = VersionsHolder.ALL_VERSIONS_BY_NAME.get(name);
|
241 | 242 | if (known == null) {
|
242 |
| - throw new IllegalStateException("unknown transport version [" + name + "]"); |
| 243 | + LevenshteinDistance ld = new LevenshteinDistance(); |
| 244 | + List<Tuple<Float, String>> scoredNames = new ArrayList<>(); |
| 245 | + for (String key : VersionsHolder.ALL_VERSIONS_BY_NAME.keySet()) { |
| 246 | + float distance = ld.getDistance(name, key); |
| 247 | + if (distance > 0.7f) { |
| 248 | + scoredNames.add(new Tuple<>(distance, key)); |
| 249 | + } |
| 250 | + } |
| 251 | + StringBuilder message = new StringBuilder("Unknown transport version ["); |
| 252 | + message.append(name); |
| 253 | + message.append("]."); |
| 254 | + if (scoredNames.isEmpty() == false) { |
| 255 | + List<String> names = scoredNames.stream().map(Tuple::v2).toList(); |
| 256 | + message.append(" Did you mean "); |
| 257 | + message.append(names); |
| 258 | + message.append("?"); |
| 259 | + } |
| 260 | + message.append(" If this is a new transport version, run './gradle generateTransportVersion'."); |
| 261 | + throw new IllegalStateException(message.toString()); |
243 | 262 | }
|
244 | 263 | return known;
|
245 | 264 | }
|
|
0 commit comments