if (searchTerm.equals("*")) {
qry = new MatchAllDocsQuery();
} else {
// Search in all indexed fields
IndexReaderAccessor readerAccessor = null;
IndexReader reader = null;
try {
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);
// obtain analyzer to parse the query:
Analyzer analyzer;
if (searchedEntity == null) {
analyzer = defaultAnalyzer;
} else {
analyzer = fullTextEntityManager.getSearchFactory().getAnalyzer(searchedEntity);
}
// search on all indexed fields: generate field list, removing internal hibernate search field name: _hibernate_class
// TODO: possible improvement: cache the fields of each entity
SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();
readerAccessor = searchFactory.getIndexReaderAccessor();
reader = readerAccessor.open(searchedEntity);
Collection<String> fieldNames = new HashSet<>();
for (FieldInfo fieldInfo : ReaderUtil.getMergedFieldInfos(reader)) {
if (fieldInfo.isIndexed) {
fieldNames.add(fieldInfo.name);
}
}
fieldNames.remove("_hibernate_class");
String[] fnames = new String[0];
fnames = fieldNames.toArray(fnames);
// To search on all fields, search the term in all fields
String[] queries = new String[fnames.length];
for (int i = 0; i < queries.length; ++i) {
queries[i] = searchTerm;
}
qry = MultiFieldQueryParser.parse(Version.LUCENE_36, queries, fnames, analyzer);
} finally {
if (readerAccessor != null && reader != null) {
readerAccessor.close(reader);
}
}
}
return qry;
}