Hello,
Can anyone help me with an example of using PostingsHighlighter in
Lucene 4.7. I am trying to modify SearchFiles.java (which can be found in
the demo directory) so that it would display not only which documents
contains the terms I was searching for, but also the entire sentences where
it found those terms. For example, if I am looking for "brown dog" and
document 1 contains a sentence "I have a brown dog", I'd like to see
something like this:
doc = 1, score = ####, matching sentence = "I have a brown dog"
so I have:
IndexReader reader = DirectoryReader.open(FSDirectory.open(new
File(index)));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_47,emptyset);
AnalyzingQueryParser parser = new AnalyzingQueryParser(Version.LUCENE_47,
field, analyzer);
Query query = parser.parse("brown dog");
PostingsHighlighter highlighter = new PostingsHighlighter();
TopDocs results = searcher.search(query, 5 * hitsPerPage);
String highlights[] = highlighter.highlight("contents", query, searcher,
results);
for (int k = 0; k< highlights.length; k++) {
System.out.println("highlights[ " + k + "] is " + highlights[k]);
}
I get the documents and the scores but the highlights[#] elements are
always null. What am I doing wrong?
Thanks!
Natalia