You can take advantage of currently unsupported types in a property bridge by leveraging ElasticsearchExtension.get()
. Something like this should work:
public class MyBinder implements PropertyBinder<MyAnnotation> {
@Override
public void bind(PropertyBindingContext context) {
context.getDependencies().useRootOnly();
IndexFieldType<String> type = context.getTypeFactory()
.extension( ElasticsearchExtension.get() )
.asNative( "{ ... your native JSON mapping ... }" )
.toIndexFieldType();
context.setBridge( new Bridge(
context.getIndexSchemaElement()
.field( context.getBridgedElement().getName(), type )
.toReference()
) );
}
private static class Bridge implements PropertyBridge {
private Gson gson = new Gson();
private final IndexFieldReference<String> field;
private Bridge(IndexFieldReference<String> field) {
this.field = field;
}
@Override
public void write(DocumentElement target, Object bridgedElement,
PropertyBridgeWriteContext context) {
// Make sure to provide well-formatted JSON, escaping quotes in particular
String json = gson.toJson( new JsonPrimitive( (String) bridgedElement ) );
target.addValue( field, json );
}
}
}
Then apply @MyAnnotation
wherever you need this binding.
See here for more information about property bridges.
It is not possible to use the same technique in a value bridge (yet), because Hibernate Search needs to set the attributes defined in the @GenericField
annotation, and that’s not possible when the mapping is defined as raw JSON. I created https://hibernate.atlassian.net/browse/HSEARCH-3717 to try and address this limitation.