Hello guys :)
I am using hibernate-search version 5.3.0.Final. I use it for search value and sort result.
I have defined my entities like this:
Code:
@Entity
@Indexed
public class Film {
...
@Basic
@Fields( {
@Field,
@Field(name = "title_sort", analyze = Analyze.NO)
} )
@Column(name = "title")
public String getTitle() {
return title;
}
....
@ManyToOne
@IndexedEmbedded
@JoinTable(.......)
public Category getCategory() {
return category;
}
....
}
It's a basic use: entity with special sort field (title_sort) and manytoone attribute (category)
I want to detect invalid field when querying (with sort and search field):
Code:
Query jpaQuery = queryBuilder
.keyword()
.wildcard()
.onField("title")
.matching(word)
.createQuery());
.......
org.hibernate.search.jpa.FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(globalQuery, className);
.......
SortField sortField = new SortField("title_sort", type, reverseSort);
Sort sort = new Sort(sortField);
jpaQuery.setSort(sort);
.......
// execute query and return results
List entityList = jpaQuery.getResultList();
So in this case check search field "title" and also check sort field "title_sort"
I have two solutions:
A/ don't check field and catch exception NoSuchFieldException
B/ check field before querying
For solution A it works well for search field but for the sort the exception isn't fired
For solution B, it works but i was unable to check collection field (like categroy in the example):
Code:
HashMap<String,FieldDescriptor.Type> attributesMap = new HashMap<>();
// doesn't work for collection field like category.name (if categroy class has field name)
FieldDescriptor fieldDescriptor = searchFactory.getIndexedTypeDescriptor(className).getIndexedField(field);
if (fieldDescriptor != null){
attributesMap.put(cls.getName().toLowerCase() + '.' + field, fieldDescriptor.getType());
}
else
logger.info("empty for: "+field);
.....
if (attributesTypeMap.containsKey(sort or search field)){
// make query
....
}
What is the best solution for my problem ?
How handle it in case of A and/or B ?
Regards