def get_ads(uri_list):
     """Get a set of ads from an ES index.
     eg "els.istresearch.com:39200"
     """
     esm = ElasticSearchManager()
     results = esm.search_es(ElasticSearchManager.create_ids_query(uri_list), len(uri_list))
     hits = results['hits']['hits']
     return map(lambda x: x['_source'], hits)
def process_csv():
    try:
        json_data = json.loads(str(request.get_data()))
        esm = ElasticSearchManager()
        es_request = convert_csv_to_esrequest(json_data['csv'])

        size = request.args.get('size')
        headings = request.args.get('heading')
        store = request.args.get('store')

        if size is None:
            size = '20'

        if headings is None:
            headings = '0'

        if store is None:
            store = '1'

        bf = BulkFolders()

        if headings == "1":
            result = "\t".join(bf.ht_headings) + '\n'
        else:
            result = ''

        # print result

        if 'ids' in es_request:
            result += process_results(bf, esm.search_es(ElasticSearchManager.create_ids_query
                                                        (es_request['ids']), len(es_request['ids'])))
        if 'phone' in es_request:
            result += process_results(bf,
                                      esm.search_es(ElasticSearchManager.create_terms_query(phone_field,
                                                    es_request['phone']), int(size)))
        if store == "1":
            response = make_response(result)
            response.headers["Content-Disposition"] = "attachment; filename=data.tsv"
            return response
        else:
            return Response(result, 200)
    except Exception as e:
        return Response(str(e), 500)
def get_ads():
    es = ElasticSearchManager()
    bf = BulkFolders()

    ad_id = request.args.get('uri')
    postids = request.args.get('post_ids')
    phone = request.args.get('phone')
    size = request.args.get('size')
    headings = request.args.get('heading')
    store = request.args.get('store')

    """first line columns names if headings = 1"""
    if headings is None:
        headings = '0'

    if size is None:
        size = "20"

    if headings == "1":
        result = "\t".join(bf.ht_headings) + '\n'
    else:
        result = ''

    if store is None:
        store = "1"

    try:
        if ad_id is not None:
            ids = [ad_id]
            result += process_results(bf, es.search_es(ElasticSearchManager.create_ids_query(ids), None))

            if store == "1":
                response = make_response(result)
                response.headers["Content-Disposition"] = "attachment; filename=data.tsv"
                return response
            else:
                return Response(result, 200)
    except Exception as e:
        return Response(str(e), 500)

    try:
        if postids is not None:
            post_ids = postids.split(',')
            for post_id in post_ids:
                res = es.search_es(ElasticSearchManager.create_postid_query(post_id), int(size))
                hits = res['hits']['hits']
                for hit in hits:
                    ad = hit['_source']
                    if post_id in ad['url']:
                        tab_separated = "\t".join(bf.ht_to_array(ad))
                        result = result + tab_separated + '\n'

            if store == "1":
                response = make_response(result)
                response.headers["Content-Disposition"] = "attachment; filename=data.tsv"
                return response
            else:
                return Response(result, 200)
    except Exception as e:
        return Response(str(e), 500)

    try:
        if phone is not None:
            phones = [phone]
            result += process_results(bf, es.search_es(
                ElasticSearchManager.create_terms_query(phone_field, phones), int(size)))

            if store == "1":
                response = make_response(result)
                response.headers["Content-Disposition"] = "attachment; filename=data.tsv"
                return response
            else:
                return Response(result, 200)
    except Exception as e:
        return Response(str(e), 500)