简体   繁体   English

无法在@OneToMany关联中休眠搜索排序?

[英]Can't hibernate search sort in @OneToMany association?

I try to sort a list of Items for a customer by ordered Date. 我尝试按订购日期为客户分类商品清单。 The Date is only avalable through Item. 日期只能通过Item获得。 orderPositions.order.orderDate . orderPositions.order.orderDate But @IndexedEmbedded doesn't work. 但是@IndexedEmbedded不起作用。 There's no Exeption or Error but the result is only sorted by HS-logic. 没有例外或错误,但结果仅按HS-logic排序。

    @Entity
    @Indexed
    public class Item{
        @Id
        private long id;

        @Field(index = Index.YES, store = Store.YES, analyse = Analyse.YES, analyser = @Analyzer(definition = Constant.ANALYSER))
        private String description;

        @OneToMany(mappedBy = "item")
        @IndexedEmbedded
        private List<OrderPosition> orderPositions;

        @ManyToOne
        @IndexedEmbedded
        private Company company;           
    //getter&setter
    }

    @Entity
    public class OrderPosition{
        @Id
        private long id;

        @ManyToOne
        private Item item;

        @ManyToOne
        @IndexedEmbedded
        private Order order;
    //getter&setter
    }

    @Entity
    public class Order{
        @Id
        private long id;

        @ManyToOne
        private Customer customer;

        @Field(index = Index.NO, store = Store.NO, analyze = Analyze.NO)
        @SortableField
        private String orderDate;
    //getter&setter
    }

    @Entity
    public class Company{
        @Id
        private long id;

        @Field(index = Index.NO, store = Store.NO, analyze = Analyze.NO)
        @SortableField
        private String name;
    //getter&setter
    }

If I sort the List by Item. 如果我按项目对列表进行排序。 company.name it works fine. company.name它工作正常。

    queryService.buildFullTextQuery("searchText", Item.class, "description", "company.name").getResultList();

If I sort the List by Item. 如果我按项目对列表进行排序。 orderPosition.order.orderDate it's sorted by default(HS-logic) orderPosition.order.orderDate默认排序(HS逻辑)

    queryService.buildFullTextQuery("searchText", Item.class, "description", "orderPositions.order.orderDate").getResultList();

I build the FullTextQuery this way: 我以这种方式构建FullTextQuery:

    public FullTextQuery buildFullTextQuery(@NonNull String searchText, @NonNull Class<?> clazz, @NonNull String... fields) throws Exception {
        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(getEntityManager());
        QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(clazz).get();
        Query query = qb.keyword().onField(fields[0]).matching(searchText).createQuery();
        SortField sortField = new SortField(fields[1], SortField.Type.STRING, false);
        Sort sort = new Sort(sortField);
        return fullTextEntityManager.createFullTextQuery(query, clazz).setSort(sort);
    }

I think HS can't find the association for @OneToMany. 我认为HS无法找到@OneToMany的关联。 Is there a way to solve this prob? 有办法解决这个问题吗?

Thank you in advance 先感谢您

I can't tell you what's going on exactly without the results of your queries, but you're definitely doing something wrong here: you are trying to sort on a multi-valued field. 如果没有查询结果,我无法告诉您到底发生了什么,但是您在这里肯定做错了:您正在尝试对多值字段进行排序。 One item is linked to multiple orders, each having its own date. 一个项目链接到多个订单,每个订单都有自己的日期。 So there is multiple dates per item. 因此,每个项目有多个日期。

When you ask to compare two items that each have three dates, what should Hibernate Search do? 当您要求比较每个都有三个日期的两个项目时,Hibernate Search应该做什么? Compare only the latest dates? 仅比较最新日期? Compare only the earliest dates? 仅比较最早的日期? You didn't say, so your query is bound to return inconsistently ordered results. 您没有说,因此您的查询必然会返回不一致的排序结果。

Thing is, there is no way to tell Hibernate Search which value to pick in multi-valued fields, so your easiest way out is to explicitly create a single-valued field to sort on. 事实是,没有办法告诉Hibernate Search在多值字段中选择哪个值,因此最简单的方法是显式创建一个单值字段进行排序。

For instance, you could add a getter on Item to return the latest order, and add the @IndexedEmbedded there: 例如,您可以在Item上添加一个吸气剂以返回最新的订单,并在其中添加@IndexedEmbedded

@Entity
@Indexed
public class Item{
    @Id
    private long id;

    @Field(index = Index.YES, store = Store.YES, analyse = Analyse.YES, analyser = @Analyzer(definition = Constant.ANALYSER))
    private String description;

    @OneToMany(mappedBy = "item")
    @IndexedEmbedded
    private List<OrderPosition> orderPositions;

    @ManyToOne
    @IndexedEmbedded
    private Company company;           

    @javax.persistence.Transient
    public Order getLatestOrder() {
        Order latestOrder;
        // ... compute the latest order ...
        return latestOrder;
    }

    //getter&setter
}

Then sort on latestOrder.orderDate and you should be good. 然后对latestOrder.orderDate排序,您应该会很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM