Hello
I have searching based on HS and it works ok.
I see HS has projection of DOCUMENT_ID.
I've tried to use highlighting as described above.
I see in debug that StreamTokenizer gets data ok, but there is problem with score. I get nothing highlighted because scores in text fragments are always 0 (despite scores being displayed ok by projection).
Below is my code.
Code:
String searchPattern = "text*";
Analyzer = new StarndardAnalyzer();
QueryParser parser = new QueryParser(searchPattern, analyzer);
Query luceneQuery = parser.parse(searchPattern);
Session session = (Session) em.getDelegate();
FullTextSession fts = Search.createFullTextSession(session);
Transaction tx = fts.beginTransaction();
FullTextQuery query = fts.createFullTextQuery(luceneQuery, Person.class);
query.setProjection(FullTextQuery.THIS, FullTextQuery.DOCUMENT_ID,FullTextQuery.DOCUMENT,FullTextQuery.SCORE,FullTextQuery.BOOST);
Collection<Object[]> lista = query.list();
SearchFactory searchFactory = fts.getSearchFactory();
String fragmentSeparator = "...";
Fragmenter fragmenter = new SimpleFragmenter();
int numOfFragments = 5;
ReaderProvider readerProvider = searchFactory.getReaderProvider();
DirectoryProvider directoryProviders = searchFactory.getDirectoryProviders(Person.class)[0];
IndexReader reader = readerProvider.openReader(directoryProvider);
luceneQuery.rewrite(reader);
Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter(), new QueryScorer(luceneQuery));
highlighter.setTextFragmenter(fragmenter);
for (Object[] zlozony : lista) {
int docId = Integer.parseInt(zlozony[1].toString());
Document document = (Document) zlozony[2];
TokenStream tokenStream = TokenSources.getAnyTokenStream( reader, docId, "name", analyzer);
org.apache.lucene.document.Field field = document.getField("name");
String highlight = highlighter.getBestFragments(tokenStream, field.stringValue(), numOfFragments,fragmentSeparator);
}
readerProvider.closeReader(reader);
tx.commit();
fts.close(); //session.close();