'Given a list of events and a search term, return a list of words
that match from the content of the event.
This is used to give a list of words that clients can match against to
highlight the matching parts.
Args:
search_query (str)
events (list): A list of events
Returns:
deferred : A set of strings.'

def _find_highlights_in_postgres(self, search_query, events):

    def f(txn):
        highlight_words = set()
        for event in events:
            values = []
            for key in ('body', 'name', 'topic'):
                v = event.content.get(key, None)
                if v:
                    values.append(v)
            if (not values):
                continue
            value = '    '.join(values)
            start_sel = '<'
            stop_sel = '>'
            while (start_sel in value):
                start_sel += '<'
            while (stop_sel in value):
                stop_sel += '>'
            query = ("SELECT    ts_headline(?,    to_tsquery('english',    ?),    %s)" % _to_postgres_options({'StartSel': start_sel, 'StopSel': stop_sel, 'MaxFragments': '50'}))
            txn.execute(query, (value, search_query))
            (headline,) = txn.fetchall()[0]
            matcher_regex = ('%s(.*?)%s' % (re.escape(start_sel), re.escape(stop_sel)))
            res = re.findall(matcher_regex, headline)
            highlight_words.update([r.lower() for r in res])
        return highlight_words
    return self.runInteraction('_find_highlights', f)
