1
votes

Good day.

I have a situation, similar to the one presented below.

@Entity
@Table(name="EntityDO")
public class EntityDO {
    @Id
    private Long id;

    @Column(nullable = false)
    private String name;

    @OneToMany(mappedBy = "parentEntity")
    private Set<EntityDO> ownedEntities;

    @ManyToOne
    @JoinColumn(name="PARENT_ID", nullable=true)
    private EntityDO parentEntity;

    ...
}

I need to index this entity via Hibernate Search in such way, that there is an ability to search for the entities matched not only by entity name, but also by matching some parent's name in the hierarchy.

My thinking was the following:

  1. I need to recursively index the 'name' member of the indexed entity + 'name' of the entity's parent + further etc. in such way that they came to the same lucene document's field.

  2. Then I will be able to create a simple query on one field to retrieve all needed entities.

In fact, I have a problem with point 1. @IndexedEmbedded doesn't suit here, due to circular relationship and the fact that empty prefixes are forbidden (I don't want the names of entities and their parents be in different fields).

Is it possible to do the entity indexing in custom way? Or maybe my thinking was initially wrong and there is completely another approach to resolve such issues?

1

1 Answers

1
votes

If you want to index the parent name also into the field name, you could write your own custom bridge - http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#d0e4426. The one thing you have to be careful about is that you detect any circular dependencies.

If you have a given max depth you could also just use @IndexedEmbedded and at query build time add query terms for fields up the configured max depth. Whether this solution is viable will depend on how deep you want to index. For jsut a couple of levels I would prefer this option over a custom bridge.