def test_es_indices(self, m_conn): """Indices tests. To avoid Elasticsearch calls, we mock out the get_connection call, then set up additional mocks for the resulting ES connection. """ m_es = mock.Mock(Elasticsearch, name='es') m_indices = mock.MagicMock(IndicesClient, name='indices') m_es.indices = m_indices m_es.indices.status.return_value = { 'indices': { 'index1': 'value1', 'not_index1': 'value3' } } m_conn.return_value = m_es # test with no prefix provided self.assertEqual(es_indices(conn=es_conn()), "_all") # test with no params self.assertEqual(es_indices(), "_all") # test with no conn result = es_indices(prefix='index') self.assertTrue(m_es.indices.status.called) self.assertIn('index1', result) self.assertNotIn('not_index1', result) # test with prefix result = es_indices('index', es_conn()) self.assertIn('index1', result) self.assertNotIn('not_index1', result)
def test_es_indices(self, m_conn): """Indices tests. To avoid Elasticsearch calls, we mock out the get_connection call, then set up additional mocks for the resulting ES connection. """ m_es = mock.Mock(Elasticsearch, name="es") m_indices = mock.MagicMock(IndicesClient, name="indices") m_es.indices = m_indices m_es.indices.status.return_value = {"indices": {"index1": "value1", "not_index1": "value3"}} m_conn.return_value = m_es # test with no prefix provided self.assertEqual(es_indices(conn=es_conn()), "_all") # test with no params self.assertEqual(es_indices(), "_all") # test with no conn result = es_indices(prefix="index") self.assertTrue(m_es.indices.status.called) self.assertIn("index1", result) self.assertNotIn("not_index1", result) # test with prefix result = es_indices("index", es_conn()) self.assertIn("index1", result) self.assertNotIn("not_index1", result)
def delete(self, using=None, index=None, **kwargs): """Deletes a record from the database. See elasticsearch-dsl for parameter information. """ if index is None: index = es_indices(self.INDEX_PREFIX) return super(DailyIndexDocType, self).delete(using, index, **kwargs)
def get(cls, id, using=None, index=None, **kwargs): """Gets a record from the database. See elasticsearch-dsl for parameter information. """ if index is None: index = es_indices(cls.INDEX_PREFIX) return super(DailyIndexDocType, cls).get(id, using, index, **kwargs)
def search(cls): """Gets a generic Log search object. See elasticsearch-dsl for parameter information. """ return Search( index=es_indices(cls.INDEX_PREFIX), doc_type={cls._doc_type.name: cls.from_es}, ).sort(cls.SORT).using(es_conn())
def get_field_mapping(cls, field): """Return a field mapping.""" conn = es_conn() index = es_indices(cls.INDEX_PREFIX) return conn.indices.get_field_mapping(field, index, cls._doc_type.name, include_defaults=True, allow_no_indices=False)
def field_has_raw(self, field): """Return boolean indicating whether the field has a .raw version.""" conn = DailyIndexDocType._doc_type.using index = es_indices(self.index_prefix) try: mapping = conn.indices.get_field_mapping(field, index, self.doc_type, include_defaults=True, allow_no_indices=False) return 'raw' in \ mapping[mapping.keys()[-1]]['mappings'][self.doc_type][ field]['mapping'][field]['fields'] except KeyError: return False
def field_has_raw(self, field): """Return boolean indicating whether the field has a .raw version.""" conn = DailyIndexDocType._doc_type.using index = es_indices(self.index_prefix) try: mapping = conn.indices.get_field_mapping( field, index, self.doc_type, include_defaults=True, allow_no_indices=False) return 'raw' in \ mapping[mapping.keys()[-1]]['mappings'][self.doc_type][ field]['mapping'][field]['fields'] except KeyError: return False
def test_es_indices(self, m_conn): """Indices tests. To avoid Elasticsearch calls, we mock out the get_connection call, then set up additional mocks for the resulting ES connection. """ m_es = mock.Mock(Elasticsearch, name='es') m_indices = mock.MagicMock(IndicesClient, name='indices') m_es.indices = m_indices m_es.indices.status.return_value = { 'indices': { 'index1': 'value1', 'not_index1': 'value1', 'index2-': 'value2', 'not_index2-': 'value2' } } m_conn.return_value = m_es # test with no prefix provided self.assertEqual(es_indices(conn=es_conn()), "_all") # test with no params self.assertEqual(es_indices(), "_all") # test with no conn, prefix has neither dash nor wildcard result = es_indices(prefix='index1') self.assertTrue(m_es.indices.status.called) self.assertIn('index1', result) self.assertNotIn('not_index1', result) # test with prefix which has neither dash nor wildcard result = es_indices('index1', es_conn()) self.assertIn('index1', result) self.assertNotIn('not_index1', result) # test with no conn, prefix has wildcard but no dash result = es_indices(prefix='index1*') self.assertTrue(m_es.indices.status.called) self.assertIn('index1', result) self.assertNotIn('not_index1', result) # test with prefix which has wildcard but no dash result = es_indices('index1*', es_conn()) self.assertIn('index1', result) self.assertNotIn('not_index1', result) # test with no conn, prefix has dash but no wildcard result = es_indices(prefix='index2-') self.assertTrue(m_es.indices.status.called) self.assertIn('index2-', result) self.assertNotIn('not_index2-', result) # test with prefix which has dash but no wildcard result = es_indices('index2-', es_conn()) self.assertIn('index2-', result) self.assertNotIn('not_index2-', result) # test with no conn, prefix has dash and wildcard result = es_indices(prefix='index2-*') self.assertTrue(m_es.indices.status.called) self.assertIn('index2-', result) self.assertNotIn('not_index2-', result) # test with prefix which has dash and wildcard result = es_indices('index2-*', es_conn()) self.assertIn('index2-', result) self.assertNotIn('not_index2-', result)