[英]DateBridge for ZonedDateTime in hibernate search gives exception while indexing the data
我的實體有一個ZonedDatetime
字段
@Indexed
@Entity
public class Book {
@Id
private Long id;
@Field
@DateBridge(resolution = Resolution.SECOND)
private ZonedDateTime createdAt;
// Other fields and setters & getters
}
當我嘗試保存實體時,應用程序拋出以下異常
field bridge: TwoWayString2FieldBridgeAdaptor [stringBridge=org.hibernate.search.elasticsearch.bridge.builtin.time.impl.ElasticsearchZonedDateTimeBridge@1bec1730]
at
以下是我嘗試過的兩個單元測試,可以重現該問題。
如果我創建一個分區日期時間ZoneId.of("UTC")
那么它對彈性搜索橋很好
@Test
public void testObjectToString_withZoneId() {
Date date = new Date();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC"));
try {
//this is good
ElasticsearchZonedDateTimeBridge.INSTANCE.objectToString(zonedDateTime);
} catch (DateTimeException e) {
Assert.fail(e.getMessage());
}
}
但它失敗了下面的代碼
@Test
public void testObjectToString_withZonedOffset() {
Date date = new Date();
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC);
try {
//this throws exception
ElasticsearchZonedDateTimeBridge.INSTANCE.objectToString(zonedDateTime);
} catch (DateTimeException e) {
// Unable to extract value: class java.time.ZonedDateTime
Assert.fail(e.getMessage());
}
}
我使用的休眠版本是,
hibernate-search-elasticsearch, hibernate-search-orm = 5.11.4.Final
這是我的完整堆棧跟蹤
Caused by: org.hibernate.search.bridge.BridgeException: Exception while calling bridge#set
entity class: com.x.x.x.x.Book
entity property path: expectedDepartureTime
document field name: expectedDepartureTime
field bridge: TwoWayString2FieldBridgeAdaptor [stringBridge=org.hibernate.search.elasticsearch.bridge.builtin.time.impl.ElasticsearchZonedDateTimeBridge@1bec1730]
at org.hibernate.search.bridge.util.impl.ContextualExceptionBridgeHelper.buildBridgeException(ContextualExceptionBridgeHelper.java:104) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.bridge.util.impl.ContextualExceptionBridgeHelper$OneWayConversionContextImpl.set(ContextualExceptionBridgeHelper.java:138) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.buildDocumentFieldsForProperties(DocumentBuilderIndexedEntity.java:669) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:466) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.getDocument(DocumentBuilderIndexedEntity.java:402) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.createUpdateWork(DocumentBuilderIndexedEntity.java:312) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
....
... 151 more
Caused by: java.time.DateTimeException: Unable to extract value: class java.time.ZonedDateTime
at java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:282) ~[?:1.8.0_222]
at java.time.format.DateTimeFormatterBuilder$ZoneIdPrinterParser.format(DateTimeFormatterBuilder.java:3787) ~[?:1.8.0_222]
at java.time.format.DateTimeFormatterBuilder$CompositePrinterParser.format(DateTimeFormatterBuilder.java:2190) ~[?:1.8.0_222]
at java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1746) ~[?:1.8.0_222]
at java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1720) ~[?:1.8.0_222]
at org.hibernate.search.elasticsearch.bridge.builtin.time.impl.ElasticsearchTemporalAccessorStringBridge.format(ElasticsearchTemporalAccessorStringBridge.java:75) ~[hibernate-search-elasticsearch-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.elasticsearch.bridge.builtin.time.impl.ElasticsearchTemporalAccessorStringBridge.objectToString(ElasticsearchTemporalAccessorStringBridge.java:56) ~[hibernate-search-elasticsearch-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.bridge.util.impl.String2FieldBridgeAdaptor.set(String2FieldBridgeAdaptor.java:31) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.bridge.util.impl.ContextualExceptionBridgeHelper$OneWayConversionContextImpl.set(ContextualExceptionBridgeHelper.java:135) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.buildDocumentFieldsForProperties(DocumentBuilderIndexedEntity.java:669) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.buildDocumentFields(DocumentBuilderIndexedEntity.java:466) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.getDocument(DocumentBuilderIndexedEntity.java:402) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.createUpdateWork(DocumentBuilderIndexedEntity.java:312) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.addWorkToQueue(DocumentBuilderIndexedEntity.java:254) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
at org.hibernate.search.engine.impl.WorkPlan$PerEntityWork.enqueueLuceneWork(WorkPlan.java:560) ~[hibernate-search-engine-5.11.3.Final.jar:5.11.3.Final]
ZoneOffset
是ZoneId
,因此此代碼是正確的:
ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC);
但是,在將分區日期時間序列化為 JSON 時,Hibernate Search 使用了一種非常特殊的格式。 該格式要求存在實際區域 ID(不僅僅是偏移量)。
這可以被視為一個錯誤,歡迎您打開一個錯誤報告,但恐怕這不是唯一的問題。 即使在 Search 5 中修復了這個問題,為了成功索引這樣的值,我們仍然需要向后移植HSEARCH-3548的修復程序,據我所知,這需要對 Hibernate 生成的 Elasticsearch 映射進行向后不兼容的更改搜索。 所以我不確定這是否真的可以在 5.11 中修復。
Hibernate Search 5 中的 Elasticsearch 支持是實驗性的。 如果可以,您將有更好的時間升級到 Hibernate Search 6(仍處於測試階段),它為 Elasticsearch 5.6 到 7.x 提供一流的支持,並且實際上對您的特定問題進行了測試(並且它有效)正確)。 但是,Hibernate Search 6 中的 API 是不同的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.