【发布时间】:2019-06-19 10:41:01
【问题描述】:
当我们对索引日期使用日期范围查询时,Hibernate 搜索不会返回任何结果。
所有其他范围查询都按预期工作
我们在 elasticsearch 之上使用休眠搜索,我们的所有查询都按预期运行,除了我们尝试在给定范围内查找日期的查询。
我们还有其他关于体重、身高等的范围查询,它们按预期工作。
用 Luke 检查索引,日期似乎按预期编入索引。
我们有以下仅显示相关字段的域对象
@Indexed
public class Person{
@IndexedEmbedded
@OneToMany(mappedBy = "person", fetch = FetchType.LAZY)
@Cascade({org.hibernate.annotations.CascadeType.PERSIST})
private Set<DateOfBirth> datesOfBirth = new HashSet<>();
}
public class DateOfBirth{
@Basic
@Field
@DateBridge(resolution = Resolution.MILLISECOND)
@Column(name = "date_of_birth")
private Date dateOfBirth;
}
我还在 dateOfBirth 字段上尝试了以下方法
@Basic
@Field(bridge = @FieldBridge( impl = ElasticsearchDateBridge.class))
@Column(name = "date_of_birth")
@JsonFormat(pattern = "dd/MM/yyyy")
private Date dateOfBirth;
我们将以下代码添加到我们的查询中,该代码旨在查找从 UI 传递的年龄范围内的所有人
public void withAgeRange(Integer lowerAge, Integer upperAge){
if(lowerAge != null || upperAge != null){
LocalDateTime localDateTime = LocalDateTime.now();
LocalDateTime lowerLocalDateTime = localDateTime.withYear(localDateTime.getYear() - upperAge);
LocalDateTime upperLocalDateTime = localDateTime.withYear(localDateTime.getYear() - lowerAge);
Date lowerDate = Date.from(lowerLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
Date upperDate = Date.from(upperLocalDateTime.atZone(ZoneId.systemDefault()).toInstant());
bool.must(getQueryBuilder().range().onField("datesOfBirth.dateOfBirth").from(lowerDate).to(upperDate).createQuery());
}
}
在执行前检查查询显示
+dateOfBirths.dateOfBirth:[33590595604 TO 1106595795604]
查看带有 Luke 的示例文档,有人的出生日期的索引值为 320976000000,位于范围的下限和上限之间。
来自 Kibana 的 Person 映射的相关部分
"datesOfBirth": {
"properties": {
"currentDateOfBirth": {
"type": "boolean"
},
"dateOfBirth": {
"type": "date",
"store": true
}
}
},
如前所述,我们所有其他范围查询都按预期工作。
我们希望至少返回这个人,但无论我们尝试什么范围,我们总是返回 0 个结果。
任何想法将不胜感激。
【问题讨论】:
-
你能为你的索引提供一个映射吗?
-
@КириллПолищук 我已经从索引映射中添加了相关部分。
标签: hibernate elasticsearch hibernate-search