Context search term highlighing not a trivial problem solved by simple regular expression matching, especially when trying to highlight phrases.

Here's why: swish-e creates a reverse index. It parses documents, extracts out "words" and creates an index. On a search, swish finds the words or phrases you are searching for, and lists the doucments where they are found.

I won't pretend that I understood everything in your explanation about how SWISH-E builds it's index, or how exactly you currently do highlighting, but I do have a general suggestion for simplifying your highlighting process:

As long as all you are doing is "prettying" up the output, then I don't think it's all that important that your highlighting logic EXACTLY match your indexing logic. Looping over each search $word and doing a s/$word/$beg_hl$word$end_hl/g may result in words being highlighted that didn't accutally contribute to the acctual relevance of the document -- but that isn't neccessarily a bad thing.

If I were in your shoes, I would focus on the fact that you are doing two very different things:

  1. Index documents using a specific criteria for what words are relevant to that document.
  2. Provide brief info for each document in a search list that lets people determine by eye if a document is useful to them.
In the case of that second one, you don't have to be exact -- just helpful.

In reply to Re: Context search term highlighting - Perl is too slow by hossman
in thread Context search term highlighting - Perl is too slow by moseley

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.