【问题标题】:Hibernate Search index embedded mapHibernate Search 索引嵌入图
【发布时间】:2016-03-15 15:37:27
【问题描述】:

我在使用 Hibernate 搜索为嵌入式文本实体编制索引时遇到了一些问题。 由于实体扩展了我无法更改的其他实体,因此使用注释是不可行的。

因此,我使用编程 API 进行映射。但是,Hibernate 搜索不会索引嵌入的文本实体。

以下是实体模型外观的简短示例(为简单起见,已精简):

@Entity
class Article {
  @Id
  private long uid; 

  private String articleNumber;

  @OneToMany ( mappedBy = "article" )
  @MapKey( name = "languageCode" )
  private Map<String, ArticleText> texts;
  ...
}

@Entity
class ArticleText {
  @ManyToOne
  private ArticleEntity article;      

  private String languageCode;
  private String someText;
  ...
}

@Entity
class SpecialArticle extends Article {
  private String someSpecialAttribute;
}

这是映射的摘录:

SearchMapping mapping = ...;
mapping.entity( SpecialArticle.class )
 .indexed()
   .property( "uid", ElementType.FIELD ).documentId()
   .property( "articleNumber", ElementType.FIELD ).field()
   .property( "someSpecialAttribute", ElementType.FIELD ).field()
   .property( "texts", ElementType.FIELD )
     .indexEmbedded().targetElement( ArticleText.class ).entity( ArticleText.class )        
       .property( "article", ElementType.FIELD ).containedIn()
       .property( "someText", ElementType.FIELD ).field();

文档对使用 .indexEmbedded().entity(...) 不是很清楚,但我有另一个嵌入式实体(多对一关联),它只使用类似的映射进行索引。

我怀疑文本没有被映射,因为正在使用地图并且 Hibernate Search 无法将该属性识别为地图。有一个MapBrigde 和一个BuildInMapBridge,但是在构建映射时似乎没有使用它们。

我可能遗漏了什么或哪里可能出现错误?

顺便说一句,我在 Hibernate Search 4.0.1 和 Hibernate 4.0.1 环境中执行此操作。

【问题讨论】:

    标签: java hibernate-search


    【解决方案1】:

    看来我已经找到了解决办法。由于文档似乎没有说清楚,所以我将其添加到这里以供其他人查找。

    问题似乎是对文本的引用是超类Article 的一个字段,因此在映射SpecialArticle 时,Hibernate Search 似乎有困难。

    为了使其工作,必须更改映射以包含超类:

    SearchMapping mapping = ...;
    mapping.entity( SpecialArticle.class )
     .indexed()
       .property( "uid", ElementType.FIELD ).documentId()   
       .property( "someSpecialAttribute", ElementType.FIELD ).field();
    
    //Map the super class directly, but don't call "indexed()"
    mapping.entity( Article.class )
     .property( "articleNumber", ElementType.FIELD ).field()
       .property( "texts", ElementType.FIELD )
         .indexEmbedded().targetElement( ArticleText.class ).entity( ArticleText.class )        
           .property( "article", ElementType.FIELD ).containedIn()
           .property( "someText", ElementType.FIELD ).field();
    

    奇怪的是articleNumber也会出现问题,但uid不会出现问题(可能是因为documentId())。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2016-10-22
      • 2018-07-23
      相关资源
      最近更新 更多