| The DSLs in Search 6 are relatively verbose, even when using the lambda syntax:
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager( entityManager );
FullTextSearchTarget<Book> target = fullTextEntityManager.search( Book.class );
FullTextQuery<MyProjectionBean> query = target.query()
.asProjection( f ->
f.composite(
MyProjectionBean::new,
f.field( "id_stored", Long.class )
f.field( "title", String.class )
).toProjection()
)
.predicate( f -> f.match()
.onFields( "title", "authors.name" )
.matching( "Refactoring: Improving the Design of Existing Code" )
.toPredicate()
)
.sort( f -> f.sort()
.byField( "title" ).asc()
.then().byField( "subtitle" ).asc()
.toSort()
)
.build();
List<MyProjectionBean> result = query.getResultList();
We could try to improve on that. One idea would be to remove the need to call the toPredicate, toProjection and toSort methods even at the top level:
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager( entityManager );
FullTextSearchTarget<Book> target = fullTextEntityManager.search( Book.class );
FullTextQuery<MyProjectionBean> query = target.query()
.asProjection( f -> f.composite(
MyProjectionBean::new,
f.field( "id_stored", Long.class )
f.field( "title", String.class )
) )
.predicate( f -> f.match()
.onFields( "title", "authors.name" )
.matching( "Refactoring: Improving the Design of Existing Code" )
)
.sort( f -> f.sort()
.byField( "title" ).asc()
.then().byField( "subtitle" ).asc()
)
.build();
List<MyProjectionBean> result = query.getResultList();
That would, however, remove the ability to returned 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? |