def test_response(self): hits = [{ u'app_id': u'fe8481a39c3738749e6a4766fca99efd', u'doc_type': u'xforminstance', u'domain': u'mikesproject', u'xmlns': u'http://openrosa.org/formdesigner/3a7cc07c-551c-4651-ab1a-d60be3017485' }, { u'app_id': u'3d622620ca00d7709625220751a7b1f9', u'doc_type': u'xforminstance', u'domain': u'mikesproject', u'xmlns': u'http://openrosa.org/formdesigner/54db1962-b938-4e2b-b00e-08414163ead4' }] fields = [u'app_id', u'doc_type', u'domain', u'xmlns'] response = ESQuerySet(self.example_response, HQESQuery('forms').fields(fields)) self.assertEquals(response.total, 5247) self.assertEquals(response.hits, hits)
def test_flatten_field_dicts(self): example_response = { 'hits': {'hits': [{ '_source': { 'domains': ['joesproject'], } }, { '_source': { 'domains': ['mikesproject'] } } ], }, } hits = [ { 'domains': 'joesproject', }, { 'domains': 'mikesproject', } ] fields = ['domain'] response = ESQuerySet( example_response, HQESQuery('forms').fields(fields) ) self.assertEqual(response.hits, hits)
def test_exclude_source(self): hits = ['8063dff5-460b-46f2-b4d0-5871abfd97d4', 'dc1376cd-0869-4c13-a267-365dfc2fa754'] response = ESQuerySet( self.example_response, HQESQuery('forms').exclude_source() ) self.assertEqual(response.hits, hits)
def run(self): result_docs = list(self._result_docs) total = len(result_docs) if self._sort_field: result_docs.sort(key=lambda doc: doc[self._sort_field], reverse=self._sort_desc) if self._size is not None: result_docs = result_docs[self._start:self._start + self._size] else: result_docs = result_docs[self._start:] def _get_doc(doc): if self._source_fields: return { key: doc[key] for key in self._source_fields if key in doc } return doc return ESQuerySet( { 'hits': { 'hits': [{ '_source': _get_doc(doc) } for doc in result_docs], 'total': total, }, }, self)
def run(self): result_docs = list(self._result_docs) total = len(result_docs) if self._sort_field: result_docs.sort(key=lambda doc: doc[self._sort_field], reverse=self._sort_desc) if self._size is not None: result_docs = result_docs[self._start:self._start + self._size] else: result_docs = result_docs[self._start:] return ESQuerySet({ 'hits': { 'hits': [{'_source': doc} for doc in result_docs], 'total': total, }, }, self)
def test_result_parsing_basic(self): query = HQESQuery('cases').aggregations([ FilterAggregation('closed', filters.term('closed', True)), FilterAggregation('open', filters.term('closed', False)) ]) raw_result = { "aggregations": { "closed": { "doc_count": 1 }, "open": { "doc_count": 2 } } } queryset = ESQuerySet(raw_result, deepcopy(query)) self.assertEqual(queryset.aggregations.closed.doc_count, 1) self.assertEqual(queryset.aggregations.open.doc_count, 2)
def test_histogram_aggregation(self): example_response = { "hits": {}, "shards": {}, "aggregations": { "forms_by_date": { "buckets": [{ "key": 1454284800000, "doc_count": 8 }, { "key": 1464284800000, "doc_count": 3 }] } } } expected_output = example_response['aggregations']['forms_by_date'][ 'buckets'] query = HQESQuery('forms').date_histogram('forms_by_date', '', '') res = ESQuerySet(example_response, query) output = res.aggregations.forms_by_date.raw_buckets self.assertEqual(output, expected_output)
def test_error(self): with self.assertRaises(ESError): ESQuerySet(self.example_error, HQESQuery('forms'))
def test_result_parsing_complex(self): query = HQESQuery('cases').aggregation( TermsAggregation("users", 'user_id').aggregation( FilterAggregation('closed', filters.term( 'closed', True))).aggregation( FilterAggregation('open', filters.term( 'closed', False)))).aggregation( RangeAggregation('by_date', 'name', [ AggregationRange(end='c'), AggregationRange(start='f'), AggregationRange(start='k', end='p') ])) raw_result = { "aggregations": { "users": { "buckets": [{ "closed": { "doc_count": 0 }, "doc_count": 2, "key": "user1", "open": { "doc_count": 2 } }], "doc_count_error_upper_bound": 0, "sum_other_doc_count": 0 }, "by_date": { "buckets": { "*-c": { "to": "c", "doc_count": 3 }, "f-*": { "from": "f", "doc_count": 8 }, "k-p": { "from": "k", "to": "p", "doc_count": 6 } } } }, } queryset = ESQuerySet(raw_result, deepcopy(query)) self.assertEqual(queryset.aggregations.users.buckets.user1.key, 'user1') self.assertEqual(queryset.aggregations.users.buckets.user1.doc_count, 2) self.assertEqual( queryset.aggregations.users.buckets.user1.closed.doc_count, 0) self.assertEqual( queryset.aggregations.users.buckets.user1.open.doc_count, 2) self.assertEqual( queryset.aggregations.users.buckets_dict['user1'].open.doc_count, 2) self.assertEqual(queryset.aggregations.users.counts_by_bucket(), {'user1': 2}) self.assertEqual(queryset.aggregations.by_date.counts_by_bucket(), { '*-c': 3, 'f-*': 8, 'k-p': 6, })