简体   繁体   English

Hibernate搜索索引嵌入式地图

[英]Hibernate Search index embedded map

I'm having some issues with indexing embedded text entities with Hibernate search. 我在使用Hibernate搜索为嵌入式文本实体建立索引时遇到一些问题。 Since entities extend other entities that I can't change, using annotations is not feasible. 由于实体扩展了我无法更改的其他实体,因此使用注释是不可行的。

Thus I do the mapping using the programmatic API. 因此,我使用程序化API进行了映射。 However, Hibernate search doesn't index the embedded text entities. 但是,Hibernate搜索不会索引嵌入的文本实体。

Here's a short example of what the entity model looks like (stripped down for simplicity): 这是实体模型的简短示例(为简单起见,将其简化):

@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;
}

And here's an excerpt of the mapping: 这是映射的摘录:

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();

The documentation isn't quite clear about using .indexEmbedded().entity(...) , but I have another embedded entity (a many-to-one association) which is only indexed using a similar mapping. 关于使用.indexEmbedded().entity(...)的文档尚不十分清楚,但是我还有另一个嵌入式实体(多对一关联),该实体仅使用类似的映射进行索引。

I suspect the texts are not mapped because of the map being used and Hibernate Search not being able to identify the property as a map. 我怀疑由于使用了地图并且Hibernate Search无法将属性标识为地图,因此未映射文本。 There is a MapBrigde as well as a BuildInMapBridge but they don't seem to be used when the mapping is built. 虽然有MapBrigdeBuildInMapBridge但是在构建映射时似乎没有使用它们。

What might I be missing or where might be the error? 我可能遗漏了什么或错误可能出在哪里?

Btw, I do this in a Hibernate Search 4.0.1 and Hibernate 4.0.1 environment. 顺便说一句,我是在Hibernate Search 4.0.1和Hibernate 4.0.1环境中进行的。

It seems like I have found the solution. 好像我找到了解决方案。 Since the documentation doesn't seem to be clear about it, I'll add it here for others to find. 由于文档似乎不清楚,因此我将其添加到此处供其他人查找。

The problem seems to be that the reference to the texts is a field of the super class Article and thus when SpecialArticle is being mapped, Hibernate Search seems to have difficulties. 问题似乎是对文本的引用是超类Article一个字段,因此在映射SpecialArticle时,Hibernate Search似乎有困难。

To make it work, the mapping had to be changed to include the super class as well: 为了使其工作,必须更改映射以包括超类:

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();

What is strange is that the problem also occurs with articleNumber but not with uid (maybe because of documentId() ). 奇怪的是, articleNumber也会出现此问题,而uid不会出现(可能是由于documentId() )。

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

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