<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def search(self, query, fetch_messages=False, thread_limit=5, message_limit=5):
 """ Searches for messages in all threads :param query: Text to search for :param fetch_messages: Whether to fetch :class:`models.Message` objects or IDs only :param thread_limit: Max. number of threads to retrieve :param message_limit: Max. number of messages to retrieve :type thread_limit: int :type message_limit: int :return: Dictionary with thread IDs as keys and generators to get messages as values :rtype: generator :raises: FBchatException if request failed """

        data = {"query": query, "snippetLimit": thread_limit}
        j = self._post(
            self.req_url.SEARCH_MESSAGES, data, fix_request=True, as_json=True
        )
        result = j["payload"]["search_snippets"][query]

        if fetch_messages:
            search_method = self.searchForMessages
        else:
            search_method = self.searchForMessageIDs

        return {
            thread_id: search_method(query, limit=message_limit, thread_id=thread_id)
            for thread_id in result
        }