【问题标题】:How to search in Hibernate Search by date as LocalDateTime?如何在 Hibernate Search 中按日期作为 LocalDateTime 进行搜索?
【发布时间】:2019-07-10 12:37:40
【问题描述】:

有:

  • 线;
  • 得分;
  • 日期。

在线搜索,店铺进行如下:

@Override
public List<Delivery> searchNewDeliveriesBy(long storeId, String text, Long selectedDateAsMills) {
    try (Session session = createSession()) {
        Store store = session.get(Store.class, storeId);
        FullTextEntityManager fullTextSession = Search.getFullTextSession(session);
        QueryBuilder qb = fullTextSession.getSearchFactory()
                .buildQueryBuilder()
                .forEntity(Delivery.class)
                .get();
        org.apache.lucene.search.Query luceneQuery = qb.bool()
                .must(
                        qb.keyword().onFields(DELIVERY_SEARCH_FIELDS).matching(text).createQuery()
                )
                .must(
                        qb.keyword().onField("store").matching(store).createQuery()
                )
                .createQuery();
        //noinspection unchecked
        return Search.getFullTextSession(session)
                .createFullTextQuery(luceneQuery, Delivery.class)
                .setSort(sortByFieldScore)
                .list();
    }
}

我的实体(没有太多):

public class Delivery {
    ...
    private LocalDateTime deliveryDateMin;

    private LocalDateTime deliveryDateMax;
    ...
}

我需要添加一个条件,使所选时间(方法变量selectedDateAsMills)介于deliveryDateMindeliveryDateMax 之间。

有LocalDateTimeBridge这样的类,但是网上没有使用的例子。这个答案(How to search between dates (Hibernate Search)?)不适合,因为有java.util.Date,而不是java.time.LocalDateTime

更新

@Entity
@Table(name = "DELIVERY")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Cacheable
@Indexed
public class Delivery {
    @Column(name = "delivery_date_min")
    @JsonFormat(pattern = "yyyy-mm-dd HH:MM")
    @Field
    @FieldBridge(impl = LocalDateTimeBridge.class)
    private LocalDateTime deliveryDateMin;
    @Column(name = "delivery_date_max")
    @JsonFormat(pattern = "yyyy-mm-dd HH:MM")
    @Field
    @FieldBridge(impl = LocalDateTimeBridge.class)
    private LocalDateTime deliveryDateMax;
}

【问题讨论】:

    标签: hibernate jpa java-8 lucene hibernate-search


    【解决方案1】:

    将您的毫秒数转换为 localDateTime,然后只需添加两个范围查询:

            LocalDateTime selectedDateTime = Instant.ofEpochMilli( selectedDateAsMills )
                    .atOffset( ZoneOffset.UTC ) // ofEpochMilli assumes the given millis are in the UTC timezone
                    .toLocalDateTime();
    
            org.apache.lucene.search.Query luceneQuery = qb.bool()
                    .must(
                            qb.keyword().onFields(DELIVERY_SEARCH_FIELDS).matching(text).createQuery()
                    )
                    .must(
                            qb.keyword().onField("store").matching(store).createQuery()
                    )
                    .must(
                            qb.range().onField("deliveryDateMin").below(selectedDateTime).createQuery()
                    )
                    .must(
                            qb.range().onField("deliveryDateMax").above(selectedDateTime).createQuery()
                    )
                    .createQuery();
    

    如果它对您不起作用,请解释发生了什么并提供有关您的实体的更多信息(特别是 @Field 注释)。

    【讨论】:

    • 据我了解,您需要为实体添加@Field @FieldBridge(impl = LocalDateTimeBridge.class) 注释。我说的对吗?
    • 对不起,我以为你已经有了这些注释,但没有把它们放在你的 sn-p 中。是的,你需要@Field,但你不需要@FieldBridge(impl = LocalDateTimeBridge.class):它将由 Hibernate Search 自动推断。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多