Runtime polymophism with TypeBinder bridge

Hello,

This is basically the same problem as the one exposed in this post.

In short, the API is not designed to allow what you want to do, but there are workarounds.

I opened [HSEARCH-4488] - Hibernate JIRA to provide ways to address this use case without hacks.
Also, related: [HSEARCH-438] - Hibernate JIRA

This answer might work for you:

Adapting to your code:


// No field to search here
@Inheritance(strategy = InheritanceType.JOINED)
@TypeBinding(binder = @TypeBinderRef(type = FullNameBinder.class))
public abstract class AbstractEntity {

  @OneToOne(mappedBy = "parentEntity")
   private EntityToSearch entityToSearch ;
  //other fields

  // Hack, see https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057
  @Transient
  public abstract AbstractEntity getSelf();
}

@PrimaryKeyJoinColumn(name = "concreteClassA _id")
ConcreteClassA extends AbstractEntity {
  
  //Field to search 
  // @KeywordField // NOTE: Commented out because you don't need this if you already index fullName in your bridge.
  private String fullName;

  // Hack, see https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057
  @Override
  public ConcreteClassA getSelf() {
    return this;
  }
}

public class FullNameBinder implements TypeBinder {
    @Override
    public void bind(final TypeBindingContext context) {
       IndexFieldReference<String> fullName= context.indexSchemaElement().field("fullName", IndexFieldTypeFactory::asString).toReference();
        // Hack, see https://discourse.hibernate.org/t/runtime-polymophism-with-typebinder-bridge/6057
        context.dependencies().fromOtherEntity( ConcreteClassA.class, "self" )
            .use("fullName");
       context.bridge(AbstractEntity.class, new MyBridge(fullName));
    }

private static class MyBridge implements TypeBridge<AbstractEntity> {

		private final IndexFieldReference<String> fullName;

		private MyBridge(IndexFieldReference<String> fullNameField) {
			this.fullName= fullNameField;
		}

		@Override
		public void write(DocumentElement target, AbstractEntity abstractEntity , TypeBridgeWriteContext context) {
			if (abstractEntity instanceof ConcreteClassA ) {
				target.addValue(this.fullName, ((ConcreteClassA ) abstractEntity ).getFullName());
			}
		}

	}
}