use of org.apache.lucene.search.spell.LevenshteinDistance in project crate by crate.
the class Schemas method getSimilarSchemas.
private List<String> getSimilarSchemas(User user, String schema) {
LevenshteinDistance levenshteinDistance = new LevenshteinDistance();
ArrayList<Candidate> candidates = new ArrayList<>();
for (String availableSchema : schemas.keySet()) {
if (user.hasAnyPrivilege(Privilege.Clazz.SCHEMA, availableSchema)) {
float score = levenshteinDistance.getDistance(schema.toLowerCase(Locale.ENGLISH), availableSchema.toLowerCase(Locale.ENGLISH));
if (score > 0.7f) {
candidates.add(new Candidate(score, availableSchema));
}
}
}
candidates.sort(Comparator.comparing((Candidate x) -> x.score).reversed());
return candidates.stream().limit(5).map(x -> x.name).collect(Collectors.toList());
}
use of org.apache.lucene.search.spell.LevenshteinDistance in project crate by crate.
the class AbstractScopedSettings method validate.
/**
* Validates that the settings is valid.
*
* @param key the key of the setting to validate
* @param settings the settings
* @param validateDependencies true if dependent settings should be validated
* @param validateInternalOrPrivateIndex true if internal index settings should be validated
* @throws IllegalArgumentException if the setting is invalid
*/
void validate(final String key, final Settings settings, final boolean validateDependencies, final boolean validateInternalOrPrivateIndex) {
Setting<?> setting = getRaw(key);
if (setting == null) {
LevenshteinDistance ld = new LevenshteinDistance();
List<Tuple<Float, String>> scoredKeys = new ArrayList<>();
for (String k : this.keySettings.keySet()) {
float distance = ld.getDistance(key, k);
if (distance > 0.7f) {
scoredKeys.add(new Tuple<>(distance, k));
}
}
CollectionUtil.timSort(scoredKeys, (a, b) -> b.v1().compareTo(a.v1()));
String msg = "unknown setting [" + key + "]";
List<String> keys = scoredKeys.stream().map((a) -> a.v2()).collect(Collectors.toList());
if (keys.isEmpty() == false) {
msg += " did you mean " + (keys.size() == 1 ? "[" + keys.get(0) + "]" : "any of " + keys.toString()) + "?";
} else {
msg += " please check that any required plugins are installed," + " or check the breaking changes documentation for removed settings";
}
throw new IllegalArgumentException(msg);
} else {
Set<Setting.SettingDependency> settingsDependencies = setting.getSettingsDependencies(key);
if (setting.hasComplexMatcher()) {
setting = setting.getConcreteSetting(key);
}
if (validateDependencies && settingsDependencies.isEmpty() == false) {
for (final Setting.SettingDependency settingDependency : settingsDependencies) {
final Setting<?> dependency = settingDependency.getSetting();
// validate the dependent setting is set
if (dependency.existsOrFallbackExists(settings) == false) {
final String message = String.format(Locale.ROOT, "missing required setting [%s] for setting [%s]", dependency.getKey(), setting.getKey());
throw new IllegalArgumentException(message);
}
// validate the dependent setting value
settingDependency.validate(setting.getKey(), setting.get(settings), dependency.get(settings));
}
}
// the only time that validateInternalOrPrivateIndex should be true is if this call is coming via the update settings API
if (validateInternalOrPrivateIndex) {
if (setting.isInternalIndex()) {
throw new IllegalArgumentException("can not update internal setting [" + setting.getKey() + "]; this setting is managed via a dedicated API");
} else if (setting.isPrivateIndex()) {
throw new IllegalArgumentException("can not update private setting [" + setting.getKey() + "]; this setting is managed by CrateDB");
}
}
}
Iterator<? extends Setting<?>> validationDependencies = setting.getValidationDependencies();
if (validationDependencies.hasNext()) {
Settings previousSettings = this.lastSettingsApplied;
Settings.Builder settingsInclDependencies = Settings.builder().put(settings);
while (validationDependencies.hasNext()) {
Setting<?> dependency = validationDependencies.next();
if (!settings.hasValue(dependency.getKey()) && previousSettings.hasValue(dependency.getKey())) {
settingsInclDependencies.copy(dependency.getKey(), previousSettings);
}
}
setting.get(settingsInclDependencies.build());
} else {
setting.get(settings);
}
}
use of org.apache.lucene.search.spell.LevenshteinDistance in project crate by crate.
the class Schemas method getSimilarTables.
private static List<String> getSimilarTables(User user, String tableName, Iterable<TableInfo> tables) {
LevenshteinDistance levenshteinDistance = new LevenshteinDistance();
ArrayList<Candidate> candidates = new ArrayList<>();
for (TableInfo table : tables) {
if (user.hasAnyPrivilege(Privilege.Clazz.TABLE, table.ident().fqn())) {
String candidate = table.ident().name();
float score = levenshteinDistance.getDistance(tableName.toLowerCase(Locale.ENGLISH), candidate.toLowerCase(Locale.ENGLISH));
if (score > 0.7f) {
candidates.add(new Candidate(score, candidate));
}
}
}
candidates.sort(Comparator.comparing((Candidate x) -> x.score).reversed());
return candidates.stream().limit(5).map(x -> x.name).collect(Collectors.toList());
}
Aggregations