from datetime import datetime


def findDateOfFirstIndex(es_client, type, query):
    """
    Query for 1 document from all indicies (he query should be sorted
    in time order) the index the document belongs to is the start index.

    Returns the date of the first index or None if no documents are found
    """

    hits = es_client.search(index="_all", doc_type=type, 
            body=query, from_=0, size=1)

    if len(hits['hits']['hits']) > 0:
        date_str = hits['hits']['hits'][0]['_index'].lstrip("logstash-")     
        
        return datetime.strptime(date_str, "%Y.%m.%d")
    else:
        return None