from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search
from datetime import datetime
from elasticsearch_dsl.connections import connections


es = Elasticsearch([{'host':'202.112.51.162','port':9200}])

def findOne(table,sources=None,detail={}):
    query = {}
    if sources is not None:
        query['_source'] = sources
    query.update(detail)
    page = es.search(index=table,body=query)
    scroll_size = page['hits']['total']
    if scroll_size>0:
        return page['hits']['hits'][0]['_source']
    else:
        return None


def loadData(table,sources=None,detail={}):
    query = {
        # "sort": ["_doc"]
    }
    if sources is not None:
        query['_source'] = sources
    query.update(detail)
    page = es.search(index=table, size=1000, scroll='2m', body=query)
    sid = page['_scroll_id']
    scroll_size = page['hits']['total']
    yield page['hits']['hits']
    while scroll_size > 0:
        page = es.scroll(scroll_id=sid, scroll='2m')
        # Update the scroll ID
        sid = page['_scroll_id']
        # Get the number of results that we returned in the last scroll
        scroll_size = len(page['hits']['hits'])
        yield page['hits']['hits']




