Add support for enum fields during indexing (#1792)

* Add support for enum fields during indexing

* Add missing value extractor for enums
This commit is contained in:
Sebastian Sdorra
2021-09-01 15:40:38 +02:00
committed by GitHub
parent 9ad501b4c6
commit ea7964d224
5 changed files with 102 additions and 0 deletions

View File

@@ -73,6 +73,8 @@ class IndexableFields {
return new BooleanFieldFactory(indexType);
} else if (isInstant(fieldType)) {
return new InstantFieldFactory(indexType);
} else if (fieldType.isEnum()) {
return new EnumFieldFactory(indexType);
} else {
throw new UnsupportedTypeOfFieldException(fieldType, field.getName());
}
@@ -169,4 +171,23 @@ class IndexableFields {
}
}
private static class EnumFieldFactory implements IndexableFieldFactory {
private final Indexed.Type type;
public EnumFieldFactory(Indexed.Type type) {
this.type = type;
}
@Override
public Iterable<IndexableField> create(String name, Object value) {
String stringValue = value.toString();
if (type.isSearchable()) {
return singleton(new StringField(name, stringValue, Store.YES));
} else {
return singleton(new StoredField(name, stringValue));
}
}
}
}

View File

@@ -45,11 +45,18 @@ final class ValueExtractors {
return booleanExtractor(name);
} else if (TypeCheck.isInstant(type)) {
return instantExtractor(name);
} else if (type.isEnum()) {
return enumExtractor(name, type);
} else {
throw new UnsupportedTypeOfFieldException(type, name);
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static ValueExtractor enumExtractor(String name, Class type) {
return doc -> Enum.valueOf(type, doc.get(name));
}
@Nonnull
private static ValueExtractor stringExtractor(String name) {
return doc -> doc.get(name);