简体   繁体   English

是否可以使用Hibernate Search 5.X对@Id字段使用数字编码

[英]Is it possible to use a numeric encoding for @Id fields with Hibernate Search 5.X

Before upgrading to Hibernate Search 5 from version 4.5, our system indexed all document ID's as numeric fields: 在从4.5版升级到Hibernate Search 5之前,我们的系统将所有文档ID索引为数字字段:

@Entity
public class Staff {
    @Id
    @NumericField
    protected Long id;
    // other fields
}

This allowed us to use numeric range queries. 这允许我们使用数值范围查询。 In Hibernate 5, all document IDs are indexed as strings, and the above annotation causes an exception. 在Hibernate 5中,所有文档ID都被索引为字符串,上面的注释会导致异常。 Without the annotation, all numeric range queries fail to properly search the ID fields. 如果没有注释,则所有数值范围查询都无法正确搜索ID字段。

Switching to TermRangeQuery instead of NumericRangeQuery will be tedious, and I'm hoping to avoid this. 切换到TermRangeQuery而不是NumericRangeQuery将是乏味的,我希望避免这种情况。

Is there a way to continue treating IDs as numeric values? 有没有办法继续将ID作为数值处理?

You're correct, you have found a bug which I'm fixing now as HSEARCH-1960 . 你是对的,你发现了一个我现在正在修理的错误,如HSEARCH-1960

You can workaround the problem by defining an additional field for your Id property like this: 您可以通过为Id属性定义其他字段来解决此问题,如下所示:

@Entity
public class Staff {
    @Id
    @Field(name="id_numeric")
    @NumericField(forField="id_numeric")
    protected Long id;
    // other fields
}

Then run your range queries on the field "id_numeric" rather than "id". 然后在字段“id_numeric”而不是“id”上运行范围查询。

In future versions of Hibernate Search we will probably encode all Ids as DocValues, as that is much more efficient. 在Hibernate Search的未来版本中,我们可能会将所有ID编码为DocValues,因为它更有效。 That implies that the above workaround will be the best way to also be able to encode it as a NumericField . 这意味着上述解决方法将是能够将其编码为NumericField的最佳方式。

I would not interfere with the way the id is indexed. 我不会干扰id索引的方式。 In the future this might not even be possible anymore. 在未来,这甚至可能不再可能。 Instead add an additional @Field . 而是添加一个额外的@Field You can give the field a dedicated name as well via the annotation. 您也可以通过注释为该字段指定专用名称。 Since the field is a Long it will be automatically numerically encoded. 由于该字段为Long因此将自动进行数字编码。

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

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