Reputation: 61
I am using Lucene to search for text in a document. Can anyone tell me how write the code that highlights text in the document? I am using the code below but it's not working.
Analyzer analyzer = new SnowballAnalyzer("English");
File file = new File("/sdcard/index1/");
indexSearcher = new IndexSearcher(indexDir, true);
QueryParser parser = new QueryParser(Version.LUCENE_29,"DIG", analyzer);
Query query = parser.parse(mEdit.getText().toString());
ScoreDoc[] hits = indexSearcher.search(query, null, 1000).scoreDocs;
assertEquals(hits.length, "Description");
int itemCount = hits.length;
if (itemCount != 0) {
for (int i = 0; i < itemCount; i++) {
objSearchResult = new SearchResults();
Document hitDoc = indexSearcher.doc(hits[i].doc);
og.i("TestAndroidLuceneActivity", "Lucene: "
hitDoc.get("Description"));
I can display the text I am searching for, and display the document. But I can't get it to highlight matches in the document.
Upvotes: 2
Views: 1096
Reputation: 491
I don't think it works that way. Lucene is highlighting words in texts for search results.
This can help http://lucene.apache.org/java/2_9_1/api/contrib-highlighter/index.html
Upvotes: 1