Hi, Everyone,
I begin to use Hibernate Search these days and it's really a great tool. And I have some questions for the usage of it:
I have two indexed entities in my project: Clazz and Registrations. A Registration must belong to one Clazz. And a Clazz contains zero or more Registrations. And this relationship also mapped in hibernate search:
Code:
@Entity
@Indexed
public class Clazz {
@Id
@DocumentId
@GeneratedValue(generator = "hibernate-uuid.hex")
@GenericGenerator(name = "hibernate-uuid.hex", strategy = "uuid.hex")
private String id;
@Field(index = Index.TOKENIZED, store = Store.YES)
private String clazzName;
@OneToMany(mappedBy = "clazz", fetch = FetchType.EAGER)
@OrderBy(clause = "username asc")
@ContainedIn
private Set<Registration> registrations = new HashSet<Registration>();
...
and
Code:
@Entity
@Indexed
public class Registration {
@Id
@DocumentId
@GeneratedValue(generator = "hibernate-uuid.hex")
@GenericGenerator(name = "hibernate-uuid.hex", strategy = "uuid.hex")
private String id;
...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "clazz_id", nullable = false)
@IndexedEmbedded(prefix="inClazz_")
private Clazz clazz;
...
and the embedded object can be successfully searched by "+inClazz_clazzName:xxx "
It's perfect and nothing wrong. But what makes me confused is, that I'm using lucene's MultiFieldQueryParser to do the search:
Code:
@Transactional(readOnly = true)
public List search(final String keyword) {
return getJpaTemplate().executeFind(new JpaCallback() {
public Object doInJpa(EntityManager entityManager) {
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search
.createFullTextEntityManager(entityManager);
MultiFieldQueryParser parser = new MultiFieldQueryParser(
new String[] { "id", "username" },
new StandardAnalyzer());
org.apache.lucene.search.Query query;
try {
query = parser.parse(keyword);
return fullTextEntityManager.createFullTextQuery(query,
Registration.class).getResultList();
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
});
}
please note that I just assign two fields for search in MultiFieldQueryParser: "id" and "username" and there's no "clazz" in it.
But why the search like 'inClazz_clazzName" still works? I've also checked the doc of Lucene but don't have much idea on it.
I'm a newbie to Lucene and Hibernate Search. I'm not sure whether is proper to post this question here or maybe in Lucene forum. Can anyone help me? Thanks!