Hibernate 搜索@IndexedEmbedded

发布于 2024-12-05 00:07:34 字数 1028 浏览 2 评论 0原文

我有类似的情况 这个

@Entity
@Indexed
public class Place {
    @Id
    @GeneratedValue
    @DocumentId
    private Long id;

    @Field( index = Index.TOKENIZED )
    private String name;

    @OneToOne( cascade = { CascadeType.PERSIST, CascadeType.REMOVE } )
    @IndexedEmbedded
    private Address address;
    ....
}

@Entity
public class Address {
    @Id
    @GeneratedValue
    private Long id;

    @Field(index=Index.TOKENIZED)
    private String street;

    @Field(index=Index.TOKENIZED)
    private String city;

    @ContainedIn
    @OneToMany(mappedBy="address")
    private Set<Place> places;
    ...
}

现在的问题如下: 例如,如果我更改实体 Place 中的 name 字段,哪些实体将被重新索引?

1) 只有名称字段?

2) 整个Place实体?

3) 整个Place实体和用@IndexedEmbedded注释的实体?

我需要的就是第三个。那么如果它不是标准的,是否有任何解决方案可以实现这种行为?

I have a similar situation like this one

@Entity
@Indexed
public class Place {
    @Id
    @GeneratedValue
    @DocumentId
    private Long id;

    @Field( index = Index.TOKENIZED )
    private String name;

    @OneToOne( cascade = { CascadeType.PERSIST, CascadeType.REMOVE } )
    @IndexedEmbedded
    private Address address;
    ....
}

@Entity
public class Address {
    @Id
    @GeneratedValue
    private Long id;

    @Field(index=Index.TOKENIZED)
    private String street;

    @Field(index=Index.TOKENIZED)
    private String city;

    @ContainedIn
    @OneToMany(mappedBy="address")
    private Set<Place> places;
    ...
}

The problem now is the following:
If I change for example the name field in entity Place which entities are going to be re-indexed?

1) Only the name field?

2) The whole Place entity?

3) The whole Place entity and the entities annotated with @IndexedEmbedded?

The one I need for my purpose would be the third. So if it is not standard, could there be any solution to achieve this behaviour?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凡尘雨 2024-12-12 00:07:34

幸运的是,这确实是第三个,所以我很幸运,它按预期工作

Fortunately it's really the third, so I was lucky and it works as expected

维持三分热 2024-12-12 00:07:34

您可以使用以下代码手动重新索引 Place 实体

public void updateIndex(T entity){
        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
        fullTextEntityManager.index(entity);
        fullTextEntityManager.getSearchFactory().optimize(entity.getClass());
    }

其次,如果您使用 hibernate,您可以在 persistence.xml 中配置 lucene 以自动更新已更改实体的索引

You can use the following code to re-index the Place Entity manually

public void updateIndex(T entity){
        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
        fullTextEntityManager.index(entity);
        fullTextEntityManager.getSearchFactory().optimize(entity.getClass());
    }

Secondly if you are using hibernate you can configure lucene in persistence.xml to automatically update the indexes of the entities that are changed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文