如果字段不在数据库中,如何在 Hibernate 搜索中索引布尔值 @Field table

How to index a boolean @Field in Hibernate Search if the field is not in the db table

我在 Hibernate Search 中索引 boolean @field 时遇到问题,问题是当对象更改时,其余字段也会更改,只有 boolean 字段保持对象的旧状态。

@JsonIgnore
@Field(name = "isWarning", index = Index.YES)
@SortableField(forField = "isWarning")
private boolean isWarning() {
   //some logic
}

解决这个问题的正确方法是什么?

我假设你提到的这个 "logic" 访问其他实体。您需要告诉 Hibernate Search 这些实体包含在具有 isWarning 方法的实体中。

假设 isWarning 方法在名为 MainEntity 的实体中定义,它从另一个名为 SomeOtherEntity.

的实体访问数据

SomeOtherEntity中,你会有反面联想:

public class SomeOtherEntity {
  @ManyToOne // Or @OneToOne, or whatever
  private MainEntity mainEntity;
}

只需添加@ContainedIn 就可以了:

public class SomeOtherEntity {
  @ManyToOne // Or @OneToOne, or whatever
  @ContainedIn
  private MainEntity mainEntity;
}

请注意,不幸的是,如果 SomeOtherEntity 经常更新,这会对性能产生重大影响:Hibernate Search 将不会准确地知道 哪个 部分SomeOtherEntity 用于 MainEntity,因此每次 SomeOtherEntity 更改时都会重新索引 MainEntity,即使 SomeOtherEntity 中的更改不影响结果isWarning。一个 ticket has been filed to address this issue,但仍在等待中。