Quote:
I had considered to construct a list of all fields available on all models and then use MultiFieldQueryParser to search across all of them. But then I supposed that searching could take much longer if all indices needed to be searched than if I created an additional index, so I decided to try the "ALL" index approach first.
Yes I understand that's what common sense suggests, but in practice there is no additional cost, or if there's any I could never measure it.
Quote:
I will now try using a MultiFieldQueryParser with all fields instead and see how it works for me. Where do you propose should I get information about indexed entities/fields? Directly via the Lucene API or from Hibernate Search? If the later, how would I best access the current configuration? After a short look through the API documentation, I found org.hibernate.search.cfg.EntityDescriptor which seems to contain all required information.
no, EntityDescriptor is not populated by the framework; that's used in case
you want to define the mapping programmatically instead of using annotations; there's no code mapping the annotations to those value holders.
I didn't test it, but something like this should provide you all fieldnames defined in the index:
Code:
SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();
DirectoryProvider[] directoryProviders = searchFactory.getDirectoryProviders( YourEntityType.class );
IndexReader indexReader = searchFactory.getReaderProvider().openReader( directoryProviders );
Set<String> fieldNames = new HashSet<String>();
try {
TermEnum termEnum = indexReader.terms();
while ( termEnum.next() ) {
Term term = termEnum.term();
fieldNames.add( term.field() );
}
}
finally {
searchFactory.getReaderProvider().closeReader( indexReader );
}
It will iterate the full index, so you might want to cache and reuse the result, but it shouldn't be that bad as performing a query does something similar, so Lucene can perform this at interesting efficiency.
Of course it won't find fieldnames which you have never written to the index, but there's nothing to find there either ;)