Note: May need to place the notebook into the idb-backend to run properly.


In [1]:
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

u is the uuid of the recordset that we wish to delete


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

In [3]:
serverlist = config.config["elasticsearch"]["servers"]

In [4]:
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)

In [5]:
conn = get_connection(hosts=serverlist)

In [6]:
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": {}
      })

In [7]:
r['hits']


Out[7]:
{u'hits': [], u'max_score': None, u'total': 0}

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


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

In [34]:
conn.delete(index='idigbio', id=u, doc_type='recordsets')


---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
<ipython-input-34-1711e0f60d37> in <module>()
----> 1 conn.delete(index='idigbio', id=u, doc_type='recordsets')

/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.pyc in _wrapped(*args, **kwargs)
     66                 if p in kwargs:
     67                     params[p] = kwargs.pop(p)
---> 68             return func(*args, params=params, **kwargs)
     69         return _wrapped
     70     return _wrapper

/usr/local/lib/python2.7/dist-packages/elasticsearch/client/__init__.pyc in delete(self, index, doc_type, id, params)
    592         :arg version_type: Specific version type
    593         """
--> 594         _, data = self.transport.perform_request('DELETE', _make_path(index, doc_type, id), params=params)
    595         return data
    596 

/usr/local/lib/python2.7/dist-packages/elasticsearch/transport.pyc in perform_request(self, method, url, params, body)
    282 
    283             try:
--> 284                 status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
    285             except ConnectionError:
    286                 self.mark_dead(connection)

/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/http_urllib3.pyc in perform_request(self, method, url, params, body, timeout, ignore)
     53         if not (200 <= response.status < 300) and response.status not in ignore:
     54             self.log_request_fail(method, url, body, duration, response.status)
---> 55             self._raise_error(response.status, raw_data)
     56 
     57         self.log_request_success(method, full_url, url, body, response.status,

/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.pyc in _raise_error(self, status_code, raw_data)
     95             pass
     96 
---> 97         raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
     98 
     99 

NotFoundError: TransportError(404, u'{"found":false,"_index":"idigbio-2.9.3","_type":"recordsets","_id":"4dce41dc-2af6-448c-99e1-abfd3a9cc3e5","_version":1,"_shards":{"total":3,"successful":3,"failed":0}}')

In [ ]: