<p>I've been using the (Java) <a href="http://lucene.apache.org/java/2_4_1/lucene-sandbox/index.html#Term%20Highlighter" rel="nofollow noreferrer">Highlighter for Lucene</a> (in the Sandbox package) for some time. However, this isn't really very accurate when it comes to matching the correct terms in search results - it works well for simple queries, for example searching for two separate words will highlight both code fragments in the results.</p>

<p>However, it doesn't act well with more complicated queries. In the simplest case, phrase queries such as "Stack Overflow" will match all occurrences of Stack or Overflow in the highlighting, which gives the impression to the user that it isn't working very well.</p>

<p>I tried applying the fix <a href="http://www.gossamer-threads.com/lists/lucene/java-user/15528" rel="nofollow noreferrer">here</a> but that came with a lot of performance caveats, and at the end of the day was just plain unusable. The performance is especially an issue on wildcard queries. This is due to the way that the highlighting works; instead of just working on the querystring and the text it parses it as Lucene would and then looks for all the matches that Lucene has made; unfortunately this means that for certain wildcard queries it can be looking for matches to 2000+ clauses on large documents, and it's simply not fast enough.</p>

<p>Is there any faster implementation of an accurate highlighter?</p>

[{'answer_id': 84993, 'author': 'Sindri Traustason', 'author_id': 1113, 'author_profile': 'https://Stackoverflow.com/users/1113', 'pm_score': 1, 'selected': False, 'text': '<p>You could look into using Solr. <a href="http://lucene.apache.org/solr" rel="nofollow noreferrer">http://lucene.apache.org/solr</a></p>\n\n<p>Solr is a sort of generic search application that uses Lucene and supports highlighting. It\'s possible that the highlighting in Solr is usable as an API outside of Solr.  You could also look at how Solr does it for inspiration.</p>\n'}, {'answer_id': 164764, 'author': 'dlamblin', 'author_id': 459, 'author_profile': 'https://Stackoverflow.com/users/459', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve been reading on the subject and came across <a href="http://lucene.apache.org/java/2_3_1/api/org/apache/lucene/search/spans/SpanQuery.html" rel="nofollow noreferrer">spanQuery</a> which would return to you the span of the matched term or terms in the field that matched.</p>\n'}, {'answer_id': 651662, 'author': 'pro', 'author_id': 352728, 'author_profile': 'https://Stackoverflow.com/users/352728', 'pm_score': 3, 'selected': True, 'text': '<p>There is a new faster highlighter (needs to be patched in but will be part of release 2.9)</p>\n\n<p><a href="https://issues.apache.org/jira/browse/LUCENE-1522" rel="nofollow noreferrer">https://issues.apache.org/jira/browse/LUCENE-1522</a></p>\n\n<p>and a <a href="https://issues.apache.org/jira/browse/LUCENE-1522?focusedCommentId=12688408&amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_12688408" rel="nofollow noreferrer">back-reference</a> to this question</p>\n'}]
['https://Stackoverflow.com/questions/82151', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6282/']