Reduce the verbosity of the predicate/projection/sort DSLs and index schema DSL


The DSLs in Search 6 are relatively verbose, even when using the lambda syntax:

We could try to improve on that.
One idea would be to remove the need to call the toPredicate, toProjection, toSort}}‌and {{toIndexFieldType methods even at the top level:

That would, however, remove the ability to return a cached SearchPredicate/SearchProjection/SearchSort object from the lambda: the lambda would be expected to return a Search(Predicate/Projection/Sort)TerminalContext.

Maybe we should add a method to the DSL to convert a SearchPredicate/SearchProjection/SearchSort to a Search(Predicate/Projection/Sort)TerminalContext? Something like f.from(searchPredicate)? If we do that, it could make sense to change the naming around SearchPredicate/SearchProjection/SearchSort: we could make it more obvious that these are mainly for re-use, and are not really necessary if you don't cache them. Maybe rename them to ReusablePredicate, or something similar?

Note that the index schema DSL also suffers from unnecessary verbosity:

With the same solution as above, we would get to something like this:

We could improve this even further by removing the createAccessor call:

… but then there would be an inconsistency with object fields, where the‌{{createAccessor}} call is actually necessary, because the user needs a builder to add sub-fields to the object field:

Maybe we should just acknowledge this inconsistency by considering the objectField object above as a builder? Then it would make more sense: when you use a builder, you expect a final call. Something like this maybe:

Also to be taken into account: we may want one day to add "sub-fields" to non-object fields; see HSEARCH-3465.





Yoann Rodière


Yoann Rodière



Suitable for new contributors


Feedback Requested



Fix versions