synapse/storage/search.py
def _find_highlights_in_postgres(self, search_query, events):
        """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 f(txn):
            highlight_words = set()
            for event in events:
                # As a hack we simply join values of all possible keys. This is
                # fine since we're only using them to find possible highlights.
                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)

                # We need to find some values for StartSel and StopSel that
                # aren't in the value so that we can pick results out.
                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]

                # Now we need to pick the possible highlights out of the haedline
                # result.
                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)
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.
        
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.

search_query (str)
events (list): A list of events

deferred : A set of strings.
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 . search_query ( str ) events ( list ) : A list of events 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)
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 )
Given a list of events and a search term, return a list of words
that match from the content of the event.
Given a list of events and a search term return a list of words that match from the content of the event .
"""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.
        """ # As a hack we simply join values of all possible keys. This is # fine since we're only using them to find possible highlights. # We need to find some values for StartSel and StopSel that # aren't in the value so that we can pick results out. # Now we need to pick the possible highlights out of the haedline # result.
self
search_query
events