Viktor
Viktor

Reputation: 1487

lucene & hibernate & joining tables

We use Lucene with HibernateSearch and at the moment we query only the properties of one entity. What we want is to be able to query the properties of the relating entity.
For example:

USER

id
name
... group_id

GROUP

id
name
type

So the type/name of the group can be typed too and the users will be found that belong to the group.
I have found the following page but (http://blog.mikemccandless.com/2012/01/searching-relational-content-with.html) it doesn't use Hibernate and I read somewhere that it is not possible to join entities with Hibernate in Lucene.

Could you please tell me how could I achieve that in Lucene with Hibernate Search?

------------------------------- UPDATE -------------------------------
I forgot to mention that we don't use annotations but .hbm.xml files. Also we use the IndexedMapping class to add the properties of the entities to be indexed. E.g.
indexedMapping.property(field.getName(), ElementType.FIELD);

Upvotes: 0

Views: 978

Answers (1)

yrodiere
yrodiere

Reputation: 9977

If I understood correctly, you're looking for a way to index properties from the group entity along with those of the user entity.

Maybe @IndexedEmbedded is what you're looking for: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/?v=5.5#search-mapping-associated

You will have to annotate your group field:

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

    @Field
    private String name;

    @ManyToOne
    @IndexedEmbedded
    private Group group;
    ....
}

Then all the indexed fields of group will be added to the user document, prefixed by "group.". For instance "group.name" will match the name of the group of the user.


EDIT : if using the programmatic API, you'll have to call .indexedEmbedded() instead:

indexedMapping.property(field.getName(), ElementType.FIELD)
    .indexedEmbedded();

See https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/?v=5.5#_programmatically_defining_embedded_entities

Upvotes: 1

Related Questions