From e19853f1baff0101bb8f4951196e330c6f7847de Mon Sep 17 00:00:00 2001 From: Honza Kral Date: Sun, 16 Jun 2013 16:20:03 +0200 Subject: [PATCH] indices.exists_type --- elasticsearch/client.py | 16 ++++++++++ test_elasticsearch/test_server/test_common.py | 32 ++++++++++++------- .../test_server/yaml/20_mapping.yaml | 14 ++++++++ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/elasticsearch/client.py b/elasticsearch/client.py index 864a4aab..0793563b 100644 --- a/elasticsearch/client.py +++ b/elasticsearch/client.py @@ -175,6 +175,22 @@ class InidicesClient(NamespacedClient): return False return True + @query_params('ignore_indices') + def exists_type(self, index, doc_type, params=None): + """ + Used to check if a type/types exists in an index/indices (available since 0. + http://www.elasticsearch.org/guide/reference/api/admin-indices-types-exists/ + + :arg index: A comma-separated list of index names; use `_all` to check the types across all indices + :arg doc_type: A comma-separated list of document types to check + :arg ignore_indices: When performed on multiple indices, allows to ignore `missing` ones, default u'none' + """ + try: + self.transport.perform_request('HEAD', _make_path(index, doc_type), params=params) + except NotFoundError: + return False + return True + @query_params('ignore_conflicts', 'timeout') def put_mapping(self, index, body, doc_type=None, params=None): """ diff --git a/test_elasticsearch/test_server/test_common.py b/test_elasticsearch/test_server/test_common.py index b45269e1..0aca1e6e 100644 --- a/test_elasticsearch/test_server/test_common.py +++ b/test_elasticsearch/test_server/test_common.py @@ -59,19 +59,27 @@ class YamlTestCase(TestCase): def run_is(self, action, transform=None): """ Match part of last response to test data. """ - self.assertEquals(1, len(action)) - path, expected = list(action.items())[0] - # fetch the possibly nested value from last_response - value = self.last_response - for step in path.split('.'): - if step.isdigit(): - step = int(step) - self.assertIsInstance(value, list) - self.assertGreater(len(value), step) - else: - self.assertIn(step, value) - value = value[step] + # matching part of the reponse dict + if isinstance(action, dict): + self.assertEquals(1, len(action)) + path, expected = list(action.items())[0] + + # fetch the possibly nested value from last_response + value = self.last_response + for step in path.split('.'): + if step.isdigit(): + step = int(step) + self.assertIsInstance(value, list) + self.assertGreater(len(value), step) + else: + self.assertIn(step, value) + value = value[step] + + # matching the entire response + else: + value = self.last_response + expected = action # sometimes we need to transform the json value before comparing if transform: diff --git a/test_elasticsearch/test_server/yaml/20_mapping.yaml b/test_elasticsearch/test_server/yaml/20_mapping.yaml index 6d3c22de..8c9fa204 100644 --- a/test_elasticsearch/test_server/yaml/20_mapping.yaml +++ b/test_elasticsearch/test_server/yaml/20_mapping.yaml @@ -29,3 +29,17 @@ indices.get_mapping: index: test - length: { test: 0 } +--- +"Test type exists": + - do: + indices.exists_type: + index: test + type: test-type + - is: true +--- +"Test type doesn't exists": + - do: + indices.exists_type: + index: test + type: not-test-type + - is: false