How to index Json Column

HI I Have in my entity 2 columns in this example, column name of String type and a Class named Workflow created by me with annotation @JdbcTypeCode(SqlTypes.JSON)

If I Put @FullTextField on my field name it works, but if I put @FullTextField on my field with the annotation SqlTypes.JSON, did not works

THe question: Is possible to index on hibernate search fields with @JdbcTypeCode(SqlTypes.JSON) ?

I’m not sure this is going to work.

We’ve only anticipated JSON mapping from strings so far (HSEARCH-1526), never relying on SqlTypes.JSON.

Your main challenge will probably be to have Hibernate Search not fail on bootstrap because it can’t find the properties of your Workflow class in the Hibernate ORM metamodel.

I can imagine a few hacks, but haven’t tested them. Please report back if something works.

First solution (unlikely to work, but worth a try):

@IndexedEmbedded
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.SHALLOW)
@Column
@JdbcTypeCode(SqlTypes.JSON)
private Workflow workflow;
public class Workflow {

   @FullTextField
   private String name;

}

See Hibernate Search 7.0.0.Final: Reference Documentation

Second solution (a bit uglier, but more likely to work):

@Column
@JdbcTypeCode(SqlTypes.JSON)
private Workflow workflow;

@Transient
@IndexedEmbedded(name = "workflow")
@IndexingDependency(derivedFrom = @ObjectPath({ 
        @PropertyValue(propertyName = "workflow")
}))
public Workflow getWorkflowForIndexing() {
    return workflow;
}
public class Workflow {

   @FullTextField
   private String name;

}

See Hibernate Search 7.0.0.Final: Reference Documentation

Third solution (much more likely to work, at the cost of never getting your entity reindexed when Workflow changes):

@IndexedEmbedded
@IndexingDependency(reindexOnUpdate = ReindexOnUpdate.NO)
@Column
@JdbcTypeCode(SqlTypes.JSON)
private Workflow workflow;
public class Workflow {

   @FullTextField
   private String name;

}

See Hibernate Search 7.0.0.Final: Reference Documentation

FWIW I created [HSEARCH-5079] - Hibernate JIRA .

Please keep us updated on the results of the solutions I gave above.

Hello thanks for your replies

I just put @IndexedEmbedded and worked, I was trying with wrong annotation

I Put like this

@IndexedEmbedded
@JdbcTypeCode(SqlTypes.JSON)
private Workflow workflow;
ANd worked perfect

Ah, well, I guess we did a good job on the ORM metamodel resolution thing, after all xD

Thanks for your feedback :slight_smile:

1 Like