Hibernate Search 6 suggesters + completion type

We released Alpha8, which fixes this bug but also updates the bridge APIs. An official announcement will follow later today as I’m able (I’m traveling).

I’ll drop an updated version of my earlier comment here:

Define an annotation:

package com.acme;

// ... imports ...

@PropertyBinding(binder = @PropertyBinderRef(type = com.acme.MyCompletionFieldBridge.Binder.class))
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD })
@Documented
@Repeatable(CompletionField.List.class)
public @interface CompletionField {

	String name();

	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.METHOD, ElementType.FIELD })
	@Documented
	@interface List {
		CompletionField[] value();
	}
}

Then the bridge and its binder:

package com.acme;

// ... imports ...

public class MyCompletionFieldBridge implements PropertyBridge {
	private final IndexFieldReference<String> valueFieldReference;

	private MyCompletionFieldBridge(IndexFieldReference<String> valueFieldReference) {
		this.valueFieldReference = valueFieldReference;
	}

	@Override
	public void write(DocumentElement target, Object bridgedElement, PropertyBridgeWriteContext context) {
		String sourceValue = (String) bridgedElement;
		if ( sourceValue != null ) {
			// The "native" field type requires you to perform the JSON conversion yourself.
			// Ideally you should use a JSON library to properly escape the strings.
			target.addValue( valueFieldReference, "['" + sourceValue + "']" );
		}
	}

	public static class Binder implements PropertyBinder<CompletionField> {
		private String fieldName;

		@Override
		public void initialize(CompletionField annotation) {
			fieldName = annotation.name();
		}

		@Override
		public void bind(PropertyBindingContext context) {
			if ( fieldName == null || fieldName.isEmpty() ) {
				throw new IllegalArgumentException( "fieldName is a mandatory parameter" );
			}
			context.getDependencies().useRootOnly();
			IndexFieldReference<String> valueFieldReference = schema.field(
					fieldName,
					f -> f.extension( ElasticsearchExtension.get() )
							.asNative( "{\"type\": \"completion\"}" )
			).toReference();
			context.setBridge( new MyCompletionFieldBridge( valueFieldReference ) );
		}
	}
}

Then use the annotation in your mapping:

@CompletionField(name = "myTextProperty_suggest")
String myTextProperty;