【问题标题】:How to map lucene documentid to Hibernate search entity field如何将 lucene documentid 映射到 Hibernate 搜索实体字段
【发布时间】:2014-09-11 06:28:37
【问题描述】:

我有一个带有 documentid 的实体

@Indexed
@Analyzer(impl = EnglishAnalyzer.class)
@Entity
@Table(name = "Tag", schema = "cpsc")
public class Tag extends BaseObject {

public static final String NAME_FIELD = "name";

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "uuidGenerator")
@GenericGenerator(name = "uuidGenerator", strategy = "mypackage.UuidGenerator")
@Column(name = ID_FIELD)
@DocumentId
private Long id;

...

我的生成器将创建一个唯一的 Long id 并在保存后将其存储到数据库中。

但是如何在 lucene 中获取 documentid 或 set 是否与我的实体 id 相同? 获取带有 lucene 文档 id 的字段会非常有用。

我需要它的主要原因是从实体的索引中获取术语,因为我需要 lucene id。 也许还有另一种获取术语休眠搜索方式的方法?

感谢您的建议。

【问题讨论】:

    标签: java lucene hibernate-search


    【解决方案1】:

    这里有几件事。首先,您不必指定@Id@DocumentId。当您未指定 @DocumentId 时,JPA id 会自动作为文档 id。如果您想明确使用不同的字段作为 Lucene 文档 ID,那么使用 @DocumentId 真的才有意义。

    回答您的问题-“但是我如何在 lucene 或 set 中获取 documentid 与我的实体 ID 相同”-它们已经相同。并且已经有一个文档 id 字段,例如,您可以通过投影检索该字段。要使用的投影常数是FullTextQuery.DOCUMENT_ID。另见http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#projections

    【讨论】:

    • 为了补充出色的答案,我们不要将 Hibernate Search 用于识别与存储在数据库中的实体的关系的“文档 ID”与 Lucene 内部使用的“docId”混淆。 Lucene docId 只是相对于段(或完整索引)的序数,并且可能会在写入后发生变化。例如,特定命中可能是索引中的第五个条目,然后在插入另一个文档时成为第六个条目。使用 Projection 允许您读取此值,但请记住读取的值将发生变化。使用 FullTextQuery.ID 获得稳定的参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 2021-07-23
    • 2020-10-24
    • 1970-01-01
    • 2019-08-10
    • 2021-03-05
    • 2013-07-18
    相关资源
    最近更新 更多