I am having to do a programmatic configuration of the fields to be indexed with Hibernate Search.
In the scenario below the use of indexEmbedded() is resulting in a "field not found error".
@Entity
public class AT {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ARRM_IDE", nullable = false)
private A arr;
private Date dateType;
(and other fields)
}
@Entity
public class A {
@Id
@SequenceGenerator(name = "C_SEQUENCE", sequenceName = "S_ARRM_01")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "C_SEQUENCE")
@Column(name = "IDE_ARR")
private Long id;
}
SearchMapping mapping = new SearchMapping();
mapping.entity(AT.class).indexed()
.property("dateType", ElementType.FIELD)
.field()
.store(Store.YES)
.property("arr", ElementType.FIELD)
.indexEmbedded()
.entity(A.class).indexed()
.property("id", ElementType.FIELD).documentId().name("arrId")
.field()
.store(Store.YES)
;
When I create and persist entities (I have integrated Hibernate Search with Elasticsearch), the entities are created and the indexes are created in Elasticsearch also.
contents on Elasticsearch:
"_index" : "com.....at",
"_type" : "com.....AT",
"_id" : "7744",
"_score" : 1.0,
"_source" : {
"dateType" : "2016-06-12T06:08:52.780Z",
"arr" : {
"id" : 6352
}
}
} ]
But when I try querying using the Hibernate Search Lucene query it fails:
FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(AT.class).get();
org.apache.lucene.search.Query luceneQuery = qb.bool()
.must(qb
.range()
.onField("dateType")
.from(parseDate(startDate))
.to(parseDate(endDate)).excludeLimit()
.createQuery())
.must(qb
.keyword()
.onField("arr")
.matching(crsArrId).createQuery())
.createQuery();
Sort sort = null;
if (order == OrderEnum.ASCENDING) {
sort = new Sort(
new SortField("dateType", SortField.Type.STRING));
} else {
sort = new Sort(
new SortField("dateType", SortField.Type.STRING, true));
}
FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, AT.class);
jpaQuery.setSort(sort);
jpaQuery.setFirstResult(offset);
jpaQuery.setMaxResults(maxReturnedEvents);
return jpaQuery.getResultList();
Error is:
org.hibernate.search.exception.SearchException: Unable to find field arr in com....AT
at org.hibernate.search.engine.spi.DocumentBuilderIndexedEntity.objectToString(DocumentBuilderIndexedEntity.java:977)
at org.hibernate.search.query.dsl.impl.FieldContext.objectToString(FieldContext.java:75)
at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.buildSearchTerm(ConnectedMultiFieldsTermQueryBuilder.java:145)
at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:105)
at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:67)
Thanks for your help!
You want to search on arr.id, not arr.
Just change .onField("arr") to .onField("arr.id").