|
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 |
|
16 | 18 | import java.io.BufferedReader;
|
17 | 19 | import java.io.IOException;
|
@@ -225,12 +227,29 @@ public static TransportVersion fromId(int id) {
|
225 | 227 | * This will only return the latest known referable transport version for a given name and not its
|
226 | 228 | * patch versions. Patch versions are constructed as a linked list internally and may be found by
|
227 | 229 | * cycling through them in a loop using {@link TransportVersion#nextPatchVersion()}.
|
228 |
| - * |
229 | 230 | */
|
230 | 231 | public static TransportVersion fromName(String name) {
|
231 | 232 | TransportVersion known = VersionsHolder.ALL_VERSIONS_BY_NAME.get(name);
|
232 | 233 | if (known == null) {
|
233 |
| - throw new IllegalStateException("unknown transport version [" + name + "]"); |
| 234 | + LevenshteinDistance ld = new LevenshteinDistance(); |
| 235 | + List<Tuple<Float, String>> scoredNames = new ArrayList<>(); |
| 236 | + for (String key : VersionsHolder.ALL_VERSIONS_BY_NAME.keySet()) { |
| 237 | + float distance = ld.getDistance(name, key); |
| 238 | + if (distance > 0.7f) { |
| 239 | + scoredNames.add(new Tuple<>(distance, key)); |
| 240 | + } |
| 241 | + } |
| 242 | + StringBuilder message = new StringBuilder("Unknown transport version ["); |
| 243 | + message.append(name); |
| 244 | + message.append("]."); |
| 245 | + if (scoredNames.isEmpty() == false) { |
| 246 | + List<String> names = scoredNames.stream().map(Tuple::v2).toList(); |
| 247 | + message.append(" Did you mean "); |
| 248 | + message.append(names); |
| 249 | + message.append("?"); |
| 250 | + } |
| 251 | + message.append(" If this is a new transport version, run './gradle generateTransportVersion'."); |
| 252 | + throw new IllegalStateException(message.toString()); |
234 | 253 | }
|
235 | 254 | return known;
|
236 | 255 | }
|
|
0 commit comments