toastdriven/django-haystack — pull request #509 by orblivion (2012-02-14 02:51:31+00:00)
Title: Fixing rendering problem with highlighting overlapping terms, such as "what" and "a".

I found that searching for "what is a..." lead to the renderer to output "whatat" for what. It seemed to be tripping up the renderer that there were words within words. I thought at first to fix the renderer, but then I realized that it probably makes more sense to just make sure to feed it better highlight rules. Here's my thinking:

Get the earliest location of a matched term. If there are two starting within the same place (such as "hard" and "hardship") go with the longer one.

Potential problems with this are cases (which I can only come up with in theory) such as "anterritory" with "ant" and "territory" being search terms. Ideally, perhaps one would prefer "territory" be highlighted, since it's so much longer, but here "ant" will get highlighted. I thought about how to make "territory" highlighted, but I decided it was probably too complicated to be worth it, and not necessarily preferable anyway.

If, alternately, you want *everything* to be highlighted, (for instance, all of "anterritory" would be highlighted, despite it being a mashup of two terms, and not the term itself), you could go into the html rendering function and merge any of the overlapping highlight regions at the beginning of the function.