/** * Generates a lucene query to search for a given term in all the indexed fields of a class * * @param searchTerm the term to search for * @param searchedEntity the class searched * @param sess the hibernate session * @param defaultAnalyzer the default analyzer for parsing the search terms * @return * @throws ParseException */ public static Query generateQuery(String searchTerm, Class searchedEntity, Session sess, Analyzer defaultAnalyzer) throws ParseException { Query qry = null; if (searchTerm.equals("*")) { qry = new MatchAllDocsQuery(); } else { // Search in all indexed fields IndexReaderAccessor readerAccessor = null; IndexReader reader = null; try { FullTextSession txtSession = Search.getFullTextSession(sess); // obtain analyzer to parse the query: Analyzer analyzer; if (searchedEntity == null) { analyzer = defaultAnalyzer; } else { analyzer = txtSession.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 = txtSession.getSearchFactory(); readerAccessor = searchFactory.getIndexReaderAccessor(); reader = readerAccessor.open(searchedEntity); Collection<String> fieldNames = reader.getFieldNames(IndexReader.FieldOption.INDEXED); 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_35, queries, fnames, analyzer); } finally { if (readerAccessor != null && reader != null) { readerAccessor.close(reader); } } } return qry; }
@Override public IndexReaderAccessor getIndexReaderAccessor() { return this.searchFactory.getIndexReaderAccessor(); }
@Override public IndexReaderAccessor getIndexReaderAccessor() { return this.searchIntegrator.getIndexReaderAccessor(); }
/** * Provides access to the IndexReader API * * @return the IndexReaderAccessor for this SearchFactory */ IndexReaderAccessor getIndexReaderAccessor();