def prepare_query(query):
    """
    Prepare query to always AND all search terms

    Spaces will be replaced with AND. Existing ANDs and ORs will be preserved.
    """
    def intersperse_and(phrase):
        # For a given phrase, intersperse AND between all words,
        # except "and" which is removed
        return ' AND '.join([word for word in phrase.split()
                             if word != 'and'])

    # If the query has any ORs, split it into phrases to be ANDed
    phrases = query.split(' or ')

    # AND words in each phrase, and OR all phrases together
    return ' OR '.join(map(intersperse_and, phrases))