Hibernate Search IndexedEmbedded Out Of Memory

377 views Asked by At

I have two entities (entityA and entityB), one which contain another with @IndexedEmbedded, because when I search, I want to query by entityB as well, and return entityA.

The relationship (simplified) is as such:

public class EntityA {
    private String name;

    @OneToMany
    @IndexedEmbedded
    private List<EntityB> children;
}

public class EntityB {
    @ManyToOne
    @ContainedIn
    private EntityA parent;

    private String childName;
}

I am having issues now because relationally, the "children" in entityA can have up to 100k items. This causes OutOfMemory issue no matter if I am using FullTextSession.index or the MassIndexer.

I could actually remove the @OneToMany mapping in EntityA because when I want to access EntityB, I will usually do a query with some filtering and pagination, but if I remove the @OneToMany, then Hibernate Search will not index my EntityB.

Is there anyway to get the FullTextSession.index to perform indexing based on batch on the "children"?

2

There are 2 answers

2
Emmanuel Bernard On

Depending on the queries you want to effectively execute, you might be able to reverse the indexing. Instead of parking EntityA as @Indexed, you mark EntityB as @Indexed and mark its parent association with @IndexedEmbedded.

That way, you won't suffer from having to load the 100k elements.

On the query side, you will need to reverse your query as well to something like childName:Emmanuel AND parent.name:Hardy What you will receive is EntityB instances but you can reach EntityA by simple navigation, or by using a ResultTransformer.

Note that if your link between EntityB and EntityA is lazy, you can ask Hibernate Search to adjust its fetching strategy:

Criteria criteria = s.createCriteria(EntityB.class).setFetchMode("parent", FetchMode.JOIN); s.createFullTextQuery(luceneQuery).setCriteriaQuery(criteria);

1
Subhash On

I feel you should remove that relationship like one to many and many to one and write a join query to fetch the results of desired size, fetching 100k rows kills the mem.

Thnx, Subhash