2017-06-16 30 views
0

我有映射它看起来像:Hibernate Search的@IndexEmbedded组合集合中的一个领域

@Entity 
@Table(name = "t_documents") 
@Indexed(interceptor = DocumentIndexingInterceptor.class) 
public class Document implements Serializable { 
    @Id 
    @GeneratedValue 
    @DocumentId 
    private Long id; 

    @IndexedEmbedded(prefix = "sections.", indexNullAs = "null", 
     includePaths = {"body"}) 
    private List<Section> sections; 

//... 
} 

@Entity 
@Table(name = "t_sections") 
@Indexed 
public class Section implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @DocumentId 
    private Long id; 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "ndocumentid") 
    private Document document; 

    @Field(name = "body", 
      analyze = Analyze.YES, 
      index = Index.YES, 
      store = Store.NO, 
      norms = Norms.NO) 
    @Column(name = "vbody") 
    private String body; 

//... 
} 

现在,如果我有两个实体部分的实体文件看起来像:
第1分体:“回答你自己题”;
第2节正文:“发布您的问题”;
*其他值并不重要。

当我看着卢克索引实体文档sections.body样子:

answer your own question post your question 

当我搜索包含一句“回答后”的文件,将发现这个特定的文档。这不是我想要的结果。我希望只有在单个主体中找到搜索到的字符串时才能找到此文档。

这甚至可能吗?如果没有,有什么办法可以达到我想要的。

回答

0

这是集合上@IndexedEmbedded的内在限制:集合中的所有元素都合并在一起。

问问自己的第一个问题是:你真的在搜索文档吗?如果你不想让不同部分的两个单词匹配,那么很可能你想搜索部分。 如果是这种情况,您可以简单地将您的查询更改为Section.class而不是Document.class


如果你需要能够在Document搜索领域也一样,那么你可以做一轮嵌入另一种方式:把一个@IndexedEmbeddedSection.document,像这样:

@Entity 
@Table(name = "t_documents") 
@Indexed(interceptor = DocumentIndexingInterceptor.class) 
public class Document implements Serializable { 
    @Id 
    @GeneratedValue 
    @DocumentId 
    private Long id; 

    @ContainedIn // IMPORTANT: this will ensure that the "section" index is updated whenever a section is updated 
    private List<Section> sections; 

//... 
} 

@Entity 
@Table(name = "t_sections") 
@Indexed // TODO: if you need it, put an interceptor here, just as in Document? 
public class Section implements Serializable { 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @DocumentId 
    private Long id; 
    @ManyToOne(fetch = FetchType.LAZY) 
    @JoinColumn(name = "ndocumentid") 
    @IndexedEmbedded(indexNullAs = "null", includePaths = { /* LIST YOUR PATHS HERE */ }) 
    private Document document; 

    @Field(name = "body", 
      analyze = Analyze.YES, 
      index = Index.YES, 
      store = Store.NO, 
      norms = Norms.NO) 
    @Column(name = "vbody") 
    private String body; 

//... 
} 
+0

文件我的主要实体。它包含很多字段,如标题,状态,作者,日期等,所有这些都被索引和搜索。 作为结果我想要得到的查询,如'让我的文件,其中标题等于“某些标题”,任何部分包含单词“某些单词”' –

+0

@JohnSmith好的。如果您不想明确列出包含的路径,则可以忽略它们:将包括所有字段。然后,如果您查询段索引而不是文档索引,则您提供的查询将翻译为“document.title:”某个标题“body:”某些词语“。 –