|
14 | 14 | import org.apache.lucene.search.spell.LevenshteinDistance; |
15 | 15 | import org.apache.lucene.util.CollectionUtil; |
16 | 16 | import org.elasticsearch.ExceptionsHelper; |
| 17 | +import org.elasticsearch.common.ReferenceDocs; |
| 18 | +import org.elasticsearch.common.Strings; |
17 | 19 | import org.elasticsearch.common.regex.Regex; |
18 | 20 | import org.elasticsearch.common.util.Maps; |
19 | 21 | import org.elasticsearch.common.util.set.Sets; |
@@ -560,29 +562,40 @@ void validate(final String key, final Settings settings, final boolean validateV |
560 | 562 | void validate(final String key, final Settings settings, final boolean validateValue, final boolean validateInternalOrPrivateIndex) { |
561 | 563 | Setting<?> setting = getRaw(key); |
562 | 564 | if (setting == null) { |
563 | | - LevenshteinDistance ld = new LevenshteinDistance(); |
564 | | - List<Tuple<Float, String>> scoredKeys = new ArrayList<>(); |
565 | | - for (String k : this.keySettings.keySet()) { |
566 | | - float distance = ld.getDistance(key, k); |
567 | | - if (distance > 0.7f) { |
568 | | - scoredKeys.add(new Tuple<>(distance, k)); |
569 | | - } |
570 | | - } |
571 | | - CollectionUtil.timSort(scoredKeys, (a, b) -> b.v1().compareTo(a.v1())); |
572 | | - String msgPrefix = "unknown setting"; |
573 | 565 | SecureSettings secureSettings = settings.getSecureSettings(); |
574 | | - if (secureSettings != null && settings.getSecureSettings().getSettingNames().contains(key)) { |
575 | | - msgPrefix = "unknown secure setting"; |
| 566 | + String msgPrefix = (secureSettings != null && secureSettings.getSettingNames().contains(key)) |
| 567 | + ? "unknown secure setting" |
| 568 | + : "unknown setting"; |
| 569 | + if (key.startsWith(ARCHIVED_SETTINGS_PREFIX)) { |
| 570 | + throw new IllegalArgumentException( |
| 571 | + Strings.format( |
| 572 | + "%s [%s] was archived after upgrading, and must be removed. See [%s] for details.", |
| 573 | + msgPrefix, |
| 574 | + key, |
| 575 | + ReferenceDocs.ARCHIVED_SETTINGS |
| 576 | + ) |
| 577 | + ); |
576 | 578 | } |
577 | | - String msg = msgPrefix + " [" + key + "]"; |
578 | | - List<String> keys = scoredKeys.stream().map((a) -> a.v2()).toList(); |
| 579 | + List<String> keys = findSimilarKeys(key); |
579 | 580 | if (keys.isEmpty() == false) { |
580 | | - msg += " did you mean " + (keys.size() == 1 ? "[" + keys.get(0) + "]" : "any of " + keys.toString()) + "?"; |
| 581 | + throw new IllegalArgumentException( |
| 582 | + Strings.format( |
| 583 | + "%s [%s] did you mean %s?", |
| 584 | + msgPrefix, |
| 585 | + key, |
| 586 | + (keys.size() == 1 ? "[" + keys.getFirst() + "]" : "any of " + keys) |
| 587 | + ) |
| 588 | + ); |
581 | 589 | } else { |
582 | | - msg += " please check that any required plugins are installed, or check the breaking changes documentation for removed " |
583 | | - + "settings"; |
| 590 | + throw new IllegalArgumentException( |
| 591 | + Strings.format( |
| 592 | + "%s [%s] please check that any required plugins are installed," |
| 593 | + + " or check the breaking changes documentation for removed settings", |
| 594 | + msgPrefix, |
| 595 | + key |
| 596 | + ) |
| 597 | + ); |
584 | 598 | } |
585 | | - throw new IllegalArgumentException(msg); |
586 | 599 | } else { |
587 | 600 | Set<Setting.SettingDependency> settingsDependencies = setting.getSettingsDependencies(key); |
588 | 601 | if (setting.hasComplexMatcher()) { |
@@ -635,6 +648,19 @@ void validate(final String key, final Settings settings, final boolean validateV |
635 | 648 | } |
636 | 649 | } |
637 | 650 |
|
| 651 | + private List<String> findSimilarKeys(String key) { |
| 652 | + LevenshteinDistance ld = new LevenshteinDistance(); |
| 653 | + List<Tuple<Float, String>> scoredKeys = new ArrayList<>(); |
| 654 | + for (String k : this.keySettings.keySet()) { |
| 655 | + float distance = ld.getDistance(key, k); |
| 656 | + if (distance > 0.7f) { |
| 657 | + scoredKeys.add(new Tuple<>(distance, k)); |
| 658 | + } |
| 659 | + } |
| 660 | + CollectionUtil.timSort(scoredKeys, (a, b) -> b.v1().compareTo(a.v1())); |
| 661 | + return scoredKeys.stream().map((a) -> a.v2()).toList(); |
| 662 | + } |
| 663 | + |
638 | 664 | /** |
639 | 665 | * Transactional interface to update settings. |
640 | 666 | * @see Setting |
|
0 commit comments