from __future__ import division, absolute_import, print_function

from pytz import timezone

import elasticsearch
import elasticsearch.helpers

from idb import config
from idb.helpers.logging import idblogger
from idb.helpers.conversions import fields, custom_mappings


"""
Explanation: Note: 
May need to place the notebook into the idb-backend to run properly.
End of explanation
"""


# u = "4dce41dc-2af6-448c-99e1-abfd3a9cc3e5"
u = "db4bb0df-8539-4617-ab5f-eb118aa3126b"

serverlist = config.config["elasticsearch"]["servers"]



def get_connection(**kwargs):
    kwargs.setdefault('hosts', config.config["elasticsearch"]["servers"])
    kwargs.setdefault('retry_on_timeout', True)  # this isn't valid until >=1.3                                                                                                                                                        
    kwargs.setdefault('sniff_on_start', False)
    kwargs.setdefault('sniff_on_connection_fail', False)
    kwargs.setdefault('max_retries', 10)
    kwargs.setdefault('timeout', 30)
    return elasticsearch.Elasticsearch(**kwargs)


conn = get_connection(hosts=serverlist)

r = conn.search(index="idigbio", doc_type="recordsets", _source=["etag"], body={
        "query": {
          "bool": {
            "must": [
              {
                "query_string": {
                  "default_field": "_id",
                  "query": u
                }
              }
            ],
            "must_not": [],
            "should": []
          }
        },
        "from": 0,
        "size": 10,
        "sort": [],
        "aggs": {}
      })

r['hits']

"""
Explanation: u is the uuid of the recordset that we wish to delete
End of explanation
"""


#    parent = r['hits']['hits'][0]['_parent']
#    conn.delete(index='idigbio', id=u, parent=parent, doc_type='mediarecords')

conn.delete(index='idigbio', id=u, doc_type='recordsets')


"""
Explanation: This from Nathan's example of deleting a mediarecord where we need the parent record:
End of explanation
"""


