I need to implement a custom converter in spring data elastic-search
. I need to concatenate something to the text when saving and retrieving. I have seen a similar question is here. And in the answer is says it is implemented now and available. But I didn't found any example how to implement it. So anybody know how to do this?
You can find examples in the test code of the library.
You have to create a converter:
class FooConverter implements PropertyValueConverter {
public static final String PREFIX = "foo-";
@Override
public Object write(Object value) {
return PREFIX + value.toString();
}
@Override
public Object read(Object value) {
String valueString = value.toString();
if (valueString.startsWith(PREFIX)) {
return valueString.substring(PREFIX.length());
} else {
return value;
}
}
}
and then register it for the property of your entity class:
@Document(indexName = "foo")
class Entity {
@Field(type = FieldType.Text)
@ValueConverter(FooConverter.class)
private String someField;
// ...
}