如何在 Hibernate Search 中按日期搜索 LocalDateTime?
How to search in Hibernate Search by date as LocalDateTime?
有:
- 行;
- 得分;
- 日期。
在线搜索和商店进行如下:
@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
)在deliveryDateMin
和deliveryDateMax
之间。
有一个class类似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;
}
将您的毫秒数转换为 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
注释)。
有:
- 行;
- 得分;
- 日期。
在线搜索和商店进行如下:
@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
)在deliveryDateMin
和deliveryDateMax
之间。
有一个class类似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;
}
将您的毫秒数转换为 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
注释)。