s.grinovero wrote:
You can use the QueryBuilder targeting the subclass, and then still target the Query to the superclass to include types which don't have an appellation field.
In my case, I don't want to build the QueryBuilder targeting the subclass, because my app works on the basis of a core ItemEntity class, with any number of possible child Entity classes for each customised environment I deploy to. The frontend passes to the backend all fields to search on, and my generic search utils build the Queries required to search on those fields. The 'appellation' field name is passed into my generic search code from a customised frontend. The core code only knows about the parent ItemEntity class, and not about the child WineEntity which has the appellation field. (the WineEntity instances themselves are created and indexed via another set of custom code).
So, I wanted to always build a QueryBuilder for my parent class, but be able to specify onField() clauses for fields that are found on child classes.
I know this goes against standard OO practise.. but in the world of Lucene indexes, the index will contain fields for both parent and child class, i.e.:
_hibernate_class: my.app.WineEntity
coreItemField: 1000
appellation: foobar
(in case you're wondering, yes, this is a completely different problem to the @IndexEmbedded JIRA I raised recently)
I see via Luke that the _hibernate_class indicates what Entity class should be used to instantiate this object.. to be honest, I don't yet understand how to think about the mapping of Java Entity classes to Lucene index Documents.
s.grinovero wrote:
I don't think this usage was expected, but please help us figuring out how it would be more easy to use. Would you expect the QueryBuilder to know all fields of subtypes? What would you expect from:
Code:
searchFactory.buildQueryBuilder().forEntity( Object.class )
?
Good question. QueryBuilder should only know to act on @Indexed classes, and hopefully the job of inspecting them and all subtypes is fairly straightforward, if not already done at SessionFactory instantiation time.
I did hope that QueryBuilder would know all fields of subtypes. But, perhaps QueryBuilder is doing The Right Thing, and I should approach the problem differently. In OO terms, it doesn't make sense to have a reference of the type of the parent class and expect it to know about members in derived classes; the inheritance system works the other way, a reference to a child class would naturally know about members of parent classes/interfaces. I don't know if it is a good idea to build this kind of introspection into HSearch.
For now, I work around by catching a SearchException when trying to specify onField for every field specified, and for those that fail, I build a plain Lucene TermQuery, knowing that Lucene's index will contain the field, even if HSearch cannot figure out what I'm talking about. Perhaps I will run into trouble with the analyzers and tokenizers used on the field.. I haven't read over the docs enough times for that complex info to sink in yet :)
Nick