0

From the Whoosh documentation I can get matched search terms with some context with:

results = mysearcher.search(myquery)
for hit in results:
    print(hit["title"])
    # Assume "content" field is stored
    print(hit.highlights("content"))

I'd like to access the "highlights" as a list of separated items (so that I can enumerate them in a html list) but the output of hit.highlights() appears to be of type <class 'str'>, and it's not clear to me that there's a unique delimiter.

Is there a way I can get a list instead of everything concatenated into one string?

Alexander Soare
  • 2,825
  • 3
  • 25
  • 53
  • 1
    I believe you need to change the highlighter formatter. See the paragraph starting "To change the fragmeter, formatter, order, or scorer used in highlighting ..." just below this place in the [Whoosh documentation](https://whoosh.readthedocs.io/en/latest/api/searching.html#whoosh.searching.Hit.highlights). – pjcunningham Jul 23 '20 at 10:04

1 Answers1

1

You could just convert the highlighted string with the separator "..." to a list.

It's as simple as this:

highlights_list = hit.highlights("content").split("...")
Samy
  • 629
  • 8
  • 22