我似乎无法让非常基本的 FieldBridge
实现正常工作。看起来索引过程完全忽略了 @FieldBridge
注释。
这里是实现:
public class LocalisedInformationBridge implements FieldBridge {
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
luceneOptions.addFieldToDocument(name + ".test", "test", document);
}
}
带有@FieldBridge
注释的实体:
@OneToMany(mappedBy = "product")
@MapKey(name = "languageCode")
@IndexedEmbedded
@FieldBridge(impl = LocalisedInformationBridge.class)
private Map<String, LocalisedProductInformation> localisedProductInformation;
包含的实体:
@ManyToOne
@JoinColumn(name="productId")
@ContainedIn
private Product product;
当我尝试搜索 localizedProductInformation.test
字段时,出现异常:
org.hibernate.search.exception.SearchException: Unable to find field localisedProductInformation.test
以下是我对数据建立索引的方式:
FullTextEntityManager fullTextEntityManager =
Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.createIndexer().startAndWait();
奇怪的是,当我在 LocalizedInformationBridge
类的 set
方法上放置断点时,调试器不会停止程序的执行。我在这里遗漏了一些非常明显的东西吗?
最佳答案
需要知道的一件事是,当您在容器属性(数组、集合或映射)上使用 @IndexedEmbedded 时,@Field 注释中定义的任何字段桥都将应用于该属性值的元素。 因此,在您的情况下,字段桥将应用于 map 的值,而不是 map 本身。如果您的 map 为空,则根本不会应用现场桥。这是你的情况吗?
这种行为确实有点奇怪(考虑到 @IndexedEmbedded
的目的略有不同),但它已经被引入一段时间了,修复它会导致依赖它的用户回归。因此,在发布新的主要版本之前,这种情况可能会保持不变......
关于java - 无法通过自定义 FieldBridge 实现找到字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41747986/