Hibernate Search 6 组合投影不起作用

Hibernate Search 6 combine projections not working

我已经实施了 Hibernate Search,但目前遇到了 Projection 问题。 所有相关数据都已编入索引,因此我尝试将它们投射到 DTO。 作为文档(https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-projection-composite),我尝试了以下

  searchSession.search(Building.class)
                .select(f -> f.composite(BuildingDto::new,
                        f.field("id", String.class),
                        f.field("name", String.class),
                        f.field("street", String.class),
                        f.field("zip", String.class),
                        f.field("town", String.class)))
                .where(f -> f.wildcard().fields("id", "name", "town", "street", "zip").matching(search))
                .sort(f -> f.field("id").desc())
                .fetch(20);

虽然 运行 构建我得到以下错误消息:

java: method composite in interface org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactory<R,E> cannot be applied to given types;
  required: org.hibernate.search.engine.search.projection.dsl.ProjectionFinalStep<?>[]
  found: BuildingDto::new,org.hibernate.search.engine.search.projection.dsl.FieldProjectionValueStep<capture#1 of ?,java.lang.String>,org.hibernate.search.engine.search.projection.dsl.FieldProjectionValueStep<capture#2 of ?,java.lang.String>,org.hibernate.search.engine.search.projection.dsl.FieldProjectionValueStep<capture#3 of ?,java.lang.String>,org.hibernate.search.engine.search.projection.dsl.FieldProjectionValueStep<capture#4 of ?,java.lang.String>,org.hibernate.search.engine.search.projection.dsl.FieldProjectionValueStep<capture#5 of ?,java.lang.String>
  reason: varargs mismatch; bad return type in method reference
      BuildingDto cannot be converted to org.hibernate.search.engine.search.projection.SearchProjection<java.lang.Object>

BuildingDto 具有以下构造函数:


    public BuildingDto (){}
    public BuildingDto (String id, String name,String street, String zip, String town) {
       //setting vars
    }

由于explained in the reference documentation,type-safe复合投影目前最多只支持三个内部投影。

如果您需要更多,创建 DTO 的“转换器”函数将需要接受 List<?> 并进行一些转换:

  searchSession.search(Building.class)
                .select(f -> f.composite(list -> new BuildingDto(
                                (String) list.get(0),
                                (String) list.get(1),
                                (String) list.get(2),
                                (String) list.get(3),
                                (String) list.get(4)
                        ),
                        f.field("id", String.class),
                        f.field("name", String.class),
                        f.field("street", String.class),
                        f.field("zip", String.class),
                        f.field("town", String.class)))
                .where(f -> f.wildcard().fields("id", "name", "town", "street", "zip").matching(search))
                .sort(f -> f.field("id").desc())
                .fetch(20);

是的,这很丑陋,但我们正在努力better solutions