Mapping an entity differently based on a discriminator


Follow-up on HSEARCH-3311.

In some cases, one could benefit of mapping an entity differently depending on the value of a property.

There are two use cases to distinguish:

  1. I want a different set of fields depending on a discriminator. This could be solved by including all fields in the schema, but populating them only when needed at runtime: e.g. only index largeTextField when entityState is ACTIVE, but not when it's ARCHIVED.

  2. I want the same fields, but configured differently depending on a discriminator. This could be solved by mapping the same entity type to multiple indexes () with a different schema (not included in ), and creating the documents in the relevant index at runtime ; e.g. map "BlogPost" to blogposts_fr and blogposts_de, use a different analyzer on the text field in each index, and route blog posts with language = FR to blogposts_fr, blog posts with language = DE to blogposts_de.

Use case 1: enable/disable fields based on a discriminator

Both use cases could be addressed with the concept of "groups" introduced in HSEARCH-3903. The user would provide a component that enables or disables "groups" dynamically, based on the state of the entity. All the groups that can be selected would be included in the index schema, and when indexing some fields would get enabled or not based on the dynamic group selection.

This would provide a feature similar to the AlternativeBinder, but much more powerful.

There is one unknown, though: how would @IndexedEmbedded.includeGroups interact with the dynamic group selection? If dynamic group selection is overridden by @IndexedEmbedded.includeGroups, it will likely not work as intended for the "multi-language" use case of AlternativeBinder. If dynamic group selection ignores @IndexedEmbedded.includeGroups, it will become impossible to exclude dynamically enabled fields from @IndexedEmbedded.

Maybe we should separate the two concepts, e.g. @GenericField(groups = ..., dynamicGroups = ...)?

Or maybe we should assign a static group to the "dynamic group resolver": the resolver and all corresponding fields would be included in the schema if the resolver's assigned static group is included, even if the field's (dynamic) groups are not included. Then it would be the user's responsibility to make sure static and dynamic groups use different names, so as not to include dynamic groups statically by mistake.

Use case 2: route to a different index with a different schema based on a discriminator

This should only be a matter of building upon what is described above and the work done in HSEARCH-3683: when declaring the indexes an entity type is mapped to, we would also declare the groups of field that are enabled for each index. Then at runtime, when an entity is routed to an index, it would use the appropriate mapping.






Yoann Rodière



Suitable for new contributors


Pull Request


Feedback Requested



Fix versions