'Searches for users in directory
Returns:
dict of the form::
"limited": <bool>,  # whether there were more results or not
"results": [  # Ordered by best match first
"user_id": <user_id>,
"display_name": <display_name>,
"avatar_url": <avatar_url>'

@defer.inlineCallbacks
def search_user_dir(self, user_id, search_term, limit):

    if isinstance(self.database_engine, PostgresEngine):
        (full_query, exact_query, prefix_query) = _parse_query_postgres(search_term)
        sql = "\n                                                                SELECT    d.user_id,    display_name,    avatar_url\n                                                                FROM    user_directory_search\n                                                                INNER    JOIN    user_directory    AS    d    USING    (user_id)\n                                                                LEFT    JOIN    users_in_pubic_room    AS    p    USING    (user_id)\n                                                                LEFT    JOIN    (\n                                                                                SELECT    other_user_id    AS    user_id    FROM    users_who_share_rooms\n                                                                                WHERE    user_id    =    ?    AND    share_private\n                                                                )    AS    s    USING    (user_id)\n                                                                WHERE\n                                                                                (s.user_id    IS    NOT    NULL    OR    p.user_id    IS    NOT    NULL)\n                                                                                AND    vector    @@    to_tsquery('english',    ?)\n                                                                ORDER    BY\n                                                                                (CASE    WHEN    s.user_id    IS    NOT    NULL    THEN    4.0    ELSE    1.0    END)\n                                                                                *    (CASE    WHEN    display_name    IS    NOT    NULL    THEN    1.2    ELSE    1.0    END)\n                                                                                *    (CASE    WHEN    avatar_url    IS    NOT    NULL    THEN    1.2    ELSE    1.0    END)\n                                                                                *    (\n                                                                                                3    *    ts_rank_cd(\n                                                                                                                '{0.1,    0.1,    0.9,    1.0}',\n                                                                                                                vector,\n                                                                                                                to_tsquery('english',    ?),\n                                                                                                                8\n                                                                                                )\n                                                                                                +    ts_rank_cd(\n                                                                                                                '{0.1,    0.1,    0.9,    1.0}',\n                                                                                                                vector,\n                                                                                                                to_tsquery('english',    ?),\n                                                                                                                8\n                                                                                                )\n                                                                                )\n                                                                                DESC,\n                                                                                display_name    IS    NULL,\n                                                                                avatar_url    IS    NULL\n                                                                LIMIT    ?\n                                                "
        args = (user_id, full_query, exact_query, prefix_query, (limit + 1))
    elif isinstance(self.database_engine, Sqlite3Engine):
        search_query = _parse_query_sqlite(search_term)
        sql = '\n                                                                SELECT    d.user_id,    display_name,    avatar_url\n                                                                FROM    user_directory_search\n                                                                INNER    JOIN    user_directory    AS    d    USING    (user_id)\n                                                                LEFT    JOIN    users_in_pubic_room    AS    p    USING    (user_id)\n                                                                LEFT    JOIN    (\n                                                                                SELECT    other_user_id    AS    user_id    FROM    users_who_share_rooms\n                                                                                WHERE    user_id    =    ?    AND    share_private\n                                                                )    AS    s    USING    (user_id)\n                                                                WHERE\n                                                                                (s.user_id    IS    NOT    NULL    OR    p.user_id    IS    NOT    NULL)\n                                                                                AND    value    MATCH    ?\n                                                                ORDER    BY\n                                                                                rank(matchinfo(user_directory_search))    DESC,\n                                                                                display_name    IS    NULL,\n                                                                                avatar_url    IS    NULL\n                                                                LIMIT    ?\n                                                '
        args = (user_id, search_query, (limit + 1))
    else:
        raise Exception('Unrecognized    database    engine')
    results = (yield self._execute('search_user_dir', self.cursor_to_dict, sql, *args))
    limited = (len(results) > limit)
    defer.returnValue({'limited': limited, 'results': results})
