Lucene Hibernate search@IndexEmbedded将集合合并到一个字段中

Lucene Hibernate search@IndexEmbedded将集合合并到一个字段中,lucene,full-text-search,hibernate-search,Lucene,Full Text Search,Hibernate Search,我有如下映射: @Entity @Table(name = "t_documents") @Indexed(interceptor = DocumentIndexingInterceptor.class) public class Document implements Serializable { @Id @GeneratedValue @DocumentId private Long id; @IndexedEmbedded(prefix = "se

我有如下映射:

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

//...
}
answer your own question post your question
当我搜索包含“回复帖子”一句的文档时,它会找到这个特定的文档。这不是我想要的结果。我希望只有在单个正文中找到我搜索的字符串时才能找到此文档


这可能吗?如果没有,是否有办法实现我想要的。

这是
@IndexedEmbedded
对集合的固有限制:集合的所有元素都合并在一起

要问自己的第一个问题是:你真的在搜索文档吗?如果您不希望不同部分中的两个单词匹配,则可能需要搜索部分。 如果是这样,您只需将查询更改为target
Section.class
,而不是
Document.class


如果您还需要能够搜索
文档中的字段
,那么您可以通过另一种方式进行嵌入:在
部分放置一个
@IndexedEmbedded
。文档
,如下所示:

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

//...
}
@实体
@表(name=“t_文件”)
@索引(拦截器=DocumentIndexingInterceptor.class)
公共类文档实现可序列化{
@身份证
@生成值
@文档ID
私人长id;
@ContainedIn//IMPORTANT:这将确保在更新节时更新“节”索引
私人名单部分;
//...
}
@实体
@表(name=“t_节”)
@索引//TODO:如果您需要它,请在此处放置一个拦截器,就像在文档中一样?
公共类部分实现了可序列化{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@文档ID
私人长id;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“ndocumentid”)
@IndexedEmbedded(indexNullAs=“null”,includePaths={/*在此处列出您的路径*/})
私人文件;
@字段(name=“body”,
analyze=analyze.YES,
index=index.YES,
store=store.NO,
标准=标准。否)
@列(name=“vbody”)
私有字符串体;
//...
}

文档是我的主要实体。它包含许多字段,如标题、状态、作者、日期等。所有这些字段都被索引和搜索。因此,我希望得到类似于
get me文档的查询,其中标题等于“some title”,任何部分都包含单词“some words”
@JohnSmith Ok。如果您不想显式列出包含的路径,您可以忽略它们:所有字段都将包含在内。如果您查询的是节索引而不是文档索引,那么您给出的查询将转换为
document.title:“some title”body:“some words”